Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5a05304
Make knn search a query
mayya-sharipova Aug 22, 2023
5aca3c2
Update docs/changelog/98916.yaml
mayya-sharipova Aug 27, 2023
897266a
Merge remote-tracking branch 'upstream/main' into knn-as-query
mayya-sharipova Aug 27, 2023
8b02234
Correct transport version, remove byteQueryVector
mayya-sharipova Aug 27, 2023
88c3233
Merge remote-tracking branch 'upstream/main' into knn-as-query
mayya-sharipova Aug 29, 2023
49f2b80
Other adjustments
mayya-sharipova Aug 29, 2023
9b1e7c8
Add aliasFilter to the SearchExecutionContext instead of QueryBuilder
mayya-sharipova Aug 29, 2023
cf1c0ae
Remove query_vector_builder
mayya-sharipova Aug 30, 2023
9046812
Add query _name to tests
mayya-sharipova Sep 1, 2023
8a4cb9b
Add filter alias during doToQuery
mayya-sharipova Sep 1, 2023
dccd3c7
Merge remote-tracking branch 'upstream/main' into knn-as-query
mayya-sharipova Sep 1, 2023
d4a5758
Simplify over-wire protocol
mayya-sharipova Sep 1, 2023
00a5d5c
Merge remote-tracking branch 'upstream/main' into knn-as-query
mayya-sharipova Oct 23, 2023
7d2d091
Add nested support for knn query
mayya-sharipova Oct 26, 2023
56b8bbb
Merge remote-tracking branch 'upstream/main' into knn-as-query
mayya-sharipova Oct 26, 2023
b3ca311
Add documentation and other queries
mayya-sharipova Oct 30, 2023
21f40b4
Merge remote-tracking branch 'upstream/main' into knn-as-query
mayya-sharipova Oct 30, 2023
9d825cf
Updates to knn-query documentation
mayya-sharipova Oct 31, 2023
e0ae1dc
Merge remote-tracking branch 'upstream/main' into knn-as-query
mayya-sharipova Oct 31, 2023
348305f
Merge remote-tracking branch 'upstream/main' into knn-as-query
mayya-sharipova Oct 31, 2023
dc0012c
Adjust docs
mayya-sharipova Oct 31, 2023
6876c5a
Merge remote-tracking branch 'upstream/main' into knn-as-query
mayya-sharipova Oct 31, 2023
c4b80ba
Merge branch 'main' into knn-as-query
mayya-sharipova Nov 1, 2023
6450681
Fix an error in docs
mayya-sharipova Nov 1, 2023
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
5 changes: 5 additions & 0 deletions docs/changelog/98916.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 98916
summary: Make knn search a query
area: Vector Search
type: feature
issues: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# test how knn query interacts with filters
setup:
- skip:
version: ' - 8.10.99'
reason: 'knn as query added in 8.11'
features: close_to

- do:
indices.create:
index: my_index
body:
settings:
number_of_shards: 2
mappings:
dynamic: false
properties:
my_vector:
type: dense_vector
dims: 4
index : true
similarity : l2_norm
my_name:
type: keyword
store: true

- do:
bulk:
refresh: true
index: my_index
body:
- '{"index": {"_id": "1"}}'
- '{"my_vector": [1, 1, 1, 1], "my_name": "v1"}'
- '{"index": {"_id": "2"}}'
- '{"my_vector": [1, 1, 1, 2], "my_name": "v2"}'
- '{"index": {"_id": "3"}}'
- '{"my_vector": [1, 1, 1, 3], "my_name": "v1"}'
- '{"index": {"_id": "4"}}'
- '{"my_vector": [1, 1, 1, 4], "my_name": "v2"}'
- '{"index": {"_id": "5"}}'
- '{"my_vector": [1, 1, 1, 5], "my_name": "v1"}'
- '{"index": {"_id": "6"}}'
- '{"my_vector": [1, 1, 1, 6], "my_name": "v2"}'
- '{"index": {"_id": "7"}}'
- '{"my_vector": [1, 1, 1, 7], "my_name": "v1"}'
- '{"index": {"_id": "8"}}'
- '{"my_vector": [1, 1, 1, 8], "my_name": "v2"}'
- '{"index": {"_id": "9"}}'
- '{"my_vector": [1, 1, 1, 9], "my_name": "v1"}'
- '{"index": {"_id": "10"}}'
- '{"my_vector": [1, 1, 1, 10], "my_name": "v2"}'
- '{"index": {"_id": "11"}}'
- '{"my_vector": [1, 1, 1, 11], "my_name": "v1"}'
- '{"index": {"_id": "12"}}'
- '{"my_vector": [1, 1, 1, 12], "my_name": "v2"}'


