Skip to content

Commit bd1b008

Browse files
yhmolikun
authored andcommitted
Support FunctionScore, multi-reranker for search/hybridSearch (milvus-io#1588)
Signed-off-by: yhmo <[email protected]>
1 parent 37bb91e commit bd1b008

File tree

7 files changed

+1078
-194
lines changed

7 files changed

+1078
-194
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.milvus.v2.service.collection.request.CreateCollectionReq;
3232
import io.milvus.v2.service.collection.request.DropCollectionReq;
3333
import io.milvus.v2.service.vector.request.AnnSearchReq;
34+
import io.milvus.v2.service.vector.request.FunctionScore;
3435
import io.milvus.v2.service.vector.request.HybridSearchReq;
3536
import io.milvus.v2.service.vector.request.InsertReq;
3637
import io.milvus.v2.service.vector.request.QueryReq;
@@ -122,7 +123,6 @@ private void createCollection() {
122123
.metricType(BINARY_VECTOR_METRIC)
123124
.build());
124125
Map<String,Object> fv16Params = new HashMap<>();
125-
fv16Params.clear();
126126
fv16Params.put("M",16);
127127
fv16Params.put("efConstruction",64);
128128
indexes.add(IndexParam.builder()
@@ -212,7 +212,9 @@ private void hybridSearch() {
212212
HybridSearchReq hybridSearchReq = HybridSearchReq.builder()
213213
.collectionName(COLLECTION_NAME)
214214
.searchRequests(searchRequests)
215-
.ranker(WeightedRanker.builder().weights(Arrays.asList(0.2f, 0.5f, 0.6f)).build())
215+
.functionScore(FunctionScore.builder()
216+
.addFunction(WeightedRanker.builder().weights(Arrays.asList(0.2f, 0.5f, 0.6f)).build())
217+
.build())
216218
.limit(5)
217219
.consistencyLevel(ConsistencyLevel.BOUNDED)
218220
.build();

sdk-core/src/main/java/io/milvus/orm/iterator/QueryIterator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,10 @@ private boolean isResSufficient(List<QueryResultsWrapper.RowRecord> ret) {
210210
private QueryResults executeQuery(String expr, long offset, long limit, long ts, boolean isSeek) {
211211
// for seeking offset, no need to return output fields
212212
List<String> outputFields = new ArrayList<>();
213+
boolean reduceStopForBest = queryIteratorParam.isReduceStopForBest();
213214
if (!isSeek) {
214215
outputFields = queryIteratorParam.getOutFields();
216+
reduceStopForBest = false;
215217
}
216218
QueryParam queryParam = QueryParam.newBuilder()
217219
.withDatabaseName(queryIteratorParam.getDatabaseName())
@@ -230,7 +232,7 @@ private QueryResults executeQuery(String expr, long offset, long limit, long ts,
230232
// reduce stop for best
231233
builder.addQueryParams(KeyValuePair.newBuilder()
232234
.setKey(Constant.REDUCE_STOP_FOR_BEST)
233-
.setValue(String.valueOf(queryIteratorParam.isReduceStopForBest()))
235+
.setValue(String.valueOf(reduceStopForBest))
234236
.build());
235237

236238
// iterator

sdk-core/src/main/java/io/milvus/v2/service/collection/request/CreateCollectionReq.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private CreateCollectionReq(Builder builder) {
6969
if (builder.collectionName == null) {
7070
throw new IllegalArgumentException("Collection name cannot be null");
7171
}
72-
72+
7373
this.databaseName = builder.databaseName;
7474
this.collectionName = builder.collectionName;
7575
this.description = builder.description;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package io.milvus.v2.service.vector.request;
21+
22+
import io.milvus.v2.service.collection.request.CreateCollectionReq;
23+
import lombok.Builder;
24+
import lombok.Data;
25+
import lombok.experimental.SuperBuilder;
26+
27+
import java.util.ArrayList;
28+
import java.util.HashMap;
29+
import java.util.List;
30+
import java.util.Map;
31+
32+
@Data
33+
@SuperBuilder
34+
public class FunctionScore {
35+
@Builder.Default
36+
private List<CreateCollectionReq.Function> functions = new ArrayList<>();
37+
@Builder.Default
38+
private Map<String, String> params = new HashMap<>();
39+
40+
public static abstract class FunctionScoreBuilder<C extends FunctionScore, B extends FunctionScore.FunctionScoreBuilder<C, B>> {
41+
public B addFunction(CreateCollectionReq.Function func) {
42+
if(null == this.functions$value ){
43+
this.functions$value = new ArrayList<>();
44+
}
45+
this.functions$value.add(func);
46+
this.functions$set = true;
47+
return self();
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)