-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Distance measures for dense and sparse vectors #37947
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
mayya-sharipova
merged 11 commits into
elastic:master
from
mayya-sharipova:vector-fied-query
Feb 20, 2019
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1f033c5
Distance measures for dense and sparse vectors
mayya-sharipova 7075c03
Address Adrien's comments
mayya-sharipova 0d4517f
Merge branch 'master' into vector-fied-query
mayya-sharipova 3535e48
Removes typed calls from YAML REST tests
mayya-sharipova ac0205c
Address Adrien's comments 2
mayya-sharipova 608a1fb
Merge branch 'master' into vector-fied-query
mayya-sharipova e00f7d5
Correct check style
mayya-sharipova f5a8ec4
Correct doc links to internal docs links
mayya-sharipova f15c510
Separate docvalues for dense and sparse vectors
mayya-sharipova 302b171
Merge remote-tracking branch 'upstream/master' into vector-fied-query
mayya-sharipova 16412f8
Add unit tests for script vector functions
mayya-sharipova 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
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
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
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
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 |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
import org.apache.lucene.util.InPlaceMergeSorter; | ||
|
||
// static utility functions for encoding and decoding dense_vector and sparse_vector fields | ||
final class VectorEncoderDecoder { | ||
public final class VectorEncoderDecoder { | ||
static final byte INT_BYTES = 4; | ||
static final byte SHORT_BYTES = 2; | ||
|
||
|
@@ -34,10 +34,11 @@ private VectorEncoderDecoder() { } | |
* BytesRef: int[] floats encoded as integers values, 2 bytes for each dimension | ||
* @param values - values of the sparse array | ||
* @param dims - dims of the sparse array | ||
* @param dimCount - number of the dimension | ||
* @param dimCount - number of the dimensions, necessary as values and dims are dynamically created arrays, | ||
* and may be over-allocated | ||
* @return BytesRef | ||
*/ | ||
static BytesRef encodeSparseVector(int[] dims, float[] values, int dimCount) { | ||
public static BytesRef encodeSparseVector(int[] dims, float[] values, int dimCount) { | ||
// 1. Sort dims and values | ||
sortSparseDimsValues(dims, values, dimCount); | ||
byte[] buf = new byte[dimCount * (INT_BYTES + SHORT_BYTES)]; | ||
|
@@ -66,9 +67,12 @@ static BytesRef encodeSparseVector(int[] dims, float[] values, int dimCount) { | |
|
||
/** | ||
* Decodes the first part of BytesRef into sparse vector dimensions | ||
* @param vectorBR - vector decoded in BytesRef | ||
* @param vectorBR - sparse vector encoded in BytesRef | ||
*/ | ||
static int[] decodeSparseVectorDims(BytesRef vectorBR) { | ||
public static int[] decodeSparseVectorDims(BytesRef vectorBR) { | ||
if (vectorBR == null) { | ||
throw new IllegalArgumentException("A document doesn't have a value for a vector field!"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be an illegal argument exception? |
||
int dimCount = vectorBR.length / (INT_BYTES + SHORT_BYTES); | ||
int[] dims = new int[dimCount]; | ||
int offset = vectorBR.offset; | ||
|
@@ -81,9 +85,12 @@ static int[] decodeSparseVectorDims(BytesRef vectorBR) { | |
|
||
/** | ||
* Decodes the second part of the BytesRef into sparse vector values | ||
* @param vectorBR - vector decoded in BytesRef | ||
* @param vectorBR - sparse vector encoded in BytesRef | ||
*/ | ||
static float[] decodeSparseVector(BytesRef vectorBR) { | ||
public static float[] decodeSparseVector(BytesRef vectorBR) { | ||
if (vectorBR == null) { | ||
throw new IllegalArgumentException("A document doesn't have a value for a vector field!"); | ||
} | ||
int dimCount = vectorBR.length / (INT_BYTES + SHORT_BYTES); | ||
int offset = vectorBR.offset + SHORT_BYTES * dimCount; //calculate the offset from where values are encoded | ||
float[] vector = new float[dimCount]; | ||
|
@@ -100,10 +107,14 @@ static float[] decodeSparseVector(BytesRef vectorBR) { | |
|
||
|
||
/** | ||
Sort dimensions in the ascending order and | ||
sort values in the same order as their corresponding dimensions | ||
**/ | ||
static void sortSparseDimsValues(int[] dims, float[] values, int n) { | ||
* Sorts dimensions in the ascending order and | ||
* sorts values in the same order as their corresponding dimensions | ||
* | ||
* @param dims - dimensions of the sparse query vector | ||
* @param values - values for the sparse query vector | ||
* @param n - number of dimensions | ||
*/ | ||
public static void sortSparseDimsValues(int[] dims, float[] values, int n) { | ||
new InPlaceMergeSorter() { | ||
@Override | ||
public int compare(int i, int j) { | ||
|
@@ -123,8 +134,42 @@ public void swap(int i, int j) { | |
}.sort(0, n); | ||
} | ||
|
||
// Decodes a BytesRef into an array of floats | ||
static float[] decodeDenseVector(BytesRef vectorBR) { | ||
/** | ||
* Sorts dimensions in the ascending order and | ||
* sorts values in the same order as their corresponding dimensions | ||
* | ||
* @param dims - dimensions of the sparse query vector | ||
* @param values - values for the sparse query vector | ||
* @param n - number of dimensions | ||
*/ | ||
public static void sortSparseDimsDoubleValues(int[] dims, double[] values, int n) { | ||
new InPlaceMergeSorter() { | ||
@Override | ||
public int compare(int i, int j) { | ||
return Integer.compare(dims[i], dims[j]); | ||
} | ||
|
||
@Override | ||
public void swap(int i, int j) { | ||
int tempDim = dims[i]; | ||
dims[i] = dims[j]; | ||
dims[j] = tempDim; | ||
|
||
double tempValue = values[j]; | ||
values[j] = values[i]; | ||
values[i] = tempValue; | ||
} | ||
}.sort(0, n); | ||
} | ||
|
||
/** | ||
* Decodes a BytesRef into an array of floats | ||
* @param vectorBR - dense vector encoded in BytesRef | ||
*/ | ||
public static float[] decodeDenseVector(BytesRef vectorBR) { | ||
if (vectorBR == null) { | ||
throw new IllegalArgumentException("A document doesn't have a value for a vector field!"); | ||
} | ||
int dimCount = vectorBR.length / INT_BYTES; | ||
float[] vector = new float[dimCount]; | ||
int offset = vectorBR.offset; | ||
|
42 changes: 42 additions & 0 deletions
42
...apper-extras/src/main/java/org/elasticsearch/index/query/DocValuesWhitelistExtension.java
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,42 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.index.query; | ||
|
||
|
||
import org.elasticsearch.painless.spi.PainlessExtension; | ||
import org.elasticsearch.painless.spi.Whitelist; | ||
import org.elasticsearch.painless.spi.WhitelistLoader; | ||
import org.elasticsearch.script.ScoreScript; | ||
import org.elasticsearch.script.ScriptContext; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class DocValuesWhitelistExtension implements PainlessExtension { | ||
|
||
private static final Whitelist WHITELIST = | ||
WhitelistLoader.loadFromResourceFiles(DocValuesWhitelistExtension.class, "docvalues_whitelist.txt"); | ||
|
||
@Override | ||
public Map<ScriptContext<?>, List<Whitelist>> getContextWhitelists() { | ||
return Collections.singletonMap(ScoreScript.CONTEXT, Collections.singletonList(WHITELIST)); | ||
} | ||
} |
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.
hi @mayya-sharipova I've been doing some digging to figure out why fielddataBuilder looks like makes the dense vector field aggregatable, while docValueFormat says
throw new UnsupportedOperationException("Field [" + name() + "] of type [" + typeName() + "] doesn't support docvalue_fields or aggregations");
. Is that correct or does docValueFormat need updating? I am totally fine with fixing if needed, just trying to put all the pieces together first. ;)