Skip to content

Commit b10896b

Browse files
authored
[8.15] Change synthetic source logic for constant_keyword (#117182) (#117211)
* Change synthetic source logic for constant_keyword (#117182) * Change synthetic source logic for constant_keyword * Update docs/changelog/117182.yaml (cherry picked from commit 3a1bc05) # Conflicts: # x-pack/plugin/mapper-constant-keyword/src/main/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapper.java * Fix style and test * Fix test
1 parent b954f9e commit b10896b

File tree

4 files changed

+50
-48
lines changed

4 files changed

+50
-48
lines changed

docs/changelog/117182.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 117182
2+
summary: Change synthetic source logic for `constant_keyword`
3+
area: Mapping
4+
type: bug
5+
issues:
6+
- 117083

x-pack/plugin/mapper-constant-keyword/src/main/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapper.java

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package org.elasticsearch.xpack.constantkeyword.mapper;
99

1010
import org.apache.lucene.index.IndexReader;
11-
import org.apache.lucene.index.LeafReader;
1211
import org.apache.lucene.index.TermsEnum;
1312
import org.apache.lucene.search.MatchAllDocsQuery;
1413
import org.apache.lucene.search.MatchNoDocsQuery;
@@ -41,7 +40,6 @@
4140
import org.elasticsearch.index.mapper.Mapper;
4241
import org.elasticsearch.index.mapper.MapperBuilderContext;
4342
import org.elasticsearch.index.mapper.MapperParsingException;
44-
import org.elasticsearch.index.mapper.SourceLoader;
4543
import org.elasticsearch.index.mapper.ValueFetcher;
4644
import org.elasticsearch.index.query.QueryRewriteContext;
4745
import org.elasticsearch.index.query.SearchExecutionContext;
@@ -57,7 +55,6 @@
5755
import java.util.Locale;
5856
import java.util.Map;
5957
import java.util.Objects;
60-
import java.util.stream.Stream;
6158

6259
/**
6360
* A {@link FieldMapper} that assigns every document the same value.
@@ -345,48 +342,14 @@ protected String contentType() {
345342

346343
@Override
347344
protected SyntheticSourceMode syntheticSourceMode() {
348-
return SyntheticSourceMode.NATIVE;
349-
}
350-
351-
@Override
352-
public SourceLoader.SyntheticFieldLoader syntheticFieldLoader() {
353-
String value = fieldType().value();
354-
;
355-
if (value == null) {
356-
return SourceLoader.SyntheticFieldLoader.NOTHING;
357-
}
358-
return new SourceLoader.SyntheticFieldLoader() {
359-
@Override
360-
public Stream<Map.Entry<String, StoredFieldLoader>> storedFieldLoaders() {
361-
return Stream.of();
362-
}
363-
364-
@Override
365-
public DocValuesLoader docValuesLoader(LeafReader reader, int[] docIdsInLeaf) {
366-
return docId -> true;
367-
}
368-
369-
@Override
370-
public boolean hasValue() {
371-
return true;
372-
}
373-
374-
@Override
375-
public void write(XContentBuilder b) throws IOException {
376-
if (fieldType().value != null) {
377-
b.field(leafName(), fieldType().value);
378-
}
379-
}
380-
381-
@Override
382-
public void reset() {
383-
// NOOP
384-
}
385-
386-
@Override
387-
public String fieldName() {
388-
return fullPath();
389-
}
390-
};
345+
/*
346+
If there was no value in the document, synthetic source should not have the value too.
347+
This is consistent with stored source behavior and is important for scenarios
348+
like reindexing into an index that has a different value of this value in the mapping.
349+
350+
In order to do that we use fallback logic which implements exactly such logic (_source only contains value
351+
if it was in the original document).
352+
*/
353+
return SyntheticSourceMode.FALLBACK;
391354
}
392355
}

x-pack/plugin/mapper-constant-keyword/src/test/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapperTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,17 @@ public void testNullValueSyntheticSource() throws IOException {
327327
assertThat(syntheticSource(mapper, b -> {}), equalTo("{}"));
328328
}
329329

330+
public void testNoValueInDocumentSyntheticSource() throws IOException {
331+
DocumentMapper mapper = createDocumentMapper(syntheticSourceMapping(b -> {
332+
b.startObject("field");
333+
b.field("type", "constant_keyword");
334+
b.field("value", randomAlphaOfLength(5));
335+
b.endObject();
336+
}));
337+
338+
assertThat(syntheticSource(mapper, b -> {}), equalTo("{}"));
339+
}
340+
330341
@Override
331342
protected boolean supportsEmptyInputArray() {
332343
return false;

x-pack/plugin/mapper-constant-keyword/src/yamlRestTest/resources/rest-api-spec/test/20_synthetic_source.yml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
constant_keyword:
22
- requires:
3-
cluster_features: ["gte_v8.4.0"]
4-
reason: introduced in 8.4.0
3+
cluster_features: "gte_v8.15.0"
4+
reason: behavior change in 8.15
55

66
- do:
77
indices.create:
@@ -25,13 +25,35 @@ constant_keyword:
2525
body:
2626
kwd: foo
2727

28+
- do:
29+
index:
30+
index: test
31+
id: 2
32+
refresh: true
33+
body:
34+
kwd: foo
35+
const_kwd: bar
36+
2837
- do:
2938
search:
3039
index: test
3140
body:
3241
query:
3342
ids:
3443
values: [1]
44+
45+
- match:
46+
hits.hits.0._source:
47+
kwd: foo
48+
49+
- do:
50+
search:
51+
index: test
52+
body:
53+
query:
54+
ids:
55+
values: [2]
56+
3557
- match:
3658
hits.hits.0._source:
3759
kwd: foo

0 commit comments

Comments
 (0)