Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion sdk-core/src/main/java/io/milvus/param/ParamUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,10 @@ public UpsertRequest buildUpsertRequest() {

@SuppressWarnings("unchecked")
public static ByteString convertPlaceholder(List<?> vectors, PlaceholderType placeType) throws ParamException {
return convertPlaceholder(vectors, placeType, false);
}

public static ByteString convertPlaceholder(List<?> vectors, PlaceholderType placeType, boolean elementLevel) throws ParamException {
PlaceholderType plType = PlaceholderType.None;
List<ByteString> byteStrings = new ArrayList<>();
for (Object vector : vectors) {
Expand Down Expand Up @@ -771,7 +775,8 @@ public static ByteString convertPlaceholder(List<?> vectors, PlaceholderType pla

PlaceholderValue.Builder pldBuilder = PlaceholderValue.newBuilder()
.setTag(Constant.VECTOR_TAG)
.setType(plType);
.setType(plType)
.setElementLevel(elementLevel);
byteStrings.forEach(pldBuilder::addValues);

PlaceholderValue plv = pldBuilder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ public static class SearchResult {
private Object id;
private String primaryKey;
private Map<String, HighlightResult> highlightResults;
private Long elementOffset;

private SearchResult(SearchResultBuilder builder) {
this.entity = builder.entity;
this.score = builder.score;
this.id = builder.id;
this.primaryKey = builder.primaryKey;
this.highlightResults = builder.highlightResults == null ? new HashMap<>() : builder.highlightResults;
this.elementOffset = builder.elementOffset;
}

public static SearchResultBuilder builder() {
Expand Down Expand Up @@ -161,10 +163,19 @@ public void addHighlightResult(String fieldName, HighlightResult highlightResult
this.highlightResults.put(fieldName, highlightResult);
}

public Long getElementOffset() {
return elementOffset;
}

public void setElementOffset(Long elementOffset) {
this.elementOffset = elementOffset;
}

@Override
public String toString() {
return "{" + getPrimaryKey() + ": " + getId() + ", Score: " + getScore() + ", OutputFields: " + entity +
(MapUtils.isEmpty(highlightResults) ? "" : (", HighlightResults: " + highlightResults)) + "}";
(MapUtils.isEmpty(highlightResults) ? "" : (", HighlightResults: " + highlightResults)) +
(elementOffset == null ? "" : (", ElementOffset: " + elementOffset)) + "}";
}

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

public SearchResultBuilder entity(Map<String, Object> entity) {
this.entity = entity;
Expand Down Expand Up @@ -205,6 +217,11 @@ public SearchResultBuilder addHighlightResult(String fieldName, HighlightResult
return this;
}

public SearchResultBuilder elementOffset(Long elementOffset) {
this.elementOffset = elementOffset;
return this;
}

public SearchResult build() {
return new SearchResult(this);
}
Expand Down
10 changes: 9 additions & 1 deletion sdk-core/src/main/java/io/milvus/v2/utils/ConvertUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public List<List<SearchResp.SearchResult>> getEntities(SearchResults response) {
.build());
}

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

// set element offset
if (response.getResults().hasElementIndices()) {
LongArray elementIndices = response.getResults().getElementIndices();
for (long j = 0; j < k; j++) {
singleResults.get((int) j).setElementOffset(elementIndices.getData((int) (offset + j)));
}
}

searchResults.add(singleResults);
}
return searchResults;
Expand Down
17 changes: 10 additions & 7 deletions sdk-core/src/main/java/io/milvus/v2/utils/VectorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,16 @@ private static long getGuaranteeTimestamp(ConsistencyLevel consistencyLevel, Str
}
}

private static ByteString convertPlaceholder(List<Object> data, PlaceholderType placeType) {
private static ByteString convertPlaceholder(List<Object> data, PlaceholderType placeType, boolean elementLevel) {
if (placeType == PlaceholderType.VarChar) {
List<ByteString> byteStrings = new ArrayList<>();
for (Object obj : data) {
byteStrings.add(ByteString.copyFrom(((String) obj).getBytes()));
}
PlaceholderValue.Builder pldBuilder = PlaceholderValue.newBuilder()
.setTag(Constant.VECTOR_TAG)
.setType(placeType);
.setType(placeType)
.setElementLevel(elementLevel);
byteStrings.forEach(pldBuilder::addValues);

PlaceholderValue plv = pldBuilder.build();
Expand All @@ -166,14 +167,14 @@ private static ByteString convertPlaceholder(List<Object> data, PlaceholderType
return placeholderGroup.toByteString();
} else {
try {
return ParamUtils.convertPlaceholder(data, placeType);
return ParamUtils.convertPlaceholder(data, placeType, elementLevel);
} catch (ParamException e) {
throw new MilvusClientException(ErrorCode.INVALID_PARAMS, e.getMessage());
}
}
}

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

ByteString byteStr = convertPlaceholder(data, plType);
ByteString byteStr = convertPlaceholder(data, plType, elementLevel);
builder.setPlaceholderGroup(byteStr);
builder.setNq(vectors.size());
} else {
Expand Down Expand Up @@ -246,7 +247,9 @@ public SearchRequest ConvertToGrpcSearchRequest(SearchReq request) {
}

// target vectors or ids
convertSearchTarget(request, builder);
String filter = request.getFilter();
boolean elementLevel = filter != null && filter.contains("element_filter");
convertSearchTarget(request, builder, elementLevel);

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

ByteString byteStr = convertPlaceholder(data, plType);
ByteString byteStr = convertPlaceholder(data, plType, false);
builder.setPlaceholderGroup(byteStr);
builder.setNq(vectors.size());

Expand Down
Loading