Skip to content

Commit 388b1f8

Browse files
Address Julie's feedback
-decodeVectorMagnitude go back to Buffer -correct documentation to use doc access methods for vector values and magnitude instead of get functions - rename getVectorMagnitude to getMagnitude - remove unnecessary yml tests
1 parent d9dfdb1 commit 388b1f8

File tree

6 files changed

+22
-78
lines changed

6 files changed

+22
-78
lines changed

docs/reference/vectors/vector-functions.asciidoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ linearly scanned. Thus, expect the query time grow linearly
88
with the number of matched documents. For this reason, we recommend
99
to limit the number of matched documents with a `query` parameter.
1010

11-
This is the list of available vector functions:
11+
This is the list of available vector functions and vector access methods:
1212

1313
1. `cosineSimilarity` – calculates cosine similarity
1414
2. `dotProduct` – calculates dot product
1515
3. `l1norm` – calculates L^1^ distance
1616
4. `l2norm` - calculates L^2^ distance
17-
5. `getVectorValue` – returns a vector's value as an array of floats
18-
6. `getVectorMagnitude` – returns a vector's magnitude
17+
5. `doc[<field>].vectorValue` – returns a vector's value as an array of floats
18+
6. `doc[<field>].magnitude` – returns a vector's magnitude
1919

2020

2121
Let's create an index with a `dense_vector` mapping and index a couple
@@ -210,10 +210,10 @@ The recommended way to access dense vectors is through `cosineSimilarity`,
210210
`dotProduct`, `l1norm` or `l2norm` functions. But for custom use cases,
211211
you can access dense vectors's values directly through the following functions:
212212

213-
- `float[] getVectorValue()` – returns a vector's value as an array of floats
213+
- `doc[<field>].vectorValue` – returns a vector's value as an array of floats
214214

215-
- `float getVectorMagnitude()` – returns a vector's magnitude (available for
216-
vectors created in the version 7.5 or later).
215+
- `doc[<field>].magnitude` – returns a vector's magnitude as a float
216+
(available for vectors created in the version 7.5 or later).
217217