---
"Search for 2 knn queries combines scores from them":
- do:
search:
index: my_index
body:
size: 6
query:
bool:
should:
- knn:
field: my_vector
query_vector: [ 1, 1, 1, 1 ]
num_candidates: 100
boost: 1.1
- knn:
field: my_vector
query_vector: [ 1, 1, 1, 12 ]
num_candidates: 100

- length: {hits.hits: 6}
- match: {hits.total.value: 12}
- match: {hits.hits.0._id: '1'}
- match: {hits.hits.1._id: '12'}
- match: {hits.hits.2._id: '2'}
- match: { hits.hits.3._id: '11' }
- match: { hits.hits.4._id: '3' }
- match: { hits.hits.5._id: '10' }


---
"Hybrid search combines scores from knn and other queries":
- do:
search:
index: my_index
body:
size: 3
query:
bool:
should:
- wildcard:
my_name:
value: "v*" # produces scores 1.0
- knn:
field: my_vector
query_vector: [ 1, 1, 1, 1 ]
num_candidates: 3

- length: {hits.hits: 3}
- match: {hits.total.value: 12}
- match: {hits.hits.0._id: '1'}
- match: {hits.hits.1._id: '2'}
- match: {hits.hits.2._id: '3'}
- close_to: {hits.hits.0._score: { value: 2.0, error: 0.00001 } }
- close_to: {hits.hits.1._score: { value: 1.5, error: 0.00001 } }
- close_to: {hits.hits.2._score: { value: 1.2, error: 0.00001 } }

# the same query with boosts
- do:
search:
index: my_index
body:
size: 3
query:
bool:
should:
- wildcard:
my_name:
value: "v*" # produces scores 1.0
boost: 100
- knn:
field: my_vector
query_vector: [ 1, 1, 1, 1 ]
num_candidates: 3
boost: 100

- length: { hits.hits: 3 }
- match: { hits.total.value: 12 }
- match: { hits.hits.0._id: '1' }
- match: { hits.hits.1._id: '2' }
- match: { hits.hits.2._id: '3' }
- close_to: { hits.hits.0._score: { value: 200.0, error: 0.00001 } }
- close_to: { hits.hits.1._score: { value: 150.0, error: 0.00001 } }
- close_to: { hits.hits.2._score: { value: 120, error: 0.00001 } }

---
"Aggregations with collected number of docs depends on num_candidates":
- do:
search:
index: my_index
body:
size: 2
query:
knn:
field: my_vector
query_vector: [1, 1, 1, 1]
num_candidates: 100 # collect up to 100 candidates from each shard
aggs:
my_agg:
terms:
field: my_name
order:
_key: asc

- length: {hits.hits: 2}
- match: {hits.total.value: 12}
- match: {aggregations.my_agg.buckets.0.key: 'v1'}
- match: {aggregations.my_agg.buckets.1.key: 'v2'}
- match: {aggregations.my_agg.buckets.0.doc_count: 6}
- match: {aggregations.my_agg.buckets.1.doc_count: 6}

- do:
search:
index: my_index
body:
size: 2
query:
knn:
field: my_vector
query_vector: [ 1, 1, 1, 1 ]
num_candidates: 3 # collect 3 candidates from each shard
aggs:
my_agg2:
terms:
field: my_name
order:
_key: asc
my_sum_buckets:
sum_bucket:
buckets_path: "my_agg2>_count"

- length: { hits.hits: 2 }
- match: { hits.total.value: 6 }
- match: { aggregations.my_agg2.buckets.0.key: 'v1' }
- match: { aggregations.my_agg2.buckets.1.key: 'v2' }
- match: { aggregations.my_sum_buckets.value: 6.0 }
Original file line number Diff line number Diff line change
Expand Up @@ -294,23 +294,6 @@ setup:
- match: { error.root_cause.0.reason: "failed to create query: field [nonexistent] does not exist in the mapping" }

---
"Direct kNN queries are disallowed":
- skip:
version: ' - 8.3.99'
reason: 'error message changed in 8.4'
- do:
catch: bad_request
search:
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" }
- match: { error.root_cause.0.reason: "[knn] queries cannot be provided directly, use the [knn] body parameter instead" }
---
"KNN Vector similarity search only":
- skip:
version: ' - 8.7.99'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,6 @@ setup:
- match: { error.root_cause.0.reason: "failed to create query: field [nonexistent] does not exist in the mapping" }

---
"Direct kNN queries are disallowed":
- do:
catch: bad_request
search:
index: test
body:
query:
knn:
field: vector
query_vector: [ -1, 0, 1, 2, 3 ]
num_candidates: 1
- match: { error.root_cause.0.type: "illegal_argument_exception" }
- match: { error.root_cause.0.reason: "[knn] queries cannot be provided directly, use the [knn] body parameter instead" }
---
"Vector similarity search only":
- skip:
version: ' - 8.7.99'
Expand Down
Loading