-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Synthetic _source: support dense_vector #89840
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
Changes from all commits
636c40b
6567f47
34a23f8
59b79e9
92f7eac
44645b6
29c8a18
b695e3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 89840 | ||
summary: "Synthetic _source: support `dense_vector`" | ||
area: Vector Search | ||
type: feature | ||
issues: [] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,10 @@ | |
import org.apache.lucene.document.BinaryDocValuesField; | ||
import org.apache.lucene.document.Field; | ||
import org.apache.lucene.document.KnnVectorField; | ||
import org.apache.lucene.index.BinaryDocValues; | ||
import org.apache.lucene.index.LeafReader; | ||
import org.apache.lucene.index.VectorSimilarityFunction; | ||
import org.apache.lucene.index.VectorValues; | ||
import org.apache.lucene.search.FieldExistsQuery; | ||
import org.apache.lucene.search.KnnVectorQuery; | ||
import org.apache.lucene.search.Query; | ||
|
@@ -31,6 +34,7 @@ | |
import org.elasticsearch.index.mapper.MappingLookup; | ||
import org.elasticsearch.index.mapper.MappingParser; | ||
import org.elasticsearch.index.mapper.SimpleMappedFieldType; | ||
import org.elasticsearch.index.mapper.SourceLoader; | ||
import org.elasticsearch.index.mapper.TextSearchInfo; | ||
import org.elasticsearch.index.mapper.ValueFetcher; | ||
import org.elasticsearch.index.query.SearchExecutionContext; | ||
|
@@ -45,6 +49,7 @@ | |
import java.time.ZoneId; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Stream; | ||
|
||
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; | ||
|
||
|
@@ -525,4 +530,97 @@ public KnnVectorsFormat getKnnVectorsFormatForField() { | |
return new Lucene94HnswVectorsFormat(hnswIndexOptions.m, hnswIndexOptions.efConstruction); | ||
} | ||
} | ||
|
||
@Override | ||
public SourceLoader.SyntheticFieldLoader syntheticFieldLoader() { | ||
if (copyTo.copyToFields().isEmpty() != true) { | ||
throw new IllegalArgumentException( | ||
"field [" + name() + "] of type [" + typeName() + "] doesn't support synthetic source because it declares copy_to" | ||
); | ||
} | ||
if (indexed) { | ||
return new IndexedSyntheticFieldLoader(); | ||
} | ||
return new DocValuesSyntheticFieldLoader(); | ||
} | ||
|
||
private class IndexedSyntheticFieldLoader implements SourceLoader.SyntheticFieldLoader { | ||
private VectorValues values; | ||
private boolean hasValue; | ||
|
||
@Override | ||
public Stream<Map.Entry<String, StoredFieldLoader>> storedFieldLoaders() { | ||
return Stream.of(); | ||
} | ||
|
||
@Override | ||
public DocValuesLoader docValuesLoader(LeafReader leafReader, int[] docIdsInLeaf) throws IOException { | ||
values = leafReader.getVectorValues(name()); | ||
if (values == null) { | ||
return null; | ||
} | ||
return docId -> { | ||
hasValue = docId == values.advance(docId); | ||
return hasValue; | ||
}; | ||
} | ||
|
||
@Override | ||
public boolean hasValue() { | ||
return hasValue; | ||
} | ||
|
||
@Override | ||
public void write(XContentBuilder b) throws IOException { | ||
if (false == hasValue) { | ||
return; | ||
} | ||
b.startArray(simpleName()); | ||
for (float v : values.vectorValue()) { | ||
b.value(v); | ||
} | ||
b.endArray(); | ||
} | ||
} | ||
|
||
private class DocValuesSyntheticFieldLoader implements SourceLoader.SyntheticFieldLoader { | ||
private BinaryDocValues values; | ||
private boolean hasValue; | ||
|
||
@Override | ||
public Stream<Map.Entry<String, StoredFieldLoader>> storedFieldLoaders() { | ||
return Stream.of(); | ||
} | ||
|
||
@Override | ||
public DocValuesLoader docValuesLoader(LeafReader leafReader, int[] docIdsInLeaf) throws IOException { | ||
values = leafReader.getBinaryDocValues(name()); | ||
if (values == null) { | ||
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. This needs to be |
||
return null; | ||
} | ||
return docId -> { | ||
hasValue = docId == values.advance(docId); | ||
return hasValue; | ||
}; | ||
} | ||
|
||
@Override | ||
public boolean hasValue() { | ||
return hasValue; | ||
} | ||
|
||
@Override | ||
public void write(XContentBuilder b) throws IOException { | ||
if (false == hasValue) { | ||
return; | ||
} | ||
b.startArray(simpleName()); | ||
BytesRef ref = values.binaryValue(); | ||
ByteBuffer byteBuffer = ByteBuffer.wrap(ref.bytes, ref.offset, ref.length); | ||
for (int dim = 0; dim < dims; dim++) { | ||
b.value(byteBuffer.getFloat()); | ||
} | ||
b.endArray(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
import org.elasticsearch.index.mapper.ParsedDocument; | ||
import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.DenseVectorFieldType; | ||
import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.VectorSimilarity; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
import org.junit.AssumptionViolatedException; | ||
|
||
|
@@ -465,12 +466,50 @@ public void testKnnVectorsFormat() throws IOException { | |
} | ||
|
||
@Override | ||
protected SyntheticSourceSupport syntheticSourceSupport() { | ||
protected IngestScriptSupport ingestScriptSupport() { | ||
throw new AssumptionViolatedException("not supported"); | ||
} | ||
|
||
@Override | ||
protected IngestScriptSupport ingestScriptSupport() { | ||
throw new AssumptionViolatedException("not supported"); | ||
protected SyntheticSourceSupport syntheticSourceSupport() { | ||
return new DenseVectorSyntheticSourceSupport(); | ||
} | ||
|
||
@Override | ||
protected boolean supportsEmptyInputArray() { | ||
return false; | ||
} | ||
|
||
private static class DenseVectorSyntheticSourceSupport implements SyntheticSourceSupport { | ||
private final int dims = between(5, 1000); | ||
private final boolean indexed = randomBoolean(); | ||
private final boolean indexOptionsSet = indexed && randomBoolean(); | ||
|
||
@Override | ||
public SyntheticSourceExample example(int maxValues) throws IOException { | ||
List<Float> value = randomList(dims, dims, ESTestCase::randomFloat); | ||
return new SyntheticSourceExample(value, value, this::mapping); | ||
} | ||
|
||
private void mapping(XContentBuilder b) throws IOException { | ||
b.field("type", "dense_vector"); | ||
b.field("dims", dims); | ||
if (indexed) { | ||
b.field("index", true); | ||
b.field("similarity", "l2_norm"); | ||
if (indexOptionsSet) { | ||
b.startObject("index_options"); | ||
b.field("type", "hnsw"); | ||
b.field("m", 5); | ||
b.field("ef_construction", 50); | ||
b.endObject(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public List<SyntheticSourceInvalidExample> invalidExample() throws IOException { | ||
return List.of(); | ||
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. It looks to me like there is nothing like |
||
} | ||
} | ||
} |
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.
We've been using the lucene APIs directly for
SyntheticFieldLoader
subclasses. I believe it saves allocating afloat[]
on the doc values version which is nice, but not a huge thing. But I did it just to line up with the other implementations.