218218
For example, the script below implements a cosine similarity using these
219219
two functions:
@@ -235,8 +235,8 @@ GET my-index-000001/_search
235235
},
236236
"script": {
237237
"source": """
238-
float[] v = doc['my_dense_vector'].getVectorValue();
239-
float vm = doc['my_dense_vector'].getVectorMagnitude();
238+
float[] v = doc['my_dense_vector'].vectorValue;
239+
float vm = doc['my_dense_vector'].magnitude;
240240
float dotProduct = 0;
241241
for (int i = 0; i < v.length; i++) {
242242
dotProduct += v[i] * params.queryVector[i];

x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/vectors/20_dense_vector_special_cases.yml

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -189,55 +189,3 @@ setup:
189189
- match: {hits.hits.0._id: "1"}
190190
- match: {hits.hits.1._id: "2"}
191191
- match: {hits.hits.1._score: 0.0}
192-
193-
---
194-
"No sort, no aggs, no docvalue_fields are allowed":
195-
- do:
196-
index:
197-
refresh: true
198-
index: test-index
199-
id: 1
200-
body:
201-
my_dense_vector: [10, 10, 10]
202-
203-
# sorting on dense_vector field is not supported
204-
- do:
205-
catch: bad_request
206-
search:
207-
index: test-index
208-
body:
209-
query:
210-
match_all: {}
211-
sort:
212-
my_dense_vector
213-
214-
- match: { status: 400 }
215-
- match: { error.root_cause.0.reason: "Field [my_dense_vector] of type [dense_vector] doesn't support sort" }
216-
217-
# aggs on dense_vector field are not supported
218-
- do:
219-
catch: bad_request
220-
search:
221-
index: test-index
222-
body:
223-
aggs:
224-
my_agg:
225-
terms:
226-
field: my_dense_vector
227-
228-
- match: { status: 400 }
229-
- match: { error.root_cause.0.reason: "Field [my_dense_vector] of type [dense_vector] doesn't support docvalue_fields or aggregations" }
230-
231-
232-
# docvalue_fields of dense_vector field are not supported
233-
- do:
234-
catch: bad_request
235-
search:
236-
index: test-index
237-
body:
238-
query:
239-
match_all: {}
240-
docvalue_fields: ["my_dense_vector"]
241-
242-
- match: { status: 400 }
243-
- match: { error.root_cause.0.reason: "Field [my_dense_vector] of type [dense_vector] doesn't support docvalue_fields or aggregations" }

x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/vectors/30_dense_vector_script_access.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
script:
3939
source: |
4040
float s = 0;
41-
for (def el : doc['v'].getVectorValue()) {
41+
for (def el : doc['v'].vectorValue) {
4242
s += el;
4343
}
4444
s;
@@ -51,7 +51,7 @@
5151
- match: { hits.hits.2._score: 3 }
5252

5353

54-
# check getVectorMagnitude() API
54+
# check getMagnitude() API
5555
- do:
5656
headers:
5757
Content-Type: application/json
@@ -61,7 +61,7 @@
6161
script_score:
6262
query: { "exists" : { "field" : "v" } }
6363
script:
64-
source: "doc['v'].getVectorMagnitude()"
64+
source: "doc['v'].magnitude"
6565

6666
- match: { hits.hits.0._id: "3" }
6767
- gte: {hits.hits.0._score: 3.3166}
@@ -82,7 +82,7 @@
8282
script_score:
8383
query: { match_all: { } }
8484
script:
85-
source: "doc['v'].getVectorValue()[0]"
85+
source: "doc['v'].vectorValue[0]"
8686

8787
- match: { status: 400 }
8888
- match: { error.root_cause.0.type: "script_exception" }
@@ -96,7 +96,7 @@
9696
script_score:
9797
query: { match_all: { } }
9898
script:
99-
source: "doc['v'].getVectorMagnitude()"
99+
source: "doc['v'].magnitude"
100100

101101
- match: { status: 400 }
102102
- match: { error.root_cause.0.type: "script_exception" }
@@ -113,8 +113,8 @@
113113
query: { "exists": { "field": "v" } }
114114
script:
115115
source: |
116-
float[] v = doc['v'].getVectorValue();
117-
float vm = doc['v'].getVectorMagnitude();
116+
float[] v = doc['v'].vectorValue;
117+
float vm = doc['v'].magnitude;
118118
119119
int closestPv = 0;
120120
float maxCosSim = -1;

x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/VectorEncoderDecoder.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,11 @@ public static int denseVectorLength(Version indexVersion, BytesRef vectorBR) {
3232
*/
3333
public static float decodeVectorMagnitude(Version indexVersion, BytesRef vectorBR) {
3434
assert indexVersion.onOrAfter(Version.V_7_5_0);
35-
int offset = vectorBR.offset + vectorBR.length - INT_BYTES;
36-
int intValue = ((vectorBR.bytes[offset] & 0xFF) << 24) |
37-
((vectorBR.bytes[offset+1] & 0xFF) << 16) |
38-
((vectorBR.bytes[offset+2] & 0xFF) << 8) |
39-
(vectorBR.bytes[offset+3] & 0xFF);
40-
return Float.intBitsToFloat(intValue);
35+
ByteBuffer byteBuffer = ByteBuffer.wrap(vectorBR.bytes, vectorBR.offset, vectorBR.length);
36+
return byteBuffer.getFloat(vectorBR.offset + vectorBR.length - 4);
4137
}
4238

43-
public static float getVectorMagnitude(Version indexVersion, BytesRef vectorBR) {
39+
public static float getMagnitude(Version indexVersion, BytesRef vectorBR) {
4440
if (vectorBR == null) {
4541
throw new IllegalArgumentException("A document doesn't have a value for a vector field!");
4642
}

x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/DenseVectorScriptDocValues.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ BytesRef getEncodedValue() {
4646
@Override
4747
public BytesRef get(int index) {
4848
throw new UnsupportedOperationException("accessing a vector field's value through 'get' or 'value' is not supported!" +
49-
"Use 'getVectorValue' or 'getVectorMagnitude' instead!'");
49+
"Use 'vectorValue' or 'magnitude' instead!'");
5050
}
5151

5252
/**
@@ -60,8 +60,8 @@ public float[] getVectorValue() {
6060
/**
6161
* Get dense vector's magnitude
6262
*/
63-
public float getVectorMagnitude() {
64-
return VectorEncoderDecoder.getVectorMagnitude(indexVersion, value);
63+
public float getMagnitude() {
64+
return VectorEncoderDecoder.getMagnitude(indexVersion, value);
6565
}
6666

6767
@Override

x-pack/plugin/vectors/src/main/resources/org/elasticsearch/xpack/vectors/query/whitelist.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77
class org.elasticsearch.xpack.vectors.query.DenseVectorScriptDocValues {
88
float[] getVectorValue()
9-
float getVectorMagnitude()
9+
float getMagnitude()
1010
}
1111
class org.elasticsearch.script.ScoreScript @no_import {
1212
}

0 commit comments

Comments
 (0)