Skip to content

Commit

Permalink
enhance: support hints param
Browse files Browse the repository at this point in the history
Signed-off-by: chasingegg <[email protected]>
  • Loading branch information
chasingegg committed Dec 13, 2024
1 parent f29741b commit e91e378
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions sdk-core/src/main/java/io/milvus/param/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class Constant {
public static final String GROUP_BY_FIELD = "group_by_field";
public static final String GROUP_SIZE = "group_size";
public static final String STRICT_GROUP_SIZE = "strict_group_size";
public static final String HINTS = "hints";

public static final String INDEX_TYPE = "index_type";
public static final String METRIC_TYPE = "metric_type";
Expand Down
15 changes: 15 additions & 0 deletions sdk-core/src/main/java/io/milvus/param/ParamUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,14 @@ public static SearchRequest convertSearchParam(@NonNull SearchParam requestParam
.build());
}

if (!StringUtils.isEmpty(requestParam.getHints())) {
builder.addSearchParams(
KeyValuePair.newBuilder()
.setKey(Constant.HINTS)
.setValue(requestParam.getHints())
.build());
}

if (!StringUtils.isEmpty(requestParam.getGroupByFieldName())) {
builder.addSearchParams(
KeyValuePair.newBuilder()
Expand Down Expand Up @@ -904,6 +912,13 @@ public static SearchRequest convertAnnSearchParam(@NonNull AnnSearchParam annSea
.setValue(annSearchParam.getMetricType())
.build());
}
if (annSearchParam.getHints() != null && !annSearchParam.getHints().isEmpty()) {
builder.addSearchParams(
KeyValuePair.newBuilder()
.setKey(Constant.HINTS)
.setValue(annSearchParam.getHints())
.build());
}

// params
String params = "{}";
Expand Down
14 changes: 14 additions & 0 deletions sdk-core/src/main/java/io/milvus/param/dml/AnnSearchParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class AnnSearchParam {
private final List<?> vectors;
private final Long NQ;
private final String params;
private final String hints;
private final PlaceholderType plType;

private AnnSearchParam(@NonNull Builder builder) {
Expand All @@ -55,6 +56,7 @@ private AnnSearchParam(@NonNull Builder builder) {
this.vectors = builder.vectors;
this.NQ = builder.NQ;
this.params = builder.params;
this.hints = builder.hints;
this.plType = builder.plType;
}

Expand All @@ -73,6 +75,7 @@ public static class Builder {
private List<?> vectors;
private Long NQ;
private String params = "{}";
private String hints;

// plType is used to distinct vector type
// for Float16Vector/BFloat16Vector and BinaryVector, user inputs ByteBuffer
Expand Down Expand Up @@ -207,6 +210,17 @@ public Builder withParams(@NonNull String params) {
return this;
}

/**
* Hints provide some optimizations for better search performance.
*
* @param hints hints field name
* @return <code>Builder</code>
*/
public Builder withHints(@NonNull String hints) {
this.hints = hints;
return this;
}


/**
* Verifies parameters and creates a new {@link AnnSearchParam} instance.
Expand Down
14 changes: 14 additions & 0 deletions sdk-core/src/main/java/io/milvus/param/dml/SearchParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class SearchParam {
private final String groupByFieldName;
private final Integer groupSize;
private final Boolean strictGroupSize;
private final String hints;
private final PlaceholderType plType;

private SearchParam(@NonNull Builder builder) {
Expand All @@ -81,6 +82,7 @@ private SearchParam(@NonNull Builder builder) {
this.groupByFieldName = builder.groupByFieldName;
this.groupSize = builder.groupSize;
this.strictGroupSize = builder.strictGroupSize;
this.hints = builder.hints;
this.plType = builder.plType;
}

Expand Down Expand Up @@ -112,6 +114,7 @@ public static class Builder {
private String groupByFieldName;
private Integer groupSize = null;
private Boolean strictGroupSize = null;
private String hints;

// plType is used to distinct vector type
// for Float16Vector/BFloat16Vector and BinaryVector, user inputs ByteBuffer
Expand Down Expand Up @@ -369,6 +372,17 @@ public Builder withIgnoreGrowing(@NonNull Boolean ignoreGrowing) {
return this;
}

/**
* Hints provide some optimizations for better search performance.
*
* @param hints field name
* @return <code>Builder</code>
*/
public Builder withHints(@NonNull String hints) {
this.hints = hints;
return this;
}

/**
* Sets field name to do grouping.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class AnnSearchReq {
private String expr = "";
private List<BaseVector> vectors;
private String params;
private String hints;

@Builder.Default
private IndexParam.MetricType metricType = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class SearchReq {
private String groupByFieldName;
private Integer groupSize;
private Boolean strictGroupSize;
private String hints;

// Expression template, to improve expression parsing performance in complicated list
// Assume user has a filter = "pk > 3 and city in ["beijing", "shanghai", ......]
Expand Down
15 changes: 15 additions & 0 deletions sdk-core/src/main/java/io/milvus/v2/utils/VectorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ public SearchRequest ConvertToGrpcSearchRequest(SearchReq request) {
}
}

if (request.getHints() != null && !request.getHints().isEmpty()) {
builder.addSearchParams(
KeyValuePair.newBuilder()
.setKey(Constant.HINTS)
.setValue(request.getHints())
.build());
}

if (request.getGroupByFieldName() != null && !request.getGroupByFieldName().isEmpty()) {
builder.addSearchParams(
KeyValuePair.newBuilder()
Expand Down Expand Up @@ -405,6 +413,13 @@ public static SearchRequest convertAnnSearchParam(@NonNull AnnSearchReq annSearc
.setValue(annSearchReq.getMetricType().name())
.build());
}
if (annSearchReq.getHints() != null && !annSearchReq.getHints().isEmpty()) {
builder.addSearchParams(
KeyValuePair.newBuilder()
.setKey(Constant.HINTS)
.setValue(annSearchReq.getHints())
.build());
}

// params
String params = "{}";
Expand Down

0 comments on commit e91e378

Please sign in to comment.