Skip to content

Commit bc25b51

Browse files
committed
Support element filter search for struct field
Signed-off-by: yhmo <yihua.mo@zilliz.com>
1 parent eb35bc6 commit bc25b51

File tree

5 files changed

+44
-11
lines changed

5 files changed

+44
-11
lines changed

sdk-core/src/main/java/io/milvus/param/ParamUtils.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,10 @@ public UpsertRequest buildUpsertRequest() {
727727

728728
@SuppressWarnings("unchecked")
729729
public static ByteString convertPlaceholder(List<?> vectors, PlaceholderType placeType) throws ParamException {
730+
return convertPlaceholder(vectors, placeType, false);
731+
}
732+
733+
public static ByteString convertPlaceholder(List<?> vectors, PlaceholderType placeType, boolean elementLevel) throws ParamException {
730734
PlaceholderType plType = PlaceholderType.None;
731735
List<ByteString> byteStrings = new ArrayList<>();
732736
for (Object vector : vectors) {
@@ -771,7 +775,8 @@ public static ByteString convertPlaceholder(List<?> vectors, PlaceholderType pla
771775

772776
PlaceholderValue.Builder pldBuilder = PlaceholderValue.newBuilder()
773777
.setTag(Constant.VECTOR_TAG)
774-
.setType(plType);
778+
.setType(plType)
779+
.setElementLevel(elementLevel);
775780
byteStrings.forEach(pldBuilder::addValues);
776781

777782
PlaceholderValue plv = pldBuilder.build();

sdk-core/src/main/java/io/milvus/v2/service/vector/response/SearchResp.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,15 @@ public static class SearchResult {
103103
private Object id;
104104
private String primaryKey;
105105
private Map<String, HighlightResult> highlightResults;
106+
private Long elementOffset;
106107

107108
private SearchResult(SearchResultBuilder builder) {
108109
this.entity = builder.entity;
109110
this.score = builder.score;
110111
this.id = builder.id;
111112
this.primaryKey = builder.primaryKey;
112113
this.highlightResults = builder.highlightResults == null ? new HashMap<>() : builder.highlightResults;
114+
this.elementOffset = builder.elementOffset;
113115
}
114116

115117
public static SearchResultBuilder builder() {
@@ -161,10 +163,19 @@ public void addHighlightResult(String fieldName, HighlightResult highlightResult
161163
this.highlightResults.put(fieldName, highlightResult);
162164
}
163165

166+
public Long getElementOffset() {
167+
return elementOffset;
168+
}
169+
170+
public void setElementOffset(Long elementOffset) {
171+
this.elementOffset = elementOffset;
172+
}
173+
164174
@Override
165175
public String toString() {
166176
return "{" + getPrimaryKey() + ": " + getId() + ", Score: " + getScore() + ", OutputFields: " + entity +
167-
(MapUtils.isEmpty(highlightResults) ? "" : (", HighlightResults: " + highlightResults)) + "}";
177+
(MapUtils.isEmpty(highlightResults) ? "" : (", HighlightResults: " + highlightResults)) +
178+
(elementOffset == null ? "" : (", ElementOffset: " + elementOffset)) + "}";
168179
}
169180

170181
public static class SearchResultBuilder {
@@ -173,6 +184,7 @@ public static class SearchResultBuilder {
173184
private Object id;
174185
private String primaryKey = "id";
175186
private Map<String, HighlightResult> highlightResults = new HashMap<>();
187+
private Long elementOffset;
176188

177189
public SearchResultBuilder entity(Map<String, Object> entity) {
178190
this.entity = entity;
@@ -205,6 +217,11 @@ public SearchResultBuilder addHighlightResult(String fieldName, HighlightResult
205217
return this;
206218
}
207219

220+
public SearchResultBuilder elementOffset(Long elementOffset) {
221+
this.elementOffset = elementOffset;
222+
return this;
223+
}
224+
208225
public SearchResult build() {
209226
return new SearchResult(this);
210227
}

sdk-core/src/main/java/io/milvus/v2/utils/ConvertUtils.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public List<List<SearchResp.SearchResult>> getEntities(SearchResults response) {
106106
.build());
107107
}
108108

109-
// set highlight
109+
// set highlight and element offset
110110
SearchResultsWrapper.Position position = searchResultsWrapper.getOffsetByIndex(i);
111111
long offset = position.getOffset();
112112
long k = position.getK();
@@ -127,6 +127,14 @@ public List<List<SearchResp.SearchResult>> getEntities(SearchResults response) {
127127
}
128128
}
129129

130+
// set element offset
131+
if (response.getResults().hasElementIndices()) {
132+
LongArray elementIndices = response.getResults().getElementIndices();
133+
for (long j = 0; j < k; j++) {
134+
singleResults.get((int) j).setElementOffset(elementIndices.getData((int) (offset + j)));
135+
}
136+
}
137+
130138
searchResults.add(singleResults);
131139
}
132140
return searchResults;

sdk-core/src/main/java/io/milvus/v2/utils/VectorUtils.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,16 @@ private static long getGuaranteeTimestamp(ConsistencyLevel consistencyLevel, Str
148148
}
149149
}
150150

151-
private static ByteString convertPlaceholder(List<Object> data, PlaceholderType placeType) {
151+
private static ByteString convertPlaceholder(List<Object> data, PlaceholderType placeType, boolean elementLevel) {
152152
if (placeType == PlaceholderType.VarChar) {
153153
List<ByteString> byteStrings = new ArrayList<>();
154154
for (Object obj : data) {
155155
byteStrings.add(ByteString.copyFrom(((String) obj).getBytes()));
156156
}
157157
PlaceholderValue.Builder pldBuilder = PlaceholderValue.newBuilder()
158158
.setTag(Constant.VECTOR_TAG)
159-
.setType(placeType);
159+
.setType(placeType)
160+
.setElementLevel(elementLevel);
160161
byteStrings.forEach(pldBuilder::addValues);
161162

162163
PlaceholderValue plv = pldBuilder.build();
@@ -166,14 +167,14 @@ private static ByteString convertPlaceholder(List<Object> data, PlaceholderType
166167
return placeholderGroup.toByteString();
167168
} else {
168169
try {
169-
return ParamUtils.convertPlaceholder(data, placeType);
170+
return ParamUtils.convertPlaceholder(data, placeType, elementLevel);
170171
} catch (ParamException e) {
171172
throw new MilvusClientException(ErrorCode.INVALID_PARAMS, e.getMessage());
172173
}
173174
}
174175
}
175176

176-
private static void convertSearchTarget(SearchReq request, SearchRequest.Builder builder) {
177+
private static void convertSearchTarget(SearchReq request, SearchRequest.Builder builder, boolean elementLevel) {
177178
// prepare target, the input could be:
178179
// 1. vectors or string list for doc-in-doc-out
179180
// 2. ids list for search by primary keys
@@ -200,7 +201,7 @@ private static void convertSearchTarget(SearchReq request, SearchRequest.Builder
200201
data.add(vector.getData());
201202
}
202203

203-
ByteString byteStr = convertPlaceholder(data, plType);
204+
ByteString byteStr = convertPlaceholder(data, plType, elementLevel);
204205
builder.setPlaceholderGroup(byteStr);
205206
builder.setNq(vectors.size());
206207
} else {
@@ -246,7 +247,9 @@ public SearchRequest ConvertToGrpcSearchRequest(SearchReq request) {
246247
}
247248

248249
// target vectors or ids
249-
convertSearchTarget(request, builder);
250+
String filter = request.getFilter();
251+
boolean elementLevel = filter != null && filter.contains("element_filter");
252+
convertSearchTarget(request, builder, elementLevel);
250253

251254
// search parameters
252255
// tries to fit the compatibility between v2.5.1 and older versions
@@ -515,7 +518,7 @@ public static SearchRequest convertAnnSearchParam(AnnSearchReq annSearchReq,
515518
data.add(vector.getData());
516519
}
517520

518-
ByteString byteStr = convertPlaceholder(data, plType);
521+
ByteString byteStr = convertPlaceholder(data, plType, false);
519522
builder.setPlaceholderGroup(byteStr);
520523
builder.setNq(vectors.size());
521524

0 commit comments

Comments
 (0)