Skip to content

Commit fc78b0f

Browse files
committed
cleanups; start of yamlRestTests
1 parent 65c5147 commit fc78b0f

File tree

4 files changed

+276
-174
lines changed

4 files changed

+276
-174
lines changed

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

Lines changed: 15 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,10 @@ protected Parameter<?>[] getParameters() {
117117

118118
@Override
119119
public SparseVectorFieldMapper build(MapperBuilderContext context) {
120-
IndexOptions buildIndexOptions = indexOptions.getValue();
121-
122-
if (buildIndexOptions == null) {
123-
buildIndexOptions = new IndexOptions(
124-
true,
125-
new TokenPruningConfig(
126-
TokenPruningConfig.DEFAULT_TOKENS_FREQ_RATIO_THRESHOLD,
127-
TokenPruningConfig.DEFAULT_TOKENS_WEIGHT_THRESHOLD,
128-
false
129-
)
130-
);
131-
}
132-
133120
return new SparseVectorFieldMapper(
134121
leafName(),
135-
new SparseVectorFieldType(context.buildFullName(leafName()), stored.getValue(), meta.getValue(), buildIndexOptions),
136-
builderParams(this, context),
137-
buildIndexOptions
122+
new SparseVectorFieldType(context.buildFullName(leafName()), stored.getValue(), meta.getValue(), indexOptions.getValue()),
123+
builderParams(this, context)
138124
);
139125
}
140126
}
@@ -158,6 +144,10 @@ private static SparseVectorFieldMapper.IndexOptions getDefaultIndexOptions(Mappi
158144
}
159145

