-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add new kNN search endpoint #79013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add new kNN search endpoint #79013
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1fb6806
Add new kNN search endpoint
jtibshirani 7fb097d
Merge remote-tracking branch 'upstream/master' into pr
jtibshirani 489bfb1
Small fixes in response to feedback
jtibshirani 205a6c6
Make sure _source can be passed as a boolean.
jtibshirani 0739a26
Rename REST spec name and num_cands
jtibshirani 1f40a8d
Revert to using knn_search for REST spec name
jtibshirani a1818fa
Merge remote-tracking branch 'upstream/master' into knn-endpoint
jtibshirani 7c6c883
Address more comments
jtibshirani 74227c4
Make sure we can parse strings and arrays for _source
jtibshirani dfb5335
Simplify tests now that we are more restrictive
jtibshirani a36d7e9
Merge remote-tracking branch 'upstream/master' into knn-endpoint
jtibshirani ce420a2
Merge remote-tracking branch 'upstream/master' into knn-endpoint
jtibshirani a0c2bdf
Ensure the endpoint respects field aliases
jtibshirani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
rest-api-spec/src/main/resources/rest-api-spec/api/knn_search.json
This file contains hidden or 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,47 @@ | ||
| { | ||
| "knn_search":{ | ||
| "documentation":{ | ||
| "url":"https://www.elastic.co/guide/en/elasticsearch/reference/master/search-search.html", | ||
| "description":"Performs a kNN search." | ||
| }, | ||
| "stability":"experimental", | ||
| "visibility":"public", | ||
| "headers":{ | ||
| "accept": [ "application/json"], | ||
| "content_type": ["application/json"] | ||
| }, | ||
| "url":{ | ||
| "paths":[ | ||
| { | ||
| "path":"/_knn_search", | ||
| "methods":[ | ||
| "GET", | ||
| "POST" | ||
| ] | ||
| }, | ||
| { | ||
| "path":"/{index}/_knn_search", | ||
| "methods":[ | ||
| "GET", | ||
| "POST" | ||
| ], | ||
| "parts":{ | ||
| "index":{ | ||
| "type":"list", | ||
| "description":"A comma-separated list of index names to search; use `_all` or empty string to perform the operation on all indices" | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| }, | ||
| "params": { | ||
| "routing":{ | ||
| "type":"list", | ||
| "description":"A comma-separated list of specific routing values" | ||
| } | ||
| }, | ||
| "body":{ | ||
| "description":"The search definition" | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
90 changes: 90 additions & 0 deletions
90
x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/vectors/40_knn_search.yml
This file contains hidden or 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,90 @@ | ||
| setup: | ||
| - do: | ||
| indices.create: | ||
| index: test | ||
| body: | ||
| settings: | ||
| number_of_replicas: 0 | ||
| mappings: | ||
| properties: | ||
| name: | ||
| type: keyword | ||
| vector: | ||
| type: dense_vector | ||
| dims: 5 | ||
| index: true | ||
| similarity: l2_norm | ||
| - do: | ||
| index: | ||
| index: test | ||
| body: | ||
| name: cow.jpg | ||
| vector: [230.0, 300.33, -34.8988, 15.555, -200.0] | ||
|
|
||
| - do: | ||
| index: | ||
| index: test | ||
| id: 2 | ||
| body: | ||
| name: moose.jpg | ||
| vector: [-0.5, 100.0, -13, 14.8, -156.0] | ||
|
|
||
| - do: | ||
| index: | ||
| index: test | ||
| id: 3 | ||
| body: | ||
| name: rabbit.jpg | ||
| vector: [0.5, 111.3, -13.0, 14.8, -156.0] | ||
|
|
||
| - do: | ||
| indices.refresh: {} | ||
|
|
||
| --- | ||
| "Basic kNN search": | ||
| - do: | ||
| knn_search: | ||
| index: test | ||
| body: | ||
| fields: [ "name" ] | ||
| knn: | ||
| field: vector | ||
| query_vector: [-0.5, 90.0, -10, 14.8, -156.0] | ||
| k: 2 | ||
| num_candidates: 3 | ||
|
|
||
| - match: {hits.hits.0._id: "2"} | ||
| - match: {hits.hits.0.fields.name.0: "moose.jpg"} | ||
|
|
||
| - match: {hits.hits.1._id: "3"} | ||
| - match: {hits.hits.1.fields.name.0: "rabbit.jpg"} | ||
|
|
||
| --- | ||
| "Basic kNN search with no index": | ||
| - do: | ||
| knn_search: | ||
| body: | ||
| fields: [ "name" ] | ||
| knn: | ||
| field: vector | ||
| query_vector: [-0.5, 90.0, -10, 14.8, -156.0] | ||
| k: 1 | ||
| num_candidates: 1 | ||
|
|
||
| - match: {hits.hits.0._id: "2"} | ||
| - match: {hits.hits.0.fields.name.0: "moose.jpg"} | ||
|
|
||
| --- | ||
| "Direct knn queries are disallowed": | ||
| - do: | ||
| catch: bad_request | ||
| search: | ||
| rest_total_hits_as_int: true | ||
| index: test-index | ||
| body: | ||
| query: | ||
| knn: | ||
| field: vector | ||
| query_vector: [ -0.5, 90.0, -10, 14.8, -156.0 ] | ||
| num_candidates: 1 | ||
| - match: { error.root_cause.0.type: "illegal_argument_exception" } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it needed ? _search allows that for on-boarding but we can be more strict here imo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I can remove this route.