Skip to content

Commit 0b1c1d2

Browse files
committed
cleanups/optimizations
1 parent 31f9e6d commit 0b1c1d2

File tree

5 files changed

+11
-36
lines changed

5 files changed

+11
-36
lines changed

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

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,9 @@ public IndexOptions getIndexOptions() {
130130
return this.indexOptions;
131131
}
132132

133-
private static SparseVectorFieldMapper.IndexOptions getDefaultIndexOptions(MappingParserContext context) {
134-
if (context.indexVersionCreated().before(SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_VERSION)) {
135-
// don't set defaults if this index was created before
136-
// we added this functionality in, so it will
137-
// not change current index behaviour
138-
return new IndexOptions(false, null);
139-
}
140-
141-
// index options are not set - for new indices,
142-
// if this is null, in the query will use the
143-
// proper defaults
144-
return null;
145-
}
146-
147133
private static SparseVectorFieldMapper.IndexOptions parseIndexOptions(MappingParserContext context, Object propNode) {
148134
if (propNode == null) {
149-
return getDefaultIndexOptions(context);
135+
return null;
150136
}
151137

152138
Map<String, Object> indexOptionsMap = XContentMapValues.nodeMapValue(propNode, SPARSE_VECTOR_INDEX_OPTIONS);
@@ -155,7 +141,7 @@ private static SparseVectorFieldMapper.IndexOptions parseIndexOptions(MappingPar
155141
TokenPruningConfig pruningConfig = IndexOptions.parseIndexOptionsPruningConfig(prune, indexOptionsMap);
156142

157143
if (prune == null && pruningConfig == null) {
158-
return getDefaultIndexOptions(context);
144+
return null;
159145
}
160146

161147
return new SparseVectorFieldMapper.IndexOptions(prune, pruningConfig);
@@ -175,8 +161,7 @@ public static final class SparseVectorFieldType extends MappedFieldType {
175161
private final IndexOptions indexOptions;
176162

177163
public SparseVectorFieldType(String name, boolean isStored, Map<String, String> meta) {
178-
super(name, true, isStored, false, TextSearchInfo.SIMPLE_MATCH_ONLY, meta);
179-
this.indexOptions = null;
164+
this(name, isStored, meta, null);
180165
}
181166

182167
public SparseVectorFieldType(
@@ -482,17 +467,15 @@ public final int hashCode() {
482467

483468
@Override
484469
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
485-
if (prune == null && pruningConfig == null) {
486-
return builder;
487-
}
488-
489470
builder.startObject();
471+
490472
if (prune != null) {
491473
builder.field(PRUNE_FIELD_NAME, prune);
492474
}
493475
if (pruningConfig != null) {
494476
builder.field(PRUNING_CONFIG_FIELD_NAME, pruningConfig);
495477
}
478+
496479
builder.endObject();
497480
return builder;
498481
}
@@ -516,18 +499,10 @@ public static TokenPruningConfig parseIndexOptionsPruningConfig(Boolean prune, M
516499
return null;
517500
}
518501

519-
if (prune == null) {
502+
if (prune == null || prune == false) {
520503
throw new MapperParsingException("[index_options] field [pruning_config] should only be set if [prune] is set to true");
521504
}
522505

523-
if ((pruningConfiguration instanceof Map) == false) {
524-
throw new MapperParsingException("[index_options] field [pruning_config] should be a map");
525-
}
526-
527-
if (prune == false) {
528-
throw new MapperParsingException("[index_options] field [pruning_config] should not be set if [prune] is false");
529-
}
530-
531506
Map<String, Object> pruningConfigurationMap = XContentMapValues.nodeMapValue(pruningConfiguration, PRUNING_CONFIG_FIELD_NAME);
532507

533508
return TokenPruningConfig.parseFromMap(pruningConfigurationMap);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ public void testPruningConfigurationIsMap() {
301301
b.field("pruning_config", "this_is_not_a_map");
302302
b.endObject();
303303
})));
304-
assertThat(e.getMessage(), containsString("index_options] field [pruning_config] should be a map"));
304+
assertThat(e.getMessage(), containsString("pruning_config should be a map but was of type:"));
305305
}
306306

307307
public void testWithIndexOptionsPruningConfigPruneRequired() throws Exception {
@@ -318,7 +318,7 @@ public void testWithIndexOptionsPruningConfigPruneRequired() throws Exception {
318318
})));
319319
assertThat(
320320
eTestPruneIsFalse.getMessage(),
321-
containsString("Failed to parse mapping: [index_options] field [pruning_config] should not be set if [prune] is false")
321+
containsString("[index_options] field [pruning_config] should only be set if [prune] is set to true")
322322
);
323323

324324
Exception eTestPruneIsMissing = expectThrows(MapperParsingException.class, () -> createMapperService(fieldMapping(b -> {

x-pack/plugin/ml/qa/multi-cluster-tests-with-security/src/test/resources/rest-api-spec/test/multi_cluster/50_sparse_vector.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ teardown:
321321
features: headers
322322

323323
- do:
324-
catch: /\[index_options\] field \[pruning_config\] should not be set if \[prune\] is false/
324+
catch: /\[index_options\] field \[pruning_config\] should only be set if \[prune\] is set to true/
325325
headers:
326326
Authorization: "Basic dGVzdF91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" #test_user credentials
327327
Content-Type: application/json

x-pack/plugin/ml/qa/multi-cluster-tests-with-security/src/test/resources/rest-api-spec/test/remote_cluster/50_sparse_vector.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ teardown:
320320
features: headers
321321

322322
- do:
323-
catch: /\[index_options\] field \[pruning_config\] should not be set if \[prune\] is false/
323+
catch: /\[index_options\] field \[pruning_config\] should only be set if \[prune\] is set to true/
324324
headers:
325325
Authorization: "Basic dGVzdF91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" #test_user credentials
326326
Content-Type: application/json

x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/ml/sparse_vector_search.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ teardown:
651651
features: headers
652652

653653
- do:
654-
catch: /\[index_options\] field \[pruning_config\] should not be set if \[prune\] is false/
654+
catch: /\[index_options\] field \[pruning_config\] should only be set if \[prune\] is set to true/
655655
headers:
656656
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
657657
Content-Type: application/json

0 commit comments

Comments
 (0)