Skip to content

Commit 2be2932

Browse files
committed
feat(search): add limit parameter to SearchIndex.search() methods
Add pagination control to search methods by exposing LIMIT parameter: - Add DEFAULT_NUM_RESULTS constant (10) for consistent defaults - Add search(query, params, limit) and search(query, params, offset, limit) overloads - Update searchWithSort() to accept numResults parameter - All search methods now properly use FTSearchParams.limit() and dialect(2) This allows users to control result pagination without falling back to direct Jedis calls. VectorQuery.getNumResults() is now properly passed through to the underlying search implementation.
1 parent 2312bbb commit 2be2932

File tree

1 file changed

+55
-15
lines changed

1 file changed

+55
-15
lines changed

core/src/main/java/com/redis/vl/index/SearchIndex.java

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,24 +1212,33 @@ public SearchResult search(VectorQuery query) {
12121212
// Convert VectorQuery to search string
12131213
String queryString = query.toQueryString();
12141214
Map<String, Object> params = query.toParams();
1215+
int numResults = query.getNumResults();
12151216

12161217
// Handle sorting or inOrder if specified
12171218
if ((query.getSortBy() != null && !query.getSortBy().isEmpty()) || query.isInOrder()) {
12181219
return searchWithSort(
1219-
queryString, params, query.getSortBy(), query.isSortDescending(), query.isInOrder());
1220+
queryString,
1221+
params,
1222+
query.getSortBy(),
1223+
query.isSortDescending(),
1224+
query.isInOrder(),
1225+
numResults);
12201226
}
12211227

1222-
return search(queryString, params);
1228+
return search(queryString, params, numResults);
12231229
}
12241230

1231+
/** Default number of results to return when limit is not specified. */
1232+
public static final int DEFAULT_NUM_RESULTS = 10;
1233+
12251234
/**
12261235
* Search the index using a query string
12271236
*
12281237
* @param query Query string
12291238
* @return Search results
12301239
*/
12311240
public SearchResult search(String query) {
1232-
return search(query, new HashMap<>());
1241+
return search(query, new HashMap<>(), DEFAULT_NUM_RESULTS);
12331242
}
12341243

12351244
/**
@@ -1240,27 +1249,53 @@ public SearchResult search(String query) {
12401249
* @return Search results
12411250
*/
12421251
public SearchResult search(String query, Map<String, Object> params) {
1252+
return search(query, params, DEFAULT_NUM_RESULTS);
1253+
}
1254+
1255+
/**
1256+
* Search the index using a query string with parameters and limit
1257+
*
1258+
* @param query Query string
1259+
* @param params Query parameters
1260+
* @param limit Maximum number of results to return
1261+
* @return Search results
1262+
*/
1263+
public SearchResult search(String query, Map<String, Object> params, int limit) {
1264+
return search(query, params, 0, limit);
1265+
}
1266+
1267+
/**
1268+
* Search the index using a query string with parameters, offset, and limit
1269+
*
1270+
* @param query Query string
1271+
* @param params Query parameters
1272+
* @param offset Number of results to skip (for pagination)
1273+
* @param limit Maximum number of results to return
1274+
* @return Search results
1275+
*/
1276+
public SearchResult search(String query, Map<String, Object> params, int offset, int limit) {
12431277
if (!exists()) {
12441278
throw new RedisVLException("Index " + getName() + " does not exist");
12451279
}
12461280

12471281
UnifiedJedis jedis = getUnifiedJedis();
12481282
try {
1249-
if (params != null && !params.isEmpty()) {
1250-
// Convert params to FTSearchParams
1251-
redis.clients.jedis.search.FTSearchParams searchParams =
1252-
new redis.clients.jedis.search.FTSearchParams();
1283+
// Convert params to FTSearchParams
1284+
redis.clients.jedis.search.FTSearchParams searchParams =
1285+
new redis.clients.jedis.search.FTSearchParams();
12531286

1254-
// Set dialect to 2 for KNN queries
1255-
searchParams.dialect(2);
1287+
// Set dialect to 2 for KNN queries
1288+
searchParams.dialect(2);
12561289

1257-
// Add vector parameters
1258-
addParamsToSearchParams(searchParams, params);
1290+
// Set limit for pagination
1291+
searchParams.limit(offset, limit);
12591292

1260-
return jedis.ftSearch(schema.getName(), query, searchParams);
1261-
} else {
1262-
return jedis.ftSearch(schema.getName(), query);
1293+
// Add vector parameters if present
1294+
if (params != null && !params.isEmpty()) {
1295+
addParamsToSearchParams(searchParams, params);
12631296
}
1297+
1298+
return jedis.ftSearch(schema.getName(), query, searchParams);
12641299
} catch (Exception e) {
12651300
throw new RuntimeException("Failed to search index: " + e.getMessage(), e);
12661301
}
@@ -1274,14 +1309,16 @@ public SearchResult search(String query, Map<String, Object> params) {
12741309
* @param sortBy Field to sort by (can be null)
12751310
* @param descending Whether to sort in descending order
12761311
* @param inOrder Whether to require terms in field to have same order as in query
1312+
* @param numResults Maximum number of results to return
12771313
* @return Search results
12781314
*/
12791315
private SearchResult searchWithSort(
12801316
String query,
12811317
Map<String, Object> params,
12821318
String sortBy,
12831319
boolean descending,
1284-
boolean inOrder) {
1320+
boolean inOrder,
1321+
int numResults) {
12851322
if (!exists()) {
12861323
throw new RedisVLException("Index " + getName() + " does not exist");
12871324
}
@@ -1295,6 +1332,9 @@ private SearchResult searchWithSort(
12951332
// Set dialect to 2 for KNN queries
12961333
searchParams.dialect(2);
12971334

1335+
// Set limit for pagination
1336+
searchParams.limit(0, numResults);
1337+
12981338
// Add vector parameters if present
12991339
if (params != null && !params.isEmpty()) {
13001340
addParamsToSearchParams(searchParams, params);

0 commit comments

Comments
 (0)