Skip to content

Commit

Permalink
Rename multi-dense vector to rank vectors (elastic#118183)
Browse files Browse the repository at this point in the history
renames `multi_dense_vector` field mapper and such to `rank_vectors` to
better describe its restricted usage.

(cherry picked from commit 31678a3)
  • Loading branch information
benwtrent committed Dec 9, 2024
1 parent afd8d84 commit 5fffef7
Show file tree
Hide file tree
Showing 28 changed files with 295 additions and 379 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ class org.elasticsearch.script.field.SeqNoDocValuesField @dynamic_type {
class org.elasticsearch.script.field.VersionDocValuesField @dynamic_type {
}

class org.elasticsearch.script.field.vectors.MultiDenseVector {
MultiDenseVector EMPTY
class org.elasticsearch.script.field.vectors.RankVectors {
RankVectors EMPTY
float[] getMagnitudes()

Iterator getVectors()
Expand All @@ -142,9 +142,9 @@ class org.elasticsearch.script.field.vectors.MultiDenseVector {
int size()
}

class org.elasticsearch.script.field.vectors.MultiDenseVectorDocValuesField {
MultiDenseVector get()
MultiDenseVector get(MultiDenseVector)
class org.elasticsearch.script.field.vectors.RankVectorsDocValuesField {
RankVectors get()
RankVectors get(RankVectors)
}

class org.elasticsearch.script.field.vectors.DenseVector {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static_import {
double cosineSimilarity(org.elasticsearch.script.ScoreScript, Object, String) bound_to org.elasticsearch.script.VectorScoreScriptUtils$CosineSimilarity
double dotProduct(org.elasticsearch.script.ScoreScript, Object, String) bound_to org.elasticsearch.script.VectorScoreScriptUtils$DotProduct
double hamming(org.elasticsearch.script.ScoreScript, Object, String) bound_to org.elasticsearch.script.VectorScoreScriptUtils$Hamming
double maxSimDotProduct(org.elasticsearch.script.ScoreScript, Object, String) bound_to org.elasticsearch.script.MultiVectorScoreScriptUtils$MaxSimDotProduct
double maxSimInvHamming(org.elasticsearch.script.ScoreScript, Object, String) bound_to org.elasticsearch.script.MultiVectorScoreScriptUtils$MaxSimInvHamming
double maxSimDotProduct(org.elasticsearch.script.ScoreScript, Object, String) bound_to org.elasticsearch.script.RankVectorsScoreScriptUtils$MaxSimDotProduct
double maxSimInvHamming(org.elasticsearch.script.ScoreScript, Object, String) bound_to org.elasticsearch.script.RankVectorsScoreScriptUtils$MaxSimInvHamming
}

Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class org.elasticsearch.index.mapper.vectors.DenseVectorScriptDocValues {
float getMagnitude()
}

class org.elasticsearch.index.mapper.vectors.MultiDenseVectorScriptDocValues {
class org.elasticsearch.index.mapper.vectors.RankVectorsScriptDocValues {
Iterator getVectorValues()
float[] getMagnitudes()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ setup:
capabilities:
- method: POST
path: /_search
capabilities: [ multi_dense_vector_script_max_sim_with_bugfix ]
capabilities: [ rank_vectors_script_max_sim_with_bugfix ]
test_runner_features: capabilities
reason: "Support for multi dense vector max-sim functions capability required"
reason: "Support for rank vectors max-sim functions capability required"
- skip:
features: headers

Expand All @@ -18,14 +18,14 @@ setup:
mappings:
properties:
vector:
type: multi_dense_vector
type: rank_vectors
dims: 5
byte_vector:
type: multi_dense_vector
type: rank_vectors
dims: 5
element_type: byte
bit_vector:
type: multi_dense_vector
type: rank_vectors
dims: 40
element_type: bit
- do:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ setup:
capabilities:
- method: POST
path: /_search
capabilities: [ multi_dense_vector_script_access ]
capabilities: [ rank_vectors_script_access ]
test_runner_features: capabilities
reason: "Support for multi dense vector field script access capability required"
reason: "Support for rank vector field script access capability required"
- skip:
features: headers

Expand All @@ -18,14 +18,14 @@ setup:
mappings:
properties:
vector:
type: multi_dense_vector
type: rank_vectors
dims: 5
byte_vector:
type: multi_dense_vector
type: rank_vectors
dims: 5
element_type: byte
bit_vector:
type: multi_dense_vector
type: rank_vectors
dims: 40
element_type: bit
- do:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ setup:
capabilities:
- method: POST
path: /_search
capabilities: [ multi_dense_vector_field_mapper ]
capabilities: [ rank_vectors_field_mapper ]
test_runner_features: capabilities
reason: "Support for multi dense vector field mapper capability required"
reason: "Support for rank vectors field mapper capability required"
---
"Test create multi-vector field":
- do:
Expand All @@ -15,7 +15,7 @@ setup:
mappings:
properties:
vector1:
type: multi_dense_vector
type: rank_vectors
dims: 3
- do:
index:
Expand Down Expand Up @@ -48,7 +48,7 @@ setup:
name:
type: keyword
vector1:
type: multi_dense_vector
type: rank_vectors
- do:
index:
index: test
Expand Down Expand Up @@ -88,7 +88,7 @@ setup:
mappings:
properties:
vector1:
type: multi_dense_vector
type: rank_vectors
- do:
catch: bad_request
index:
Expand All @@ -105,7 +105,7 @@ setup:
mappings:
properties:
vector1:
type: multi_dense_vector
type: rank_vectors
dims: 3
- do:
catch: bad_request
Expand All @@ -123,7 +123,7 @@ setup:
mappings:
properties:
vector1:
type: multi_dense_vector
type: rank_vectors
dims: 3
- do:
catch: bad_request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
import org.elasticsearch.index.fielddata.LeafFieldData;
import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
import org.elasticsearch.script.field.DocValuesScriptFieldFactory;
import org.elasticsearch.script.field.vectors.BitMultiDenseVectorDocValuesField;
import org.elasticsearch.script.field.vectors.ByteMultiDenseVectorDocValuesField;
import org.elasticsearch.script.field.vectors.FloatMultiDenseVectorDocValuesField;
import org.elasticsearch.script.field.vectors.BitRankVectorsDocValuesField;
import org.elasticsearch.script.field.vectors.ByteRankVectorsDocValuesField;
import org.elasticsearch.script.field.vectors.FloatRankVectorsDocValuesField;

import java.io.IOException;

final class MultiVectorDVLeafFieldData implements LeafFieldData {
final class RankVectorsDVLeafFieldData implements LeafFieldData {
private final LeafReader reader;
private final String field;
private final DenseVectorFieldMapper.ElementType elementType;
private final int dims;

MultiVectorDVLeafFieldData(LeafReader reader, String field, DenseVectorFieldMapper.ElementType elementType, int dims) {
RankVectorsDVLeafFieldData(LeafReader reader, String field, DenseVectorFieldMapper.ElementType elementType, int dims) {
this.reader = reader;
this.field = field;
this.elementType = elementType;
Expand All @@ -38,11 +38,11 @@ final class MultiVectorDVLeafFieldData implements LeafFieldData {
public DocValuesScriptFieldFactory getScriptFieldFactory(String name) {
try {
BinaryDocValues values = DocValues.getBinary(reader, field);
BinaryDocValues magnitudeValues = DocValues.getBinary(reader, field + MultiDenseVectorFieldMapper.VECTOR_MAGNITUDES_SUFFIX);
BinaryDocValues magnitudeValues = DocValues.getBinary(reader, field + RankVectorsFieldMapper.VECTOR_MAGNITUDES_SUFFIX);
return switch (elementType) {
case BYTE -> new ByteMultiDenseVectorDocValuesField(values, magnitudeValues, name, elementType, dims);
case FLOAT -> new FloatMultiDenseVectorDocValuesField(values, magnitudeValues, name, elementType, dims);
case BIT -> new BitMultiDenseVectorDocValuesField(values, magnitudeValues, name, elementType, dims);
case BYTE -> new ByteRankVectorsDocValuesField(values, magnitudeValues, name, elementType, dims);
case FLOAT -> new FloatRankVectorsDocValuesField(values, magnitudeValues, name, elementType, dims);
case BIT -> new BitRankVectorsDocValuesField(values, magnitudeValues, name, elementType, dims);
};
} catch (IOException e) {
throw new IllegalStateException("Cannot load doc values for multi-vector field!", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@
import static org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.MAX_DIMS_COUNT_BIT;
import static org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.namesToElementType;

public class MultiDenseVectorFieldMapper extends FieldMapper {
public class RankVectorsFieldMapper extends FieldMapper {

public static final String VECTOR_MAGNITUDES_SUFFIX = "._magnitude";
public static final FeatureFlag FEATURE_FLAG = new FeatureFlag("multi_dense_vector");
public static final String CONTENT_TYPE = "multi_dense_vector";
public static final FeatureFlag FEATURE_FLAG = new FeatureFlag("rank_vectors");
public static final String CONTENT_TYPE = "rank_vectors";

private static MultiDenseVectorFieldMapper toType(FieldMapper in) {
return (MultiDenseVectorFieldMapper) in;
private static RankVectorsFieldMapper toType(FieldMapper in) {
return (RankVectorsFieldMapper) in;
}

public static class Builder extends FieldMapper.Builder {
Expand Down Expand Up @@ -122,24 +122,24 @@ protected Parameter<?>[] getParameters() {
return new Parameter<?>[] { elementType, dims, meta };
}

public MultiDenseVectorFieldMapper.Builder dimensions(int dimensions) {
public RankVectorsFieldMapper.Builder dimensions(int dimensions) {
this.dims.setValue(dimensions);
return this;
}

public MultiDenseVectorFieldMapper.Builder elementType(DenseVectorFieldMapper.ElementType elementType) {
public RankVectorsFieldMapper.Builder elementType(DenseVectorFieldMapper.ElementType elementType) {
this.elementType.setValue(elementType);
return this;
}

@Override
public MultiDenseVectorFieldMapper build(MapperBuilderContext context) {
public RankVectorsFieldMapper build(MapperBuilderContext context) {
// Validate again here because the dimensions or element type could have been set programmatically,
// which affects index option validity
validate();
return new MultiDenseVectorFieldMapper(
return new RankVectorsFieldMapper(
leafName(),
new MultiDenseVectorFieldType(
new RankVectorsFieldType(
context.buildFullName(leafName()),
elementType.getValue(),
dims.getValue(),
Expand All @@ -153,16 +153,16 @@ public MultiDenseVectorFieldMapper build(MapperBuilderContext context) {
}

public static final TypeParser PARSER = new TypeParser(
(n, c) -> new MultiDenseVectorFieldMapper.Builder(n, c.indexVersionCreated()),
(n, c) -> new RankVectorsFieldMapper.Builder(n, c.indexVersionCreated()),
notInMultiFields(CONTENT_TYPE)
);

public static final class MultiDenseVectorFieldType extends SimpleMappedFieldType {
public static final class RankVectorsFieldType extends SimpleMappedFieldType {
private final DenseVectorFieldMapper.ElementType elementType;
private final Integer dims;
private final IndexVersion indexCreatedVersion;

public MultiDenseVectorFieldType(
public RankVectorsFieldType(
String name,
DenseVectorFieldMapper.ElementType elementType,
Integer dims,
Expand Down Expand Up @@ -207,7 +207,7 @@ public boolean isAggregatable() {

@Override
public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext) {
return new MultiVectorIndexFieldData.Builder(name(), CoreValuesSourceType.KEYWORD, indexCreatedVersion, dims, elementType);
return new RankVectorsIndexFieldData.Builder(name(), CoreValuesSourceType.KEYWORD, indexCreatedVersion, dims, elementType);
}

@Override
Expand All @@ -231,19 +231,14 @@ DenseVectorFieldMapper.ElementType getElementType() {

private final IndexVersion indexCreatedVersion;

private MultiDenseVectorFieldMapper(
String simpleName,
MappedFieldType fieldType,
BuilderParams params,
IndexVersion indexCreatedVersion
) {
private RankVectorsFieldMapper(String simpleName, MappedFieldType fieldType, BuilderParams params, IndexVersion indexCreatedVersion) {
super(simpleName, fieldType, params);
this.indexCreatedVersion = indexCreatedVersion;
}

@Override
public MultiDenseVectorFieldType fieldType() {
return (MultiDenseVectorFieldType) super.fieldType();
public RankVectorsFieldType fieldType() {
return (RankVectorsFieldType) super.fieldType();
}

@Override
Expand Down Expand Up @@ -282,14 +277,14 @@ public void parse(DocumentParserContext context) throws IOException {
);
}
}
MultiDenseVectorFieldType updatedFieldType = new MultiDenseVectorFieldType(
RankVectorsFieldType updatedFieldType = new RankVectorsFieldType(
fieldType().name(),
fieldType().elementType,
currentDims,
indexCreatedVersion,
fieldType().meta()
);
Mapper update = new MultiDenseVectorFieldMapper(leafName(), updatedFieldType, builderParams, indexCreatedVersion);
Mapper update = new RankVectorsFieldMapper(leafName(), updatedFieldType, builderParams, indexCreatedVersion);
context.addDynamicMapper(update);
return;
}
Expand Down Expand Up @@ -371,12 +366,12 @@ protected String contentType() {

@Override
public FieldMapper.Builder getMergeBuilder() {
return new MultiDenseVectorFieldMapper.Builder(leafName(), indexCreatedVersion).init(this);
return new RankVectorsFieldMapper.Builder(leafName(), indexCreatedVersion).init(this);
}

@Override
protected SyntheticSourceSupport syntheticSourceSupport() {
return new SyntheticSourceSupport.Native(new MultiDenseVectorFieldMapper.DocValuesSyntheticFieldLoader());
return new SyntheticSourceSupport.Native(new RankVectorsFieldMapper.DocValuesSyntheticFieldLoader());
}

private class DocValuesSyntheticFieldLoader extends SourceLoader.DocValuesBasedSyntheticFieldLoader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import org.elasticsearch.search.sort.BucketedSort;
import org.elasticsearch.search.sort.SortOrder;

public class MultiVectorIndexFieldData implements IndexFieldData<MultiVectorDVLeafFieldData> {
public class RankVectorsIndexFieldData implements IndexFieldData<RankVectorsDVLeafFieldData> {
protected final String fieldName;
protected final ValuesSourceType valuesSourceType;
private final int dims;
private final IndexVersion indexVersion;
private final DenseVectorFieldMapper.ElementType elementType;

public MultiVectorIndexFieldData(
public RankVectorsIndexFieldData(
String fieldName,
int dims,
ValuesSourceType valuesSourceType,
Expand All @@ -54,19 +54,19 @@ public ValuesSourceType getValuesSourceType() {
}

@Override
public MultiVectorDVLeafFieldData load(LeafReaderContext context) {
return new MultiVectorDVLeafFieldData(context.reader(), fieldName, elementType, dims);
public RankVectorsDVLeafFieldData load(LeafReaderContext context) {
return new RankVectorsDVLeafFieldData(context.reader(), fieldName, elementType, dims);
}

@Override
public MultiVectorDVLeafFieldData loadDirect(LeafReaderContext context) throws Exception {
public RankVectorsDVLeafFieldData loadDirect(LeafReaderContext context) throws Exception {
return load(context);
}

@Override
public SortField sortField(Object missingValue, MultiValueMode sortMode, XFieldComparatorSource.Nested nested, boolean reverse) {
throw new IllegalArgumentException(
"Field [" + fieldName + "] of type [" + MultiDenseVectorFieldMapper.CONTENT_TYPE + "] doesn't support sort"
"Field [" + fieldName + "] of type [" + RankVectorsFieldMapper.CONTENT_TYPE + "] doesn't support sort"
);
}

Expand Down Expand Up @@ -108,7 +108,7 @@ public Builder(

@Override
public IndexFieldData<?> build(IndexFieldDataCache cache, CircuitBreakerService breakerService) {
return new MultiVectorIndexFieldData(name, dims, valuesSourceType, indexVersion, elementType);
return new RankVectorsIndexFieldData(name, dims, valuesSourceType, indexVersion, elementType);
}
}
}
Loading

0 comments on commit 5fffef7

Please sign in to comment.