Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatten search params #1238

Closed
wants to merge 1 commit into from
Closed
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
104 changes: 84 additions & 20 deletions sdk-core/src/main/java/io/milvus/param/ParamUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,77 @@ public static ByteString convertPlaceholder(List<?> vectors, PlaceholderType pla
return placeholderGroup.toByteString();
}

// in versions older than milvus v2.5.1, the search parameters are organized as:
// search_params
// {
// "topk"
// "round_decimal"
// "ignore_growing"
// "offset"
// "metric_type"
// "group_by_field"
// "group_size"
// "strict_group_size"
//
// "params": {
// "nprobe"
// "ef"
// "reorder_k"
// "max_empty_result_buckets"
// "drop_ratio_search"
// "radius"
// "range_filter"
// }
// }
// in milvus v2.5.1, the "params" is removed, all the parameters inside "params" are moved into the top level
// search_params
// {
// "topk"
// "round_decimal"
// "ignore_growing"
// "offset"
// "metric_type"
// "group_by_field"
// "group_size"
// "strict_group_size"
//
// "nprobe"
// "ef"
// "reorder_k"
// "max_empty_result_buckets"
// "drop_ratio_search"
// "radius"
// "range_filter"
// }
// the following logic tries to fit the compatibility between v2.5.1 and older versions
public static void compatibleSearchParams(Map<String, Object> searchParams, SearchRequest.Builder builder) {
Map<String, Object> forOldVersionParams = new HashMap<>();
List<String> oldParamKeys = Arrays.asList("nprobe", "ef", "reorder_k", "max_empty_result_buckets", "drop_ratio_search", "radius", "range_filter");
searchParams.forEach((key, value) -> {
// for old versions, these keys are in "params" level
if (oldParamKeys.contains(key)) {
forOldVersionParams.put(key, value);
}

// for new versions, all keys are in the top level
builder.addSearchParams(
KeyValuePair.newBuilder()
.setKey(key)
.setValue(String.valueOf(value))
.build());
});
try {
String params = JsonUtils.toJson(forOldVersionParams);
builder.addSearchParams(
KeyValuePair.newBuilder()
.setKey(Constant.PARAMS)
.setValue(params)
.build());
} catch (IllegalArgumentException e) {
throw new MilvusClientException(ErrorCode.INVALID_PARAMS, e.getMessage() + e.getCause().getMessage());
}
}

@SuppressWarnings("unchecked")
public static SearchRequest convertSearchParam(@NonNull SearchParam requestParam) throws ParamException {
SearchRequest.Builder builder = SearchRequest.newBuilder()
Expand All @@ -784,6 +855,19 @@ public static SearchRequest convertSearchParam(@NonNull SearchParam requestParam
builder.setNq(requestParam.getNQ());

// search parameters
// tries to fit the compatibility between v2.5.1 and older versions
if (null != requestParam.getParams() && !requestParam.getParams().isEmpty()) {
try {
Map<String, Object> paramMap = JsonUtils.fromJson(requestParam.getParams(),
new TypeToken<Map<String, Object>>() {}.getType());
compatibleSearchParams(paramMap, builder);
} catch (IllegalArgumentException e) {
throw new ParamException(e.getMessage() + e.getCause().getMessage());
}
}

// the following parameters are not changed
// just note: if the searchParams already contains the same key, the following parameters will overwrite it
builder.addSearchParams(
KeyValuePair.newBuilder()
.setKey(Constant.VECTOR_FIELD)
Expand Down Expand Up @@ -835,26 +919,6 @@ public static SearchRequest convertSearchParam(@NonNull SearchParam requestParam
}
}

if (null != requestParam.getParams() && !requestParam.getParams().isEmpty()) {
try {
Map<String, Object> paramMap = JsonUtils.fromJson(requestParam.getParams(),
new TypeToken<Map<String, Object>>() {}.getType());
String offset = paramMap.getOrDefault(Constant.OFFSET, 0).toString();
builder.addSearchParams(
KeyValuePair.newBuilder()
.setKey(Constant.OFFSET)
.setValue(offset)
.build());
builder.addSearchParams(
KeyValuePair.newBuilder()
.setKey(Constant.PARAMS)
.setValue(requestParam.getParams())
.build());
} catch (IllegalArgumentException e) {
throw new ParamException(e.getMessage() + e.getCause().getMessage());
}
}

if (!requestParam.getOutFields().isEmpty()) {
requestParam.getOutFields().forEach(builder::addOutputFields);
}
Expand Down
19 changes: 6 additions & 13 deletions sdk-core/src/main/java/io/milvus/v2/utils/VectorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ public SearchRequest ConvertToGrpcSearchRequest(SearchReq request) {
builder.setNq(vectors.size());

// search parameters
// tries to fit the compatibility between v2.5.1 and older versions
Map<String, Object> searchParams = request.getSearchParams();
ParamUtils.compatibleSearchParams(searchParams, builder);

// the following parameters are not changed
// just note: if the searchParams already contains the same key, the following parameters will overwrite it
if (StringUtils.isNotEmpty(request.getAnnsField())) {
builder.addSearchParams(
KeyValuePair.newBuilder()
Expand Down Expand Up @@ -198,19 +204,6 @@ public SearchRequest ConvertToGrpcSearchRequest(SearchReq request) {
.build());
}

if (null != request.getSearchParams()) {
try {
String searchParams = JsonUtils.toJson(request.getSearchParams());
builder.addSearchParams(
KeyValuePair.newBuilder()
.setKey(Constant.PARAMS)
.setValue(searchParams)
.build());
} catch (IllegalArgumentException e) {
throw new MilvusClientException(ErrorCode.INVALID_PARAMS, e.getMessage() + e.getCause().getMessage());
}
}

if (request.getGroupByFieldName() != null && !request.getGroupByFieldName().isEmpty()) {
builder.addSearchParams(
KeyValuePair.newBuilder()
Expand Down
Loading