Skip to content

Commit

Permalink
Integrate ANN into _search endpoint (#88694)
Browse files Browse the repository at this point in the history
This PR adds a new `knn` option to the `_search` API to support ANN search.
It's powered by the same Lucene ANN capabilities as the old `_knn_search`
endpoint. The `knn` option can be combined with other search features like
queries and aggregations.

Addresses #87625
  • Loading branch information
jtibshirani committed Jul 22, 2022
1 parent 3cb759b commit e3ede67
Show file tree
Hide file tree
Showing 38 changed files with 1,997 additions and 350 deletions.
14 changes: 14 additions & 0 deletions docs/changelog/88694.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pr: 88694
summary: Integrate ANN into `_search` endpoint
area: Vector Search
type: feature
issues:
- 87625
highlight:
title: Integrate ANN into `_search` endpoint
body: |-
This change adds a `knn` option to the `_search` API to support ANN
search. It's powered by the same Lucene ANN capabilities as the old
`_knn_search` endpoint. The `knn` option can be combined with other
search features like queries and aggregations.
notable: true
1 change: 1 addition & 0 deletions docs/reference/mapping/types/dense-vector.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ NOTE: Although they are conceptually related, the `similarity` parameter is
different from <<text,`text`>> field <<similarity,`similarity`>> and accepts
a distinct set of options.

[[dense-vector-index-options]]
`index_options`::
(Optional, object)
An optional section that configures the kNN indexing algorithm. The HNSW
Expand Down
107 changes: 2 additions & 105 deletions docs/reference/search/knn-search.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ that sacrifices result accuracy for improved search speed. This means the
results returned are not always the true _k_ closest neighbors.
//end::hnsw-algorithm[]

The kNN search API supports <<knn-search-api-filter-example, restricting the search using a filter>>.
The search will return the top `k` documents that also match the filter query.
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.

[[knn-search-api-path-params]]
==== {api-path-parms-title}
Expand Down Expand Up @@ -152,106 +152,3 @@ the similarity between the query and document vector. See
* 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.

[[knn-search-api-example]]
==== {api-examples-title}

===== Basic kNN search

The following requests create a `dense_vector` field with indexing enabled and
add sample documents:

[source,console]
----
PUT my-index
{
"mappings": {
"properties": {
"image_vector": {
"type": "dense_vector",
"dims": 3,
"index": true,
"similarity": "l2_norm"
},
"name": {
"type": "keyword"
},
"file_type": {
"type": "keyword"
}
}
}
}
PUT my-index/_doc/1?refresh
{
"image_vector" : [0.5, 0.1, 2.6],
"name": "moose family",
"file_type": "jpeg"
}
PUT my-index/_doc/2?refresh
{
"image_vector" : [1.0, 0.8, -0.2],
"name": "alpine lake",
"file_type": "svg"
}
----

[[knn-search-api-filter-example]]
===== Filtered kNN search

The next request performs a kNN search filtered by the `file_type` field:

[source,console]
----
GET my-index/_knn_search
{
"knn": {
"field": "image_vector",
"query_vector": [0.3, 0.1, 1.2],
"k": 5,
"num_candidates": 50
},
"filter": {
"term": {
"file_type": "svg"
}
},
"_source": ["name"]
}
----
// TEST[continued]

[source,console-result]
----
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.2538071,
"hits": [
{
"_index": "my-index",
"_id": "2",
"_score": 0.2538071,
"_source": {
"name": "alpine lake"
}
}
]
}
}
----
// TESTRESPONSE[s/"took": 5/"took": $body.took/]
// TESTRESPONSE[s/,\n \.\.\.//]

0 comments on commit e3ede67

Please sign in to comment.