Skip to content
This repository was archived by the owner on Jan 19, 2021. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public final class VectorScoreScript extends ScoreScript {

private BinaryDocValues binaryEmbeddingReader;

private final String field;
private final boolean cosine;

Expand All @@ -25,19 +25,23 @@ public final class VectorScoreScript extends ScoreScript {

@Override
public double execute() {
if (binaryEmbeddingReader == null) {
return 0f;
}

try {
final byte[] bytes = binaryEmbeddingReader.binaryValue().bytes;
final ByteArrayDataInput input = new ByteArrayDataInput(bytes);

input.readVInt(); // returns the number of values which should be 1, MUST appear hear since it affect the next calls

final int len = input.readVInt();
// in case vector is of different size
if (len != inputVector.length * Float.BYTES) {
// Failing in order not to hide potential bugs
throw new IllegalArgumentException("Input vector & indexed vector don't have the same dimensions");
}

float score = 0;
if (cosine) {
float docVectorNorm = 0.0f;
Expand All @@ -64,7 +68,7 @@ public double execute() {
throw new UncheckedIOException(e); // again - Failing in order not to hide potential bugs
}
}

@Override
public void setDocument(int docId) {
try {
Expand All @@ -73,7 +77,7 @@ public void setDocument(int docId) {
throw new UncheckedIOException(e);
}
}

@SuppressWarnings("unchecked")
public VectorScoreScript(Map<String, Object> params, SearchLookup lookup, LeafReaderContext leafContext) {
super(params, lookup, leafContext);
Expand Down Expand Up @@ -115,23 +119,19 @@ public VectorScoreScript(Map<String, Object> params, SearchLookup lookup, LeafRe
} else {
this.magnitude = 0.0f;
}

try {
this.binaryEmbeddingReader = leafContext.reader().getBinaryDocValues(this.field);
if(this.binaryEmbeddingReader == null) {
throw new IllegalStateException();
}
} catch (Exception e) {
throw new IllegalStateException("binaryEmbeddingReader can't be null, is '" + this.field +
"' the right binary vector field name, if so, is it defined as a binary type in the index mapping?");
this.binaryEmbeddingReader = null;
}

}

public static class VectorScoreScriptFactory implements LeafFactory {
private final Map<String, Object> params;
private final SearchLookup lookup;

public VectorScoreScriptFactory(Map<String, Object> params, SearchLookup lookup) {
this.params = params;
this.lookup = lookup;
Expand Down