-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add access to dense_vector values (#71847)
Allow direct access to a dense_vector' values in script through the following functions: - getVectorValue – returns a vector's value as an array of floats - getMagnitude – returns a vector's magnitude Closes #51964 Backport for #71313
- Loading branch information
1 parent
d190d58
commit d53e83c
Showing
23 changed files
with
436 additions
and
139 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
65 changes: 65 additions & 0 deletions
65
...n/src/yamlRestTest/resources/rest-api-spec/test/vectors/30_dense_vector_script_access.yml
This file contains 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,65 @@ | ||
--- | ||
"Access to values of dense_vector in script": | ||
- skip: | ||
version: " - 7.12.99" | ||
reason: "Access to values of dense_vector in script was added in 7.13" | ||
- do: | ||
indices.create: | ||
index: test-index | ||
body: | ||
mappings: | ||
properties: | ||
v: | ||
type: dense_vector | ||
dims: 3 | ||
|
||
- do: | ||
bulk: | ||
index: test-index | ||
refresh: true | ||
body: | ||
- '{"index": {"_id": "1"}}' | ||
- '{"v": [1, 1, 1]}' | ||
- '{"index": {"_id": "2"}}' | ||
- '{"v": [1, 1, 2]}' | ||
- '{"index": {"_id": "3"}}' | ||
- '{"v": [1, 1, 3]}' | ||
- '{"index": {"_id": "missing_vector"}}' | ||
- '{}' | ||
|
||
# vector functions in loop – return the index of the closest parameter vector based on cosine similarity | ||
- do: | ||
search: | ||
body: | ||
query: | ||
script_score: | ||
query: { "exists": { "field": "v" } } | ||
script: | ||
source: | | ||
float[] v = doc['v'].vectorValue; | ||
float vm = doc['v'].magnitude; | ||
int closestPv = 0; | ||
float maxCosSim = -1; | ||
for (int i = 0; i < params.pvs.length; i++) { | ||
float dotProduct = 0; | ||
for (int j = 0; j < v.length; j++) { | ||
dotProduct += v[j] * params.pvs[i][j]; | ||
} | ||
float cosSim = dotProduct / (vm * (float) params.pvs_magnts[i]); | ||
if (maxCosSim < cosSim) { | ||
maxCosSim = cosSim; | ||
closestPv = i; | ||
} | ||
} | ||
closestPv; | ||
params: | ||
pvs: [ [ 1, 1, 1 ], [ 1, 1, 2 ], [ 1, 1, 3 ] ] | ||
pvs_magnts: [1.7320, 2.4495, 3.3166] | ||
|
||
- match: { hits.hits.0._id: "3" } | ||
- match: { hits.hits.0._score: 2 } | ||
- match: { hits.hits.1._id: "2" } | ||
- match: { hits.hits.1._score: 1 } | ||
- match: { hits.hits.2._id: "1" } | ||
- match: { hits.hits.2._score: 0 } |
This file contains 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 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 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.