Documentation
¶
Overview ¶
Run a knn search.
NOTE: The kNN search API has been replaced by the `knn` option in the search API.
Perform a k-nearest neighbor (kNN) search on a dense_vector field and return the matching documents. Given a query vector, the API finds the k closest vectors and returns those documents as search hits.
Elasticsearch uses the HNSW algorithm to support efficient kNN search. Like most kNN algorithms, HNSW is an approximate method that sacrifices result accuracy for improved search speed. This means the results returned are not always the true k closest neighbors.
The kNN search API supports restricting the search using a filter. The search will return the top k documents that also match the filter query.
A kNN search response has the exact same structure as a search API response. However, certain sections have a meaning specific to kNN search:
* The document `_score` is determined by the similarity between the query and document vector. * The `hits.total` object contains the total number of nearest neighbor candidates considered, which is `num_candidates * num_shards`. The `hits.total.relation` will always be `eq`, indicating an exact value.
Index ¶
- Variables
- type KnnSearch
- func (r KnnSearch) Do(providedCtx context.Context) (*Response, error)
- func (r *KnnSearch) DocvalueFields(docvaluefields ...types.FieldAndFormat) *KnnSearch
- func (r *KnnSearch) ErrorTrace(errortrace bool) *KnnSearch
- func (r *KnnSearch) Fields(fields ...string) *KnnSearch
- func (r *KnnSearch) Filter(filters ...types.Query) *KnnSearch
- func (r *KnnSearch) FilterPath(filterpaths ...string) *KnnSearch
- func (r *KnnSearch) Header(key, value string) *KnnSearch
- func (r *KnnSearch) HttpRequest(ctx context.Context) (*http.Request, error)
- func (r *KnnSearch) Human(human bool) *KnnSearch
- func (r *KnnSearch) Knn(knn *types.CoreKnnQuery) *KnnSearch
- func (r KnnSearch) Perform(providedCtx context.Context) (*http.Response, error)
- func (r *KnnSearch) Pretty(pretty bool) *KnnSearch
- func (r *KnnSearch) Raw(raw io.Reader) *KnnSearch
- func (r *KnnSearch) Request(req *Request) *KnnSearch
- func (r *KnnSearch) Routing(routing string) *KnnSearch
- func (r *KnnSearch) Source_(sourceconfig types.SourceConfig) *KnnSearch
- func (r *KnnSearch) StoredFields(fields ...string) *KnnSearch
- type NewKnnSearch
- type Request
- type Response
Constants ¶
This section is empty.
Variables ¶
var ErrBuildPath = errors.New("cannot build path, check for missing path parameters")
ErrBuildPath is returned in case of missing parameters within the build of the request.
Functions ¶
This section is empty.
Types ¶
type KnnSearch ¶
type KnnSearch struct {
// contains filtered or unexported fields
}
func New ¶
func New(tp elastictransport.Interface) *KnnSearch
Run a knn search.
NOTE: The kNN search API has been replaced by the `knn` option in the search API.
Perform a k-nearest neighbor (kNN) search on a dense_vector field and return the matching documents. Given a query vector, the API finds the k closest vectors and returns those documents as search hits.
Elasticsearch uses the HNSW algorithm to support efficient kNN search. Like most kNN algorithms, HNSW is an approximate method that sacrifices result accuracy for improved search speed. This means the results returned are not always the true k closest neighbors.
The kNN search API supports restricting the search using a filter. The search will return the top k documents that also match the filter query.
A kNN search response has the exact same structure as a search API response. However, certain sections have a meaning specific to kNN search:
* The document `_score` is determined by the similarity between the query and document vector. * The `hits.total` object contains the total number of nearest neighbor candidates considered, which is `num_candidates * num_shards`. The `hits.total.relation` will always be `eq`, indicating an exact value.
https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search-api.html
func (KnnSearch) Do ¶
Do runs the request through the transport, handle the response and returns a knnsearch.Response
func (*KnnSearch) DocvalueFields ¶ added in v8.9.0
func (r *KnnSearch) DocvalueFields(docvaluefields ...types.FieldAndFormat) *KnnSearch
DocvalueFields The request returns doc values for field names matching these patterns in the `hits.fields` property of the response. It accepts wildcard (`*`) patterns. API name: docvalue_fields
func (*KnnSearch) ErrorTrace ¶ added in v8.14.0
ErrorTrace When set to `true` Elasticsearch will include the full stack trace of errors when they occur. API name: error_trace
func (*KnnSearch) Fields ¶ added in v8.9.0
Fields The request returns values for field names matching these patterns in the `hits.fields` property of the response. It accepts wildcard (`*`) patterns. API name: fields
func (*KnnSearch) Filter ¶ added in v8.9.0
Filter A query to filter the documents that can match. The kNN search will return the top `k` documents that also match this filter. The value can be a single query or a list of queries. If `filter` isn't provided, all documents are allowed to match. API name: filter
func (*KnnSearch) FilterPath ¶ added in v8.14.0
FilterPath Comma-separated list of filters in dot notation which reduce the response returned by Elasticsearch. API name: filter_path
func (*KnnSearch) HttpRequest ¶
HttpRequest returns the http.Request object built from the given parameters.
func (*KnnSearch) Human ¶ added in v8.14.0
Human When set to `true` will return statistics in a format suitable for humans. For example `"exists_time": "1h"` for humans and `"eixsts_time_in_millis": 3600000` for computers. When disabled the human readable values will be omitted. This makes sense for responses being consumed only by machines. API name: human
func (*KnnSearch) Knn ¶ added in v8.9.0
func (r *KnnSearch) Knn(knn *types.CoreKnnQuery) *KnnSearch
Knn The kNN query to run. API name: knn
func (KnnSearch) Perform ¶ added in v8.7.0
Perform runs the http.Request through the provided transport and returns an http.Response.
func (*KnnSearch) Pretty ¶ added in v8.14.0
Pretty If set to `true` the returned JSON will be "pretty-formatted". Only use this option for debugging only. API name: pretty
func (*KnnSearch) Raw ¶
Raw takes a json payload as input which is then passed to the http.Request If specified Raw takes precedence on Request method.
func (*KnnSearch) Request ¶
Request allows to set the request property with the appropriate payload.
func (*KnnSearch) Routing ¶
Routing A comma-separated list of specific routing values. API name: routing
func (*KnnSearch) Source_ ¶ added in v8.9.0
func (r *KnnSearch) Source_(sourceconfig types.SourceConfig) *KnnSearch
Source_ Indicates which source fields are returned for matching documents. These fields are returned in the `hits._source` property of the search response. API name: _source
func (*KnnSearch) StoredFields ¶ added in v8.9.0
StoredFields A list of stored fields to return as part of a hit. If no fields are specified, no stored fields are included in the response. If this field is specified, the `_source` parameter defaults to `false`. You can pass `_source: true` to return both source fields and stored fields in the search response. API name: stored_fields
type NewKnnSearch ¶
NewKnnSearch type alias for index.
func NewKnnSearchFunc ¶
func NewKnnSearchFunc(tp elastictransport.Interface) NewKnnSearch
NewKnnSearchFunc returns a new instance of KnnSearch with the provided transport. Used in the index of the library this allows to retrieve every apis in once place.
type Request ¶
type Request struct { // DocvalueFields The request returns doc values for field names matching these patterns // in the `hits.fields` property of the response. // It accepts wildcard (`*`) patterns. DocvalueFields []types.FieldAndFormat `json:"docvalue_fields,omitempty"` // Fields The request returns values for field names matching these patterns // in the `hits.fields` property of the response. // It accepts wildcard (`*`) patterns. Fields []string `json:"fields,omitempty"` // Filter A query to filter the documents that can match. The kNN search will return // the top // `k` documents that also match this filter. The value can be a single query or // a // list of queries. If `filter` isn't provided, all documents are allowed to // match. Filter []types.Query `json:"filter,omitempty"` // Knn The kNN query to run. Knn types.CoreKnnQuery `json:"knn"` // Source_ Indicates which source fields are returned for matching documents. These // fields are returned in the `hits._source` property of the search response. Source_ types.SourceConfig `json:"_source,omitempty"` // StoredFields A list of stored fields to return as part of a hit. If no fields are // specified, // no stored fields are included in the response. If this field is specified, // the `_source` // parameter defaults to `false`. You can pass `_source: true` to return both // source fields // and stored fields in the search response. StoredFields []string `json:"stored_fields,omitempty"` }
Request holds the request body struct for the package knnsearch
func (*Request) FromJSON ¶ added in v8.5.0
FromJSON allows to load an arbitrary json into the request structure
func (*Request) UnmarshalJSON ¶ added in v8.12.1
type Response ¶ added in v8.7.0
type Response struct { // Fields The field values for the documents. These fields // must be specified in the request using the `fields` parameter. Fields map[string]json.RawMessage `json:"fields,omitempty"` // Hits The returned documents and metadata. Hits types.HitsMetadata `json:"hits"` // MaxScore The highest returned document score. This value is null for requests // that do not sort by score. MaxScore *types.Float64 `json:"max_score,omitempty"` // Shards_ A count of shards used for the request. Shards_ types.ShardStatistics `json:"_shards"` // TimedOut If true, the request timed out before completion; // returned results may be partial or empty. TimedOut bool `json:"timed_out"` // Took The milliseconds it took Elasticsearch to run the request. Took int64 `json:"took"` }
Response holds the response body struct for the package knnsearch