160146
private static SparseVectorFieldMapper.IndexOptions parseIndexOptions(MappingParserContext context, Object propNode) {
147+
if (propNode == null) {
148+
return getDefaultIndexOptions(context);
149+
}
150+
161151
Map<String, Object> indexOptionsMap = XContentMapValues.nodeMapValue(propNode, SPARSE_VECTOR_INDEX_OPTIONS);
162152

163153
Boolean prune = IndexOptions.parseIndexOptionsPruneValue(indexOptionsMap);
@@ -198,6 +188,10 @@ public SparseVectorFieldType(
198188
this.indexOptions = indexOptions;
199189
}
200190

191+
public IndexOptions getIndexOptions() {
192+
return indexOptions;
193+
}
194+
201195
@Override
202196
public String typeName() {
203197
return CONTENT_TYPE;
@@ -241,14 +235,9 @@ private static String indexedValueForSearch(Object value) {
241235
}
242236
}
243237

244-
private SparseVectorFieldMapper(
245-
String simpleName,
246-
MappedFieldType mappedFieldType,
247-
BuilderParams builderParams,
248-
@Nullable IndexOptions indexOptions
249-
) {
238+
private SparseVectorFieldMapper(String simpleName, MappedFieldType mappedFieldType, BuilderParams builderParams) {
250239
super(simpleName, mappedFieldType, builderParams);
251-
this.indexOptions = indexOptions;
240+
this.indexOptions = ((SparseVectorFieldType) mappedFieldType).getIndexOptions();
252241
}
253242

254243
@Override
@@ -513,11 +502,11 @@ public static Boolean parseIndexOptionsPruneValue(Map<String, Object> indexOptio
513502
return null;
514503
}
515504

516-
if ((shouldPrune instanceof Boolean) == false) {
517-
throw new MapperParsingException("[index_options] field [prune] should be true or false");
505+
if (shouldPrune instanceof Boolean boolValue) {
506+
return boolValue;
518507
}
519508

520-
return ((Boolean) shouldPrune);
509+
throw new MapperParsingException("[index_options] field [prune] should be true or false");
521510
}
522511

523512
public static TokenPruningConfig parseIndexOptionsPruningConfig(Boolean prune, Map<String, Object> indexOptionsMap) {
@@ -538,147 +527,4 @@ public static TokenPruningConfig parseIndexOptionsPruningConfig(Boolean prune, M
538527
return TokenPruningConfig.parseFromMap(pruningConfigurationMap);
539528
}
540529
}
541-
542-
/*
543-
public static class PruningConfig implements ToXContent {
544-
public static final String TOKENS_FREQ_RATIO_THRESHOLD_FIELD_NAME = "tokens_freq_ratio_threshold";
545-
public static final String TOKENS_WEIGHT_THRESHOLD_FIELD_NAME = "tokens_weight_threshold";
546-
547-
public static float DEFAULT_TOKENS_FREQ_RATIO_THRESHOLD = 5;
548-
public static float MIN_TOKENS_FREQ_RATIO_THRESHOLD = 1;
549-
public static float MAX_TOKENS_FREQ_RATIO_THRESHOLD = 100;
550-
551-
public static float DEFAULT_TOKENS_WEIGHT_THRESHOLD = 0.4f;
552-
public static float MIN_TOKENS_WEIGHT_THRESHOLD = 0.0f;
553-
public static float MAX_TOKENS_WEIGHT_THRESHOLD = 1.0f;
554-
555-
final Float tokens_freq_ratio_threshold;
556-
final Float tokens_weight_threshold;
557-
558-
PruningConfig(@Nullable Float tokens_freq_ratio_threshold, @Nullable Float tokens_weight_threshold) {
559-
this.tokens_freq_ratio_threshold = tokens_freq_ratio_threshold;
560-
this.tokens_weight_threshold = tokens_weight_threshold;
561-
}
562-
563-
public float getTokensFreqRatioThresholdOrDefault() {
564-
if (tokens_freq_ratio_threshold == null) {
565-
return DEFAULT_TOKENS_FREQ_RATIO_THRESHOLD;
566-
}
567-
return tokens_freq_ratio_threshold;
568-
}
569-
570-
public float getTokensWeightThresholdOrDefault() {
571-
if (tokens_weight_threshold == null) {
572-
return DEFAULT_TOKENS_WEIGHT_THRESHOLD;
573-
}
574-
return tokens_weight_threshold;
575-
}
576-
577-
@Override
578-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
579-
builder.startObject();
580-
if (tokens_freq_ratio_threshold != null) {
581-
builder.field(TOKENS_FREQ_RATIO_THRESHOLD_FIELD_NAME, tokens_freq_ratio_threshold);
582-
}
583-
if (tokens_weight_threshold != null) {
584-
builder.field(TOKENS_WEIGHT_THRESHOLD_FIELD_NAME, tokens_weight_threshold);
585-
}
586-
builder.endObject();
587-
return builder;
588-
}
589-
590-
@Override
591-
public final boolean equals(Object other) {
592-
if (other == this) {
593-
return true;
594-
}
595-
if (other instanceof PruningConfig otherConfig) {
596-
return Objects.equals(tokens_freq_ratio_threshold, otherConfig.tokens_freq_ratio_threshold)
597-
&& Objects.equals(tokens_weight_threshold, otherConfig.tokens_weight_threshold);
598-
}
599-
return false;
600-
}
601-
602-
@Override
603-
public final int hashCode() {
604-
return Objects.hash(tokens_freq_ratio_threshold, tokens_weight_threshold);
605-
}
606-
607-
public static PruningConfig parsePruningConfig(Object pruningConfiguration) {
608-
Map<String, Object> pruningConfigMap = XContentMapValues.nodeMapValue(pruningConfiguration, SPARSE_VECTOR_INDEX_OPTIONS);
609-
610-
Object mappedTokensFreqRatioThreshold = pruningConfigMap.remove(TOKENS_FREQ_RATIO_THRESHOLD_FIELD_NAME);
611-
Object mappedTokensWeightThreshold = pruningConfigMap.remove(TOKENS_WEIGHT_THRESHOLD_FIELD_NAME);
612-
613-
if (pruningConfigMap.isEmpty() == false) {
614-
throw new MapperParsingException("[index_options] field [pruning_config] has unknown fields");
615-
}
616-
617-
Float tokensFreqRatioThreshold = parseTokensFreqRatioThreshold(mappedTokensFreqRatioThreshold);
618-
Float tokensWeightThreshold = parseTokensWeightThreshold(mappedTokensWeightThreshold);
619-
620-
if (tokensFreqRatioThreshold != null || tokensWeightThreshold != null) {
621-
return new PruningConfig(tokensFreqRatioThreshold, tokensWeightThreshold);
622-
}
623-
624-
return null;
625-
}
626-
627-
private static Float parseFloatNumberFromObject(Object numberObject) {
628-
if (numberObject instanceof Integer intValue) {
629-
return (float) intValue;
630-
} else if (numberObject instanceof Float floatValue) {
631-
return floatValue;
632-
} else if (numberObject instanceof Double doubleValue) {
633-
return ((Double) numberObject).floatValue();
634-
}
635-
return null;
636-
}
637-
638-
private static Float parseTokensWeightThreshold(Object mappedTokensWeightThreshold) {
639-
if (mappedTokensWeightThreshold == null) {
640-
return null;
641-
}
642-
643-
Float tokensWeightThreshold = parseFloatNumberFromObject(mappedTokensWeightThreshold);
644-
645-
if (tokensWeightThreshold == null) {
646-
throw new MapperParsingException(
647-
"[pruning_config] field [tokens_weight_threshold] field should be a number between 0.0 and 1.0"
648-
);
649-
}
650-
651-
if (tokensWeightThreshold < PruningConfig.MIN_TOKENS_WEIGHT_THRESHOLD
652-
|| tokensWeightThreshold > PruningConfig.MAX_TOKENS_WEIGHT_THRESHOLD) {
653-
throw new MapperParsingException(
654-
"[pruning_config] field [tokens_weight_threshold] field should be a number between 0.0 and 1.0"
655-
);
656-
}
657-
return tokensWeightThreshold;
658-
}
659-
660-
private static Float parseTokensFreqRatioThreshold(Object mappedTokensFreqRatioThreshold) {
661-
if (mappedTokensFreqRatioThreshold == null) {
662-
return null;
663-
}
664-
665-
Float tokensFreqRatioThreshold = parseFloatNumberFromObject(mappedTokensFreqRatioThreshold);
666-
667-
if (tokensFreqRatioThreshold == null) {
668-
throw new MapperParsingException(
669-
"[pruning_config] field [tokens_freq_ratio_threshold] field should be a number between 1 and 100"
670-
);
671-
}
672-
673-
if (tokensFreqRatioThreshold < PruningConfig.MIN_TOKENS_FREQ_RATIO_THRESHOLD
674-
|| tokensFreqRatioThreshold > PruningConfig.MAX_TOKENS_FREQ_RATIO_THRESHOLD) {
675-
throw new MapperParsingException(
676-
"[pruning_config] field [tokens_freq_ratio_threshold] field should be a number between 1 and 100"
677-
);
678-
}
679-
680-
return tokensFreqRatioThreshold;
681-
}
682-
}
683-
*/
684530
}

server/src/test/java/org/elasticsearch/index/mapper/vectors/SparseVectorFieldMapperTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private static int getFrequency(TokenStream tk) throws IOException {
124124

125125
public void testDefaults() throws Exception {
126126
DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping));
127-
assertEquals(Strings.toString(fieldMapping(this::mappingWithDefaultIndexOptions)), mapper.mappingSource().toString());
127+
assertEquals(Strings.toString(fieldMapping(this::minimalMapping)), mapper.mappingSource().toString());
128128

129129
ParsedDocument doc1 = mapper.parse(source(this::writeField));
130130

@@ -150,9 +150,7 @@ public void testDefaults() throws Exception {
150150

151151
public void testWithIndexOptionsPrune() throws Exception {
152152
DocumentMapper mapper = createDocumentMapper(fieldMapping(this::mappingWithIndexOptionsPrune));
153-
String expectedMapping = "{\"_doc\":{\"properties\":{\"field\":{\"type\":\"sparse_vector\",\"index_options\":"
154-
+ "{\"prune\":true}}}}}";
155-
assertEquals(expectedMapping, mapper.mappingSource().toString());
153+
assertEquals(Strings.toString(fieldMapping(this::mappingWithIndexOptionsPrune)), mapper.mappingSource().toString());
156154

157155
ParsedDocument doc1 = mapper.parse(source(this::writeField));
158156

@@ -176,7 +174,7 @@ public void testWithIndexOptionsPrune() throws Exception {
176174
assertTrue(freq1 < freq2);
177175
}
178176

179-
public void testWithIndexOptionsPruningConfig() throws Exception {
177+
public void testWithIndexOptionsPruningConfigOnly() throws Exception {
180178
DocumentMapper mapper = createDocumentMapper(fieldMapping(this::mappingWithIndexOptionsPruningConfig));
181179
assertEquals(Strings.toString(fieldMapping(this::mappingWithIndexOptionsPruningConfig)), mapper.mappingSource().toString());
182180

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackFeatures.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
import java.util.Set;
1414

15+
import static org.elasticsearch.index.mapper.vectors.SparseVectorFieldMapper.SPARSE_VECTOR_INDEX_OPTIONS_FEATURE;
16+
1517
/**
1618
* Provides the XPack features that this version of the code supports
1719
*/
@@ -20,4 +22,9 @@ public class XPackFeatures implements FeatureSpecification {
2022
public Set<NodeFeature> getFeatures() {
2123
return Set.of();
2224
}
25+
26+
@Override
27+
public Set<NodeFeature> getTestFeatures() {
28+
return Set.of(SPARSE_VECTOR_INDEX_OPTIONS_FEATURE);
29+
}
2330
}

0 commit comments

Comments
 (0)