Skip to content

Commit aeec0ca

Browse files
committed
fix docs; add include_defaults unit and yaml test
1 parent 350910c commit aeec0ca

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

docs/reference/elasticsearch/mapping-reference/sparse-vector.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ PUT my-index
2424
}
2525
```
2626

27-
{applies_to}`stack: preview 9.1`
27+
## Token pruning
28+
```{applies_to}
29+
stack: preview 9.1
30+
```
31+
2832
With any new indices created, token pruning will be turned on by default with appropriate defaults. You can control this behaviour using the optional `index_options` parameters for the field:
2933

3034
```console

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ public SparseVectorFieldMapper build(MapperBuilderContext context) {
146146
}
147147

148148
private boolean indexOptionsSerializerCheck(boolean includeDefaults, boolean isConfigured, IndexOptions value) {
149-
return includeDefaults || (value == null || IndexOptions.isDefaultOptions(value, indexVersion)) == false;
149+
return (
150+
indexVersionSupportsDefaultPruningConfig(indexVersion) && (
151+
includeDefaults || (value == null || IndexOptions.isDefaultOptions(value, indexVersion)) == false
152+
)
153+
);
150154
}
151155
}
152156

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.elasticsearch.test.index.IndexVersionUtils;
4141
import org.elasticsearch.xcontent.XContentBuilder;
4242
import org.elasticsearch.xcontent.XContentParseException;
43+
import org.elasticsearch.xcontent.json.JsonXContent;
4344
import org.hamcrest.Matchers;
4445
import org.junit.AssumptionViolatedException;
4546

@@ -80,6 +81,21 @@ protected void minimalMapping(XContentBuilder b) throws IOException {
8081
b.field("type", "sparse_vector");
8182
}
8283

84+
protected void minimalMappingWithExplicitDefaults(XContentBuilder b) throws IOException {
85+
b.field("type", "sparse_vector");
86+
b.startObject("index_options");
87+
{
88+
b.field("prune", true);
89+
b.startObject("pruning_config");
90+
{
91+
b.field("tokens_freq_ratio_threshold", TokenPruningConfig.DEFAULT_TOKENS_FREQ_RATIO_THRESHOLD);
92+
b.field("tokens_weight_threshold", TokenPruningConfig.DEFAULT_TOKENS_WEIGHT_THRESHOLD);
93+
}
94+
b.endObject();
95+
}
96+
b.endObject();
97+
}
98+
8399
protected void minimalMappingWithExplicitIndexOptions(XContentBuilder b) throws IOException {
84100
b.field("type", "sparse_vector");
85101
b.startObject("index_options");
@@ -195,6 +211,19 @@ public void testDefaults() throws Exception {
195211
assertTrue(freq1 < freq2);
196212
}
197213

214+
public void testDefaultsWithIncludeDefaults() throws Exception {
215+
XContentBuilder orig = JsonXContent.contentBuilder().startObject();
216+
createMapperService(fieldMapping(this::minimalMapping)).documentMapper().mapping().toXContent(orig, INCLUDE_DEFAULTS);
217+
orig.endObject();
218+
219+
XContentBuilder withDefaults = JsonXContent.contentBuilder().startObject();
220+
createMapperService(fieldMapping(this::minimalMappingWithExplicitDefaults)).documentMapper().mapping()
221+
.toXContent(withDefaults, INCLUDE_DEFAULTS);
222+
withDefaults.endObject();
223+
224+
assertEquals(Strings.toString(withDefaults), Strings.toString(orig));
225+
}
226+
198227
public void testMappingWithoutIndexOptionsUsesDefaults() throws Exception {
199228
DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping));
200229
assertEquals(Strings.toString(fieldMapping(this::minimalMapping)), mapper.mappingSource().toString());

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,13 +595,15 @@ teardown:
595595
- do:
596596
headers:
597597
Content-Type: application/json
598-
indices.get_mapping:
598+
indices.get_field_mapping:
599599
index: sparse_vector_pruning_test
600+
fields: ml.tokens
601+
include_defaults: true
600602

601603
# the index_options with pruning defaults will be serialized here explicitly
602-
- match: { sparse_vector_pruning_test.mappings.properties.ml.properties.tokens.index_options.prune: true }
603-
- match: { sparse_vector_pruning_test.mappings.properties.ml.properties.tokens.index_options.pruning_config.tokens_freq_ratio_threshold: 5.0 }
604-
- match: { sparse_vector_pruning_test.mappings.properties.ml.properties.tokens.index_options.pruning_config.tokens_weight_threshold: 0.4 }
604+
- match: { sparse_vector_pruning_test.mappings.ml.tokens.mapping.tokens.index_options.prune: true }
605+
- match: { sparse_vector_pruning_test.mappings.ml.tokens.mapping.tokens.index_options.pruning_config.tokens_freq_ratio_threshold: 5.0 }
606+
- match: { sparse_vector_pruning_test.mappings.ml.tokens.mapping.tokens.index_options.pruning_config.tokens_weight_threshold: 0.4 }
605607

606608

607609
---

0 commit comments

Comments
 (0)