Skip to content

Commit aaa42d6

Browse files
committed
Support highlighter
Signed-off-by: yhmo <yihua.mo@zilliz.com>
1 parent 303d8fe commit aaa42d6

File tree

9 files changed

+544
-14
lines changed

9 files changed

+544
-14
lines changed

sdk-core/src/main/java/io/milvus/response/SearchResultsWrapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public long getNumQueries() {
253253
return results.getNumQueries();
254254
}
255255

256-
private static final class Position {
256+
public static final class Position {
257257
private final long offset;
258258
private final long k;
259259

@@ -271,7 +271,7 @@ public long getK() {
271271
}
272272
}
273273

274-
private Position getOffsetByIndex(int indexOfTarget) {
274+
public Position getOffsetByIndex(int indexOfTarget) {
275275
List<Long> kList = results.getTopksList();
276276

277277
// if the server didn't return separate topK, use same topK value "0"

sdk-core/src/main/java/io/milvus/v2/service/vector/request/SearchReq.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.milvus.v2.common.IndexParam;
2424
import io.milvus.v2.service.collection.request.CreateCollectionReq;
2525
import io.milvus.v2.service.vector.request.data.BaseVector;
26+
import io.milvus.v2.service.vector.request.highlighter.Highlighter;
2627

2728
import java.util.ArrayList;
2829
import java.util.HashMap;
@@ -69,6 +70,9 @@ public class SearchReq {
6970
// Boolean, Long, Double, String, List<Boolean>, List<Long>, List<Double>, List<String>
7071
private Map<String, Object> filterTemplateValues;
7172

73+
// milvus v2.6.9 supports highlighter for search results
74+
private Highlighter highlighter;
75+
7276
private SearchReq(SearchReqBuilder builder) {
7377
this.databaseName = builder.databaseName;
7478
this.collectionName = builder.collectionName;
@@ -95,6 +99,7 @@ private SearchReq(SearchReqBuilder builder) {
9599
this.functionScore = builder.functionScore;
96100
this.filterTemplateValues = builder.filterTemplateValues;
97101
this.timezone = builder.timezone;
102+
this.highlighter = builder.highlighter;
98103
}
99104

100105
// Getters and Setters
@@ -294,6 +299,10 @@ public void setFilterTemplateValues(Map<String, Object> filterTemplateValues) {
294299
this.filterTemplateValues = filterTemplateValues;
295300
}
296301

302+
public Highlighter getHighlighter() {
303+
return highlighter;
304+
}
305+
297306
@Override
298307
public String toString() {
299308
return "SearchReq{" +
@@ -319,6 +328,7 @@ public String toString() {
319328
", groupSize=" + groupSize +
320329
", strictGroupSize=" + strictGroupSize +
321330
", ranker=" + ranker +
331+
", highlighter=" + (highlighter == null ? "null" : (highlighter.highlightType() + ":" + highlighter.getParams())) +
322332
", functionScore=" + functionScore +
323333
// ", filterTemplateValues=" + filterTemplateValues +
324334
'}';
@@ -354,6 +364,7 @@ public static class SearchReqBuilder {
354364
private CreateCollectionReq.Function ranker;
355365
private FunctionScore functionScore;
356366
private Map<String, Object> filterTemplateValues = new HashMap<>(); // default value
367+
private Highlighter highlighter;
357368

358369
private SearchReqBuilder() {
359370
}
@@ -487,6 +498,11 @@ public SearchReqBuilder filterTemplateValues(Map<String, Object> filterTemplateV
487498
return this;
488499
}
489500

501+
public SearchReqBuilder highlighter(Highlighter highlighter) {
502+
this.highlighter = highlighter;
503+
return this;
504+
}
505+
490506
public SearchReq build() {
491507
return new SearchReq(this);
492508
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.highlighter;
21+
22+
import java.util.Map;
23+
24+
public interface Highlighter {
25+
String highlightType();
26+
27+
Map<String, String> getParams();
28+
}
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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.highlighter;
21+
22+
import io.milvus.common.utils.JsonUtils;
23+
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
import java.util.Map;
27+
28+
public class LexicalHighlighter implements Highlighter {
29+
private final List<HighlightQuery> highlightQuires;
30+
private final Boolean highlightSearchText;
31+
private final List<String> preTags;
32+
private final List<String> postTags;
33+
private final Integer fragmentOffset;
34+
private final Integer fragmentSize;
35+
private final Integer numOfFragments;
36+
37+
public LexicalHighlighter(LexicalHighlighterBuilder builder) {
38+
this.highlightQuires = builder.highlightQuires;
39+
this.highlightSearchText = builder.highlightSearchText;
40+
this.preTags = builder.preTags;
41+
this.postTags = builder.postTags;
42+
this.fragmentOffset = builder.fragmentOffset;
43+
this.fragmentSize = builder.fragmentSize;
44+
this.numOfFragments = builder.numOfFragments;
45+
}
46+
47+
@Override
48+
public String highlightType() {
49+
return "Lexical";
50+
}
51+
52+
@Override
53+
public Map<String, String> getParams() {
54+
Map<String, String> params = new java.util.HashMap<>();
55+
if (this.highlightQuires != null) {
56+
// serialize the list of HighlightQuery to a JSON array string using Gson
57+
params.put("highlight_queries", JsonUtils.toJson(this.highlightQuires));
58+
}
59+
if (this.highlightSearchText != null) {
60+
params.put("highlight_search_text", this.highlightSearchText.toString());
61+
}
62+
if (this.preTags != null) {
63+
params.put("pre_tags", JsonUtils.toJson(this.preTags));
64+
}
65+
if (this.postTags != null) {
66+
params.put("post_tags", JsonUtils.toJson(this.postTags));
67+
}
68+
if (this.fragmentOffset != null) {
69+
params.put("fragment_offset", this.fragmentOffset.toString());
70+
}
71+
if (this.fragmentSize != null) {
72+
params.put("fragment_size", this.fragmentSize.toString());
73+
}
74+
if (this.numOfFragments != null) {
75+
params.put("num_of_fragments", this.numOfFragments.toString());
76+
}
77+
return params;
78+
}
79+
80+
public static class HighlightQuery {
81+
public String type;
82+
public String field;
83+
public String text;
84+
85+
public HighlightQuery(String type, String field, String query) {
86+
this.type = type;
87+
this.field = field;
88+
this.text = query;
89+
}
90+
91+
@Override
92+
public String toString() {
93+
return JsonUtils.toJson(this);
94+
}
95+
}
96+
97+
public static class LexicalHighlighterBuilder {
98+
private List<HighlightQuery> highlightQuires;
99+
private Boolean highlightSearchText;
100+
private List<String> preTags;
101+
private List<String> postTags;
102+
private Integer fragmentOffset;
103+
private Integer fragmentSize;
104+
private Integer numOfFragments;
105+
106+
public LexicalHighlighterBuilder() {
107+
}
108+
109+
public LexicalHighlighterBuilder highlightQueries(List<HighlightQuery> queries) {
110+
this.highlightQuires = queries;
111+
return this;
112+
}
113+
114+
public LexicalHighlighterBuilder addHighlightQuery(HighlightQuery q) {
115+
if (this.highlightQuires == null) this.highlightQuires = new ArrayList<>();
116+
this.highlightQuires.add(q);
117+
return this;
118+
}
119+
120+
public LexicalHighlighterBuilder highlightSearchText(Boolean highlightSearchText) {
121+
this.highlightSearchText = highlightSearchText;
122+
return this;
123+
}
124+
125+
public LexicalHighlighterBuilder preTags(List<String> preTags) {
126+
this.preTags = preTags;
127+
return this;
128+
}
129+
130+
public LexicalHighlighterBuilder addPreTag(String tag) {
131+
if (this.preTags == null) this.preTags = new ArrayList<>();
132+
this.preTags.add(tag);
133+
return this;
134+
}
135+
136+
public LexicalHighlighterBuilder postTags(List<String> postTags) {
137+
this.postTags = postTags;
138+
return this;
139+
}
140+
141+
public LexicalHighlighterBuilder addPostTag(String tag) {
142+
if (this.postTags == null) this.postTags = new ArrayList<>();
143+
this.postTags.add(tag);
144+
return this;
145+
}
146+
147+
public LexicalHighlighterBuilder fragmentOffset(Integer offset) {
148+
this.fragmentOffset = offset;
149+
return this;
150+
}
151+
152+
public LexicalHighlighterBuilder fragmentSize(Integer size) {
153+
this.fragmentSize = size;
154+
return this;
155+
}
156+
157+
public LexicalHighlighterBuilder numOfFragments(Integer num) {
158+
this.numOfFragments = num;
159+
return this;
160+
}
161+
162+
public LexicalHighlighter build() {
163+
return new LexicalHighlighter(this);
164+
}
165+
}
166+
167+
public static LexicalHighlighterBuilder builder() {
168+
return new LexicalHighlighterBuilder();
169+
}
170+
}

0 commit comments

Comments
 (0)