Skip to content

Commit

Permalink
Script: Fields API for Dense Vector (#83550)
Browse files Browse the repository at this point in the history
Adds the fields API for `dense_vector` field mapper.

Adds a `DenseVector` interface for the value type.

Implemented by:
 * `KnnDenseVector` which wraps a decoded float array from `VectorValues`
 * `BinaryDenseVector` which lazily decodes a `BytesRef` from `BinaryDocValues`

The vector operations have moved into those implements from `BinaryDenseVectorScriptDocValues.java` and  `KnnDenseVectorScriptDocValues.java`, respectively.

The `DenseVector` API is:
```
float getMagnitude();
double dotProduct(float[] | List);
double l1Norm(float[] | List);
double l2Norm(float[] | List);
float[] getVector();
int dims();

boolean isEmpty(); // does the value exist
int size();        // 0 if isEmpty(), 1 otherwise
Iterator<Float> iterator()
```

`dotProduct`, `l1Norm` and `l2Norm` take a `float[]` or a `List` via the
a delegating `default` method on the `DenseVector` interface.

The `DenseVectorDocValuesField` abstract class contains two getter APIS.
It is implemented by  `KnnDenseVectorDocValuesField` and
`BinaryDenseVectorDocValuesField`.

```
DenseVector get()
DenseVector get(DenseVector defaultValue)
```

The `get()` method is included because there isn't a good default dense vector,
so that API returns an empty `DenseVector` which throws an
`IllegalArgumentException` for all method calls other than `isEmpty()`,
`size()` and `iterator()`.

The empty dense vector will always be `DenseVector.EMPTY` in case users want
to use equality checks.

Refs: #79105
  • Loading branch information
stu-elastic committed Feb 16, 2022
1 parent e7ca532 commit b44fcfb
Show file tree
Hide file tree
Showing 19 changed files with 1,883 additions and 390 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/83550.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 83550
summary: "Script: Fields API for Dense Vector"
area: Infra/Scripting
type: enhancement
issues: []

0 comments on commit b44fcfb

Please sign in to comment.