knnsearch

package
v8.19.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 31, 2025 License: Apache-2.0 Imports: 12 Imported by: 5

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

Constants

This section is empty.

Variables

View Source
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

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

func (r KnnSearch) Do(providedCtx context.Context) (*Response, error)

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

func (r *KnnSearch) ErrorTrace(errortrace bool) *KnnSearch

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

func (r *KnnSearch) Fields(fields ...string) *KnnSearch

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

func (r *KnnSearch) Filter(filters ...types.Query) *KnnSearch

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

func (r *KnnSearch) FilterPath(filterpaths ...string) *KnnSearch

FilterPath Comma-separated list of filters in dot notation which reduce the response returned by Elasticsearch. API name: filter_path

func (*KnnSearch) Header

func (r *KnnSearch) Header(key, value string) *KnnSearch

Header set a key, value pair in the KnnSearch headers map.

func (*KnnSearch) HttpRequest

func (r *KnnSearch) HttpRequest(ctx context.Context) (*http.Request, error)

HttpRequest returns the http.Request object built from the given parameters.

func (*KnnSearch) Human added in v8.14.0

func (r *KnnSearch) Human(human bool) *KnnSearch

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

func (r KnnSearch) Perform(providedCtx context.Context) (*http.Response, error)

Perform runs the http.Request through the provided transport and returns an http.Response.

func (*KnnSearch) Pretty added in v8.14.0

func (r *KnnSearch) Pretty(pretty bool) *KnnSearch

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

func (r *KnnSearch) Raw(raw io.Reader) *KnnSearch

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

func (r *KnnSearch) Request(req *Request) *KnnSearch

Request allows to set the request property with the appropriate payload.

func (*KnnSearch) Routing

func (r *KnnSearch) Routing(routing string) *KnnSearch

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

func (r *KnnSearch) StoredFields(fields ...string) *KnnSearch

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

type NewKnnSearch func(index string) *KnnSearch

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

https://github.com/elastic/elasticsearch-specification/blob/470b4b9aaaa25cae633ec690e54b725c6fc939c7/specification/_global/knn_search/KnnSearchRequest.ts#L26-L112

func NewRequest added in v8.5.0

func NewRequest() *Request

NewRequest returns a Request

func (*Request) FromJSON added in v8.5.0

func (r *Request) FromJSON(data string) (*Request, error)

FromJSON allows to load an arbitrary json into the request structure

func (*Request) UnmarshalJSON added in v8.12.1

func (s *Request) UnmarshalJSON(data []byte) error

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

https://github.com/elastic/elasticsearch-specification/blob/470b4b9aaaa25cae633ec690e54b725c6fc939c7/specification/_global/knn_search/KnnSearchResponse.ts#L26-L54

func NewResponse added in v8.7.0

func NewResponse() *Response

NewResponse returns a Response

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL