Skip to content

Commit e60db9f

Browse files
authored
[8.16] Change synthetic source logic for constant_keyword (#117182) (#117210)
* 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) * Fix test * Fix test
1 parent 93ce500 commit e60db9f

File tree

4 files changed

+47
-38
lines changed

4 files changed

+47
-38
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: 8 additions & 36 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;
@@ -57,7 +56,6 @@
5756
import java.util.Locale;
5857
import java.util.Map;
5958
import java.util.Objects;
60-
import java.util.stream.Stream;
6159

6260
/**
6361
* A {@link FieldMapper} that assigns every document the same value.
@@ -352,40 +350,14 @@ protected SyntheticSourceSupport syntheticSourceSupport() {
352350
return new SyntheticSourceSupport.Native(SourceLoader.SyntheticFieldLoader.NOTHING);
353351
}
354352

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

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
@@ -333,6 +333,17 @@ public void testNullValueSyntheticSource() throws IOException {
333333
assertThat(syntheticSource(mapper, b -> {}), equalTo("{}"));
334334
}
335335

336+
public void testNoValueInDocumentSyntheticSource() throws IOException {
337+
DocumentMapper mapper = createDocumentMapper(syntheticSourceMapping(b -> {
338+
b.startObject("field");
339+
b.field("type", "constant_keyword");
340+
b.field("value", randomAlphaOfLength(5));
341+
b.endObject();
342+
}));
343+
344+
assertThat(syntheticSource(mapper, b -> {}), equalTo("{}"));
345+
}
346+
336347
@Override
337348
protected boolean supportsEmptyInputArray() {
338349
return false;

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

Lines changed: 22 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.16.0"
4+
reason: behavior change in 8.16
55

66
- do:
77
indices.create:
@@ -25,13 +25,33 @@ 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+
- match:
45+
hits.hits.0._source:
46+
kwd: foo
47+
48+
- do:
49+
search:
50+
index: test
51+
body:
52+
query:
53+
ids:
54+
values: [2]
3555
- match:
3656
hits.hits.0._source:
3757
kwd: foo

0 commit comments

Comments
 (0)