Skip to content

Commit bd03b00

Browse files
committed
External function example for SearchIteratorV2
Signed-off-by: yhmo <yihua.mo@zilliz.com>
1 parent 9e35eb9 commit bd03b00

2 files changed

Lines changed: 23 additions & 5 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
}

sdk-core/src/test/java/io/milvus/v2/client/MilvusClientV2DockerTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,12 +1570,14 @@ public void testIterator() {
15701570
Assertions.assertEquals(DIMENSION*2, bfloat16Vector.limit());
15711571

15721572
SortedMap<Long, Float> sparseVector = (SortedMap<Long, Float>)entity.get("sparse_vector");
1573-
Assertions.assertTrue(sparseVector.size() >= 10 && sparseVector.size() <= 20); // defined in generateSparseVector()
1573+
Assertions.assertTrue(sparseVector.size() >= 10 && sparseVector.size() < 20); // defined in generateSparseVector()
15741574

15751575
counter++;
15761576
}
15771577
}
1578-
Assertions.assertEquals((int)count - 50, counter);
1578+
// search iterator could not ensure that all the entities can be retrieved
1579+
// expect count is 9950, but sometimes it returns 9949 or 9948
1580+
Assertions.assertTrue(counter > ((int)count - 55) && counter <= ((int)count - 50));
15791581

15801582
client.dropCollection(DropCollectionReq.builder().collectionName(randomCollectionName).build());
15811583
}

0 commit comments

Comments
 (0)