Skip to content

Commit 96a6a57

Browse files
committed
External function example for SearchIteratorV2
Signed-off-by: yhmo <yihua.mo@zilliz.com>
1 parent 2a8feb3 commit 96a6a57

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

examples/src/main/java/io/milvus/v2/IteratorExample.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.apache.commons.lang3.StringUtils;
4444

4545
import java.util.*;
46+
import java.util.function.Function;
4647

4748
public class IteratorExample {
4849
private static final MilvusClientV2 client;
@@ -196,7 +197,8 @@ private static void searchIteratorV1(String expr, String params, int batchSize,
196197
// Search iterator V2
197198
// In SDK v2.5.6, we provide a new search iterator implementation. SearchIteratorV2 is recommended.
198199
// SearchIteratorV2 is faster than V1 by 20~30 percent, and the recall is a little better than V1.
199-
private static void searchIteratorV2(String filter, Map<String, Object> params, int batchSize, int topK) {
200+
private static void searchIteratorV2(String filter, Map<String, Object> params, int batchSize, int topK,
201+
Function<List<SearchResp.SearchResult>, List<SearchResp.SearchResult>> externalFilterFunc) {
200202
System.out.println("\n========== searchIteratorV2() ==========");
201203
System.out.println(String.format("expr='%s', params='%s', batchSize=%d, topK=%d",
202204
filter, params==null ? "" : params.toString(), batchSize, topK));
@@ -211,6 +213,7 @@ private static void searchIteratorV2(String filter, Map<String, Object> params,
211213
.topK(topK)
212214
.metricType(IndexParam.MetricType.L2)
213215
.consistencyLevel(ConsistencyLevel.BOUNDED)
216+
.externalFilterFunc(externalFilterFunc)
214217
.build());
215218

216219
System.out.println("SearchIteratorV2 results:");
@@ -236,10 +239,23 @@ public static void main(String[] args) {
236239
queryIterator("userID < 300",50, 5,400);
237240
searchIteratorV1("userAge > 50 &&userAge < 100", "{\"range_filter\": 15.0, \"radius\": 20.0}", 100, 500);
238241
searchIteratorV1("", "", 10, 99);
239-
searchIteratorV2("userAge > 10 &&userAge < 20", null, 50, 100);
242+
searchIteratorV2("userAge > 10 &&userAge < 20", null, 50, 120, null);
240243

241244
Map<String,Object> extraParams = new HashMap<>();
242245
extraParams.put("radius",15.0);
243-
searchIteratorV2("", extraParams, 50, 100);
246+
searchIteratorV2("", extraParams, 50, 100, null);
247+
248+
// use external function to filter the result
249+
Function<List<SearchResp.SearchResult>, List<SearchResp.SearchResult>> externalFilterFunc = (List<SearchResp.SearchResult> src)->{
250+
List<SearchResp.SearchResult> newRes = new ArrayList<>();
251+
for (SearchResp.SearchResult res : src) {
252+
long id = (long)res.getId();
253+
if (id%2 == 0) {
254+
newRes.add(res);
255+
}
256+
}
257+
return newRes;
258+
};
259+
searchIteratorV2("userAge < 20", null, 50, 88, externalFilterFunc);
244260
}
245261
}

0 commit comments

Comments
 (0)