-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance: Support hybrid search multiple vector fields (#663)
See also: milvus-io/milvus#25639 milvus pr: milvus-io/milvus#29433 --------- Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
- Loading branch information
Showing
6 changed files
with
360 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb" | ||
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb" | ||
"github.com/milvus-io/milvus-sdk-go/v2/entity" | ||
) | ||
|
||
type ANNSearchRequest struct { | ||
fieldName string | ||
vectors []entity.Vector | ||
metricType entity.MetricType | ||
expr string | ||
searchParam entity.SearchParam | ||
options []SearchQueryOptionFunc | ||
limit int | ||
} | ||
|
||
func NewANNSearchRequest(fieldName string, metricsType entity.MetricType, vectors []entity.Vector, searchParam entity.SearchParam, limit int, options ...SearchQueryOptionFunc) *ANNSearchRequest { | ||
return &ANNSearchRequest{ | ||
fieldName: fieldName, | ||
vectors: vectors, | ||
metricType: metricsType, | ||
searchParam: searchParam, | ||
limit: limit, | ||
options: options, | ||
} | ||
} | ||
func (r *ANNSearchRequest) WithExpr(expr string) *ANNSearchRequest { | ||
r.expr = expr | ||
return r | ||
} | ||
|
||
func (r *ANNSearchRequest) getMilvusSearchRequest(collectionInfo *collInfo) (*milvuspb.SearchRequest, error) { | ||
opt := &SearchQueryOption{ | ||
ConsistencyLevel: collectionInfo.ConsistencyLevel, // default | ||
} | ||
for _, o := range r.options { | ||
o(opt) | ||
} | ||
params := r.searchParam.Params() | ||
params[forTuningKey] = opt.ForTuning | ||
bs, err := json.Marshal(params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
searchParams := entity.MapKvPairs(map[string]string{ | ||
"anns_field": r.fieldName, | ||
"topk": fmt.Sprintf("%d", r.limit), | ||
"params": string(bs), | ||
"metric_type": string(r.metricType), | ||
"round_decimal": "-1", | ||
ignoreGrowingKey: strconv.FormatBool(opt.IgnoreGrowing), | ||
offsetKey: fmt.Sprintf("%d", opt.Offset), | ||
groupByKey: opt.GroupByField, | ||
}) | ||
|
||
result := &milvuspb.SearchRequest{ | ||
DbName: "", | ||
Dsl: r.expr, | ||
PlaceholderGroup: vector2PlaceholderGroupBytes(r.vectors), | ||
DslType: commonpb.DslType_BoolExprV1, | ||
SearchParams: searchParams, | ||
GuaranteeTimestamp: opt.GuaranteeTimestamp, | ||
Nq: int64(len(r.vectors)), | ||
} | ||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb" | ||
) | ||
|
||
const ( | ||
rerankType = "strategy" | ||
rerankParams = "params" | ||
rffParam = "k" | ||
weightedParam = "weights" | ||
|
||
rrfRerankType = `rrf` | ||
weightedRerankType = `weighted` | ||
) | ||
|
||
type Reranker interface { | ||
GetParams() []*commonpb.KeyValuePair | ||
} | ||
|
||
type rrfReranker struct { | ||
K float64 `json:"k,omitempty"` | ||
} | ||
|
||
func (r *rrfReranker) WithK(k float64) *rrfReranker { | ||
r.K = k | ||
return r | ||
} | ||
|
||
func (r *rrfReranker) GetParams() []*commonpb.KeyValuePair { | ||
bs, _ := json.Marshal(r) | ||
|
||
return []*commonpb.KeyValuePair{ | ||
{Key: rerankType, Value: rrfRerankType}, | ||
{Key: rerankParams, Value: string(bs)}, | ||
} | ||
} | ||
|
||
func NewRRFReranker() *rrfReranker { | ||
return &rrfReranker{K: 60} | ||
} | ||
|
||
type weightedReranker struct { | ||
Weights []float64 `json:"weights,omitempty"` | ||
} | ||
|
||
func (r *weightedReranker) GetParams() []*commonpb.KeyValuePair { | ||
bs, _ := json.Marshal(r) | ||
|
||
return []*commonpb.KeyValuePair{ | ||
{Key: rerankType, Value: rrfRerankType}, | ||
{Key: rerankParams, Value: string(bs)}, | ||
} | ||
} | ||
|
||
func NewWeightedReranker(weights []float64) *weightedReranker { | ||
return &weightedReranker{ | ||
Weights: weights, | ||
} | ||
} |
Oops, something went wrong.