Skip to content

Commit 4121283

Browse files
committed
Support NGRAM and BoostRanker
Signed-off-by: yhmo <yihua.mo@zilliz.com>
1 parent 70e14f4 commit 4121283

3 files changed

Lines changed: 112 additions & 3 deletions

File tree

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,32 @@ public static void main(String[] args) {
9898
.metricType(IndexParam.MetricType.COSINE)
9999
.build());
100100

101+
// Create INVERTED index for a specific entry of JSON field
102+
// Index for JSON field is supported from milvus v2.5.7 and fully supported in v2.5.13+
103+
// Read the doc for more info: https://milvus.io/docs/json-indexing.md
104+
Map<String,Object> p1 = new HashMap<>();
105+
p1.put("json_path", "metadata[\"flags\"]");
106+
p1.put("json_cast_type", "array_double");
107+
indexes.add(IndexParam.builder()
108+
.fieldName(JSON_FIELD)
109+
.indexType(IndexParam.IndexType.INVERTED)
110+
.extraParams(p1)
111+
.build());
112+
113+
// Create NGRAM index for a specific entry of JSON field
114+
// NGRAM index for JSON field is supported from milvus v2.6.2
115+
// Read the doc for more info: https://milvus.io/docs/ngram.md
116+
Map<String,Object> p2 = new HashMap<>();
117+
p2.put("json_path","metadata[\"path\"]");
118+
p2.put("json_cast_type", "varchar");
119+
p2.put("min_gram", 3);
120+
p2.put("max_gram", 5);
121+
indexes.add(IndexParam.builder()
122+
.fieldName(JSON_FIELD)
123+
.indexType(IndexParam.IndexType.NGRAM)
124+
.extraParams(p2)
125+
.build());
126+
101127
CreateCollectionReq requestCreate = CreateCollectionReq.builder()
102128
.collectionName(COLLECTION_NAME)
103129
.collectionSchema(collectionSchema)
@@ -121,7 +147,7 @@ public static void main(String[] args) {
121147
// Note: for JSON field, always construct a real JsonObject
122148
// don't use row.addProperty(JSON_FIELD, strContent) since the value is treated as a string, not a JsonObject
123149
JsonObject metadata = new JsonObject();
124-
metadata.addProperty("path", String.format("\\root/abc/path_%d", i));
150+
metadata.addProperty("path", String.format("\\root/abc_%d/path_%d", i, i));
125151
metadata.addProperty("size", i);
126152
if (i%7 == 0) {
127153
metadata.addProperty("special", true);
@@ -197,6 +223,7 @@ public static void main(String[] args) {
197223
queryWithExpr(client, "JSON_CONTAINS(metadata[\"flags\"], 9)");
198224
queryWithExpr(client, "JSON_CONTAINS_ANY(metadata[\"flags\"], [8, 9, 10])");
199225
queryWithExpr(client, "JSON_CONTAINS_ALL(metadata[\"flags\"], [8, 9, 10])");
226+
queryWithExpr(client, "metadata[\"path\"] LIKE \"%c_5%\"");
200227
queryWithExpr(client, "dynamic1 < 2.0");
201228

202229
client.close();

sdk-core/src/main/java/io/milvus/v2/common/IndexParam.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,15 @@ public enum IndexType {
8989
// Only for varchar type field
9090
TRIE("Trie", 100),
9191

92+
// Only for varchar type field and json_path of JSON field
93+
NGRAM(101),
94+
9295
// Only for geometry type field
93-
RTREE("RTREE", 120),
96+
RTREE(120),
9497

9598
// Only for scalar type field
9699
STL_SORT(200), // only for numeric type field
97-
INVERTED(201), // works for all scalar fields except JSON type field
100+
INVERTED(201), // works for all scalar fields and json_path of JSON field
98101
BITMAP(202), // works for all scalar fields except JSON, FLOAT and DOUBLE type fields
99102

100103
// Only for sparse vectors
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.ranker;
21+
22+
import io.milvus.common.clientenum.FunctionType;
23+
import io.milvus.common.utils.JsonUtils;
24+
import io.milvus.v2.service.collection.request.CreateCollectionReq;
25+
import lombok.experimental.SuperBuilder;
26+
import org.apache.commons.lang3.StringUtils;
27+
28+
import java.util.HashMap;
29+
import java.util.Map;
30+
31+
/**
32+
* The Decay reranking strategy, which by adjusting search rankings based on numeric field values.
33+
* Read the doc for more info: https://milvus.io/docs/decay-ranker-overview.md
34+
*
35+
* You also can declare a decay ranker by Function
36+
* CreateCollectionReq.Function rr = CreateCollectionReq.Function.builder()
37+
* .functionType(FunctionType.RERANK)
38+
* .name("xxx_boost")
39+
* .description("random on xxx")
40+
* .param("reranker", "boost")
41+
* .param("filter", "xx == 2")
42+
* .param("weight", "0.5")
43+
* .param("random_score", "{\"seed\": 123, \"field\": \"id\"}")
44+
* .build();
45+
*/
46+
@SuperBuilder
47+
public class BoostRanker extends CreateCollectionReq.Function {
48+
private String filter;
49+
private Float weight;
50+
private Long randomScoreSeed;
51+
private String randomScoreField;
52+
53+
public FunctionType getFunctionType() {
54+
return FunctionType.RERANK;
55+
}
56+
57+
public Map<String, String> getParams() {
58+
Map<String, String> props = super.getParams();
59+
props.put("reranker", "boost");
60+
if (!StringUtils.isEmpty(filter)) {
61+
props.put("filter", filter);
62+
}
63+
if (weight != null) {
64+
props.put("weight", weight.toString());
65+
}
66+
67+
Map<String, Object> randomScore = new HashMap<>();
68+
if (randomScoreSeed != null) {
69+
randomScore.put("seed", randomScoreSeed);
70+
}
71+
if (!StringUtils.isEmpty(randomScoreField)) {
72+
randomScore.put("field", randomScoreField);
73+
}
74+
if (!randomScore.isEmpty()) {
75+
props.put("random_score", JsonUtils.toJson(randomScore));
76+
}
77+
return props;
78+
}
79+
}

0 commit comments

Comments
 (0)