Skip to content

Commit 8194f3c

Browse files
committed
Initial refactoring to allow per field type block loaders
1 parent 0675a42 commit 8194f3c

File tree

1 file changed

+68
-21
lines changed

1 file changed

+68
-21
lines changed

server/src/main/java/org/elasticsearch/index/mapper/vectors/DenseVectorFieldMapper.java

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.elasticsearch.index.fielddata.IndexFieldData;
5353
import org.elasticsearch.index.mapper.ArraySourceValueFetcher;
5454
import org.elasticsearch.index.mapper.BlockDocValuesReader;
55+
import org.elasticsearch.index.mapper.BlockDocValuesReader.DocValuesBlockLoader;
5556
import org.elasticsearch.index.mapper.BlockLoader;
5657
import org.elasticsearch.index.mapper.BlockSourceReader;
5758
import org.elasticsearch.index.mapper.DocumentParserContext;
@@ -431,6 +432,21 @@ public double computeSquaredMagnitude(VectorData vectorData) {
431432
return VectorUtil.dotProduct(vectorData.asByteVector(), vectorData.asByteVector());
432433
}
433434

435+
@Override
436+
public DocValuesBlockLoader indexedBlockLoader(String fieldName) {
437+
return null;
438+
}
439+
440+
@Override
441+
public DocValuesBlockLoader docValuesBlockLoader(String fieldName, int dimensions, IndexVersion indexVersionCreated) {
442+
return null;
443+
}
444+
445+
@Override
446+
public BlockLoader sourceBlockLoader(String fieldName, MappedFieldType.BlockLoaderContext blContext) {
447+
return null;
448+
}
449+
434450
private VectorData parseVectorArray(
435451
DocumentParserContext context,
436452
int dims,
@@ -672,6 +688,34 @@ public double computeSquaredMagnitude(VectorData vectorData) {
672688
return VectorUtil.dotProduct(vectorData.asFloatVector(), vectorData.asFloatVector());
673689
}
674690

691+
@Override
692+
public DocValuesBlockLoader indexedBlockLoader(String fieldName) {
693+
return new BlockDocValuesReader.DenseVectorBlockLoader(fieldName);
694+
}
695+
696+
@Override
697+
public DocValuesBlockLoader docValuesBlockLoader(String fieldName, int dimensions, IndexVersion indexVersionCreated) {
698+
return new BlockDocValuesReader.DenseVectorFromBinaryBlockLoader(fieldName, dimensions, indexVersionCreated);
699+
}
700+
701+
@Override
702+
public BlockLoader sourceBlockLoader(String fieldName, MappedFieldType.BlockLoaderContext blContext) {
703+
BlockSourceReader.LeafIteratorLookup lookup = BlockSourceReader.lookupMatchingAll();
704+
return new BlockSourceReader.FloatsBlockLoader(sourceValueFetcher(blContext.sourcePaths(fieldName)), lookup);
705+
}
706+
707+
private SourceValueFetcher sourceValueFetcher(Set<String> sourcePaths) {
708+
return new SourceValueFetcher(sourcePaths, null) {
709+
@Override
710+
protected Object parseSourceValue(Object value) {
711+
if (value.equals("")) {
712+
return null;
713+
}
714+
return NumberFieldMapper.NumberType.FLOAT.parse(value, false);
715+
}
716+
};
717+
}
718+
675719
@Override
676720
public void parseKnnVectorAndIndex(DocumentParserContext context, DenseVectorFieldMapper fieldMapper) throws IOException {
677721
int index = 0;
@@ -851,6 +895,21 @@ public double computeSquaredMagnitude(VectorData vectorData) {
851895
return count;
852896
}
853897

898+
@Override
899+
public DocValuesBlockLoader indexedBlockLoader(String fieldName) {
900+
return null;
901+
}
902+
903+
@Override
904+
public DocValuesBlockLoader docValuesBlockLoader(String fieldName, int dimensions, IndexVersion indexVersionCreated) {
905+
return null;
906+
}
907+
908+
@Override
909+
public BlockLoader sourceBlockLoader(String fieldName, MappedFieldType.BlockLoaderContext blContext) {
910+
return null;
911+
}
912+
854913
private VectorData parseVectorArray(
855914
DocumentParserContext context,
856915
int dims,
@@ -1145,6 +1204,12 @@ static Function<StringBuilder, StringBuilder> errorByteElementsAppender(byte[] v
11451204
public static ElementType fromString(String name) {
11461205
return valueOf(name.trim().toUpperCase(Locale.ROOT));
11471206
}
1207+
1208+
public abstract DocValuesBlockLoader indexedBlockLoader(String fieldName);
1209+
1210+
public abstract DocValuesBlockLoader docValuesBlockLoader(String fieldName, int dimensions, IndexVersion indexVersionCreated);
1211+
1212+
public abstract BlockLoader sourceBlockLoader(String fieldName, MappedFieldType.BlockLoaderContext blContext);
11481213
}
11491214

11501215
public static final Map<String, ElementType> namesToElementType = Map.of(
@@ -2324,33 +2389,15 @@ ElementType getElementType() {
23242389

23252390
@Override
23262391
public BlockLoader blockLoader(MappedFieldType.BlockLoaderContext blContext) {
2327-
if (elementType != ElementType.FLOAT) {
2328-
// Just float dense vector support for now
2329-
return null;
2330-
}
2331-
23322392
if (indexed) {
2333-
return new BlockDocValuesReader.DenseVectorBlockLoader(name());
2393+
return getElementType().indexedBlockLoader(name());
23342394
}
23352395

23362396
if (hasDocValues() && (blContext.fieldExtractPreference() != FieldExtractPreference.STORED || isSyntheticSource)) {
2337-
return new BlockDocValuesReader.DenseVectorFromBinaryBlockLoader(name(), dims, indexVersionCreated);
2397+
return getElementType().docValuesBlockLoader(name(), dims, indexVersionCreated);
23382398
}
23392399

2340-
BlockSourceReader.LeafIteratorLookup lookup = BlockSourceReader.lookupMatchingAll();
2341-
return new BlockSourceReader.FloatsBlockLoader(sourceValueFetcher(blContext.sourcePaths(name())), lookup);
2342-
}
2343-
2344-
private SourceValueFetcher sourceValueFetcher(Set<String> sourcePaths) {
2345-
return new SourceValueFetcher(sourcePaths, null) {
2346-
@Override
2347-
protected Object parseSourceValue(Object value) {
2348-
if (value.equals("")) {
2349-
return null;
2350-
}
2351-
return NumberFieldMapper.NumberType.FLOAT.parse(value, false);
2352-
}
2353-
};
2400+
return getElementType().sourceBlockLoader(name(), blContext);
23542401
}
23552402
}
23562403

0 commit comments

Comments
 (0)