Skip to content

Commit 71af331

Browse files
committed
cleanups; add comments; remove redundant test
1 parent 1cab345 commit 71af331

File tree

4 files changed

+36
-26
lines changed

4 files changed

+36
-26
lines changed

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,6 @@ private static String indexedValueForSearch(Object value) {
300300
}
301301
return value.toString();
302302
}
303-
304-
public IndexVersion getIndexVersionCreated() {
305-
return indexVersionCreated;
306-
}
307303
}
308304

309305
private SparseVectorFieldMapper(String simpleName, MappedFieldType mappedFieldType, BuilderParams builderParams) {
@@ -325,8 +321,7 @@ public Map<String, NamedAnalyzer> indexAnalyzers() {
325321

326322
@Override
327323
public FieldMapper.Builder getMergeBuilder() {
328-
IndexVersion indexVersion = this.fieldType() != null ? this.fieldType().getIndexVersionCreated() : IndexVersion.current();
329-
return new Builder(leafName(), indexVersion).init(this);
324+
return new Builder(leafName(), this.fieldType().indexVersionCreated).init(this);
330325
}
331326

332327
@Override
@@ -406,9 +401,8 @@ protected String contentType() {
406401

407402
private static boolean indexVersionSupportsDefaultPruningConfig(IndexVersion indexVersion) {
408403
// default pruning for 9.1.0+ or 8.19.0+ is true for this index
409-
return (indexVersion != null
410-
&& (indexVersion.onOrAfter(SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_VERSION)
411-
|| indexVersion.between(SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_VERSION_8_X, IndexVersions.UPGRADE_TO_LUCENE_10_0_0)));
404+
return (indexVersion.onOrAfter(SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_VERSION)
405+
|| indexVersion.between(SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_VERSION_8_X, IndexVersions.UPGRADE_TO_LUCENE_10_0_0));
412406
}
413407

414408
private static class SparseVectorValueFetcher implements ValueFetcher {

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.elasticsearch.search.lookup.Source;
3939
import org.elasticsearch.search.vectors.SparseVectorQueryWrapper;
4040
import org.elasticsearch.test.index.IndexVersionUtils;
41+
import org.elasticsearch.xcontent.ToXContent;
4142
import org.elasticsearch.xcontent.XContentBuilder;
4243
import org.elasticsearch.xcontent.XContentParseException;
4344
import org.elasticsearch.xcontent.json.JsonXContent;
@@ -88,6 +89,9 @@ protected void minimalFieldMappingPreviousIndexVersion(XContentBuilder b) throws
8889
b.startObject("meta");
8990
b.endObject();
9091

92+
// note that internally, this will have a `index_options: null` field,
93+
// but when serialized back to the client, this field will be pruned
94+
// the YAML Rest tests checks for this
9195
b.field("index_options", (Object) null);
9296
}
9397

@@ -256,7 +260,7 @@ private void buildDocForSparseVectorFieldMapping(XContentBuilder b, CheckedConsu
256260
b.endObject();
257261
};
258262

259-
public void testDefaultsWithIncludeDefaults() throws Exception {
263+
public void testDefaultsWithAndWithoutIncludeDefaults() throws Exception {
260264
XContentBuilder orig = JsonXContent.contentBuilder().startObject();
261265
createMapperService(fieldMapping(this::minimalMapping)).documentMapper().mapping().toXContent(orig, INCLUDE_DEFAULTS);
262266
orig.endObject();
@@ -266,9 +270,16 @@ public void testDefaultsWithIncludeDefaults() throws Exception {
266270
withDefaults.endObject();
267271

268272
assertEquals(Strings.toString(withDefaults), Strings.toString(orig));
273+
274+
XContentBuilder origWithoutDefaults = JsonXContent.contentBuilder().startObject();
275+
createMapperService(fieldMapping(this::minimalMapping))
276+
.documentMapper().mapping().toXContent(origWithoutDefaults, ToXContent.EMPTY_PARAMS);
277+
origWithoutDefaults.endObject();
278+
279+
assertEquals(Strings.toString(fieldMapping(this::minimalMapping)), Strings.toString(origWithoutDefaults));
269280
}
270281

271-
public void testDefaultsWithIncludeDefaultsOlderIndexVersion() throws Exception {
282+
public void testDefaultsWithAndWithoutIncludeDefaultsOlderIndexVersion() throws Exception {
272283
IndexVersion indexVersion = IndexVersionUtils.randomVersionBetween(
273284
random(),
274285
UPGRADE_TO_LUCENE_10_0_0,
@@ -284,19 +295,17 @@ public void testDefaultsWithIncludeDefaultsOlderIndexVersion() throws Exception
284295
withDefaults.endObject();
285296

286297
assertEquals(Strings.toString(withDefaults), Strings.toString(orig));
287-
}
288298

289-
public void testMappingWithoutIndexOptionsUsesDefaults() throws Exception {
290-
DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping));
291-
assertEquals(Strings.toString(fieldMapping(this::minimalMapping)), mapper.mappingSource().toString());
299+
XContentBuilder origWithoutDefaults = JsonXContent.contentBuilder().startObject();
300+
createMapperService(indexVersion, fieldMapping(this::minimalMapping))
301+
.documentMapper().mapping().toXContent(origWithoutDefaults, INCLUDE_DEFAULTS);
302+
origWithoutDefaults.endObject();
292303

293-
IndexVersion preIndexOptionsVersion = IndexVersionUtils.randomVersionBetween(
294-
random(),
295-
UPGRADE_TO_LUCENE_10_0_0,
296-
IndexVersionUtils.getPreviousVersion(SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_VERSION)
297-
);
298-
DocumentMapper previousMapper = createDocumentMapper(preIndexOptionsVersion, fieldMapping(this::minimalMapping));
299-
assertEquals(Strings.toString(fieldMapping(this::minimalMapping)), previousMapper.mappingSource().toString());
304+
XContentBuilder withoutDefaults = JsonXContent.contentBuilder().startObject();
305+
buildDocForSparseVectorFieldMapping(withoutDefaults, this::minimalFieldMappingPreviousIndexVersion);
306+
withoutDefaults.endObject();
307+
308+
assertEquals(Strings.toString(withoutDefaults), Strings.toString(origWithoutDefaults));
300309
}
301310

302311
public void testMappingWithExplicitIndexOptions() throws Exception {
@@ -694,7 +703,7 @@ private void withSearchExecutionContext(MapperService mapperService, CheckedCons
694703
iw.close();
695704

696705
try (DirectoryReader reader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
697-
var searchContext = createSearchExecutionContext(mapperService, new IndexSearcher(reader));
706+
var searchContext = createSearchExecutionContext(mapperService, newSearcher(reader));
698707
consumer.accept(searchContext);
699708
}
700709
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,27 @@
1010
package org.elasticsearch.index.mapper.vectors;
1111

1212
import org.elasticsearch.index.IndexVersion;
13+
import org.elasticsearch.index.IndexVersions;
1314
import org.elasticsearch.index.fielddata.FieldDataContext;
1415
import org.elasticsearch.index.mapper.FieldTypeTestCase;
1516
import org.elasticsearch.index.mapper.MappedFieldType;
17+
import org.elasticsearch.test.index.IndexVersionUtils;
1618

1719
import java.util.Collections;
1820

1921
public class SparseVectorFieldTypeTests extends FieldTypeTestCase {
2022

2123
public void testDocValuesDisabled() {
22-
IndexVersion indexVersion = IndexVersion.current();
24+
IndexVersion indexVersion = IndexVersionUtils.randomVersionBetween(random(),
25+
IndexVersions.SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT, IndexVersion.current());
2326
MappedFieldType fieldType = new SparseVectorFieldMapper.SparseVectorFieldType(indexVersion, "field", false, Collections.emptyMap());
2427
assertFalse(fieldType.hasDocValues());
2528
expectThrows(IllegalArgumentException.class, () -> fieldType.fielddataBuilder(FieldDataContext.noRuntimeFields("test")));
2629
}
2730

2831
public void testIsNotAggregatable() {
29-
IndexVersion indexVersion = IndexVersion.current();
32+
IndexVersion indexVersion = IndexVersionUtils.randomVersionBetween(random(),
33+
IndexVersions.SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT, IndexVersion.current());
3034
MappedFieldType fieldType = new SparseVectorFieldMapper.SparseVectorFieldType(indexVersion, "field", false, Collections.emptyMap());
3135
assertFalse(fieldType.isAggregatable());
3236
}

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/queries/SemanticQueryBuilderTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ public class SemanticQueryBuilderTests extends AbstractQueryTestCase<SemanticQue
102102
private static DenseVectorFieldMapper.ElementType denseVectorElementType;
103103
private static boolean useSearchInferenceId;
104104
private final boolean useLegacyFormat;
105-
private MapperService currentMapperService;
106105

107106
private enum InferenceResultType {
108107
NONE,
@@ -186,6 +185,10 @@ protected void initializeAdditionalMappings(MapperService mapperService) throws
186185

187186
@Override
188187
protected IndexReaderManager getIndexReaderManager() {
188+
// note that because token pruning for sparse vector types are on by default now
189+
// we have to have at least one document with the `semantic.inference.chunks.embeddings`
190+
// populated or else the weightedTokenUtils will return a MatchNoDocsQuery instead of the
191+
// expected BooleanQuery.
189192
return new IndexReaderManager() {
190193
@Override
191194
protected void initIndexWriter(RandomIndexWriter indexWriter) throws IOException {

0 commit comments

Comments
 (0)