Skip to content

Commit d008ada

Browse files
authored
Change synthetic source logic for constant_keyword to not rely on fallback synthetic source (#119323)
1 parent 1632929 commit d008ada

File tree

2 files changed

+117
-10
lines changed

2 files changed

+117
-10
lines changed

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

+17-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package org.elasticsearch.xpack.constantkeyword.mapper;
99

10+
import org.apache.lucene.document.SortedNumericDocValuesField;
1011
import org.apache.lucene.index.IndexReader;
1112
import org.apache.lucene.index.TermsEnum;
1213
import org.apache.lucene.search.MatchAllDocsQuery;
@@ -41,6 +42,7 @@
4142
import org.elasticsearch.index.mapper.Mapper;
4243
import org.elasticsearch.index.mapper.MapperBuilderContext;
4344
import org.elasticsearch.index.mapper.MapperParsingException;
45+
import org.elasticsearch.index.mapper.SortedNumericDocValuesSyntheticFieldLoader;
4446
import org.elasticsearch.index.mapper.SourceLoader;
4547
import org.elasticsearch.index.mapper.ValueFetcher;
4648
import org.elasticsearch.index.query.QueryRewriteContext;
@@ -339,6 +341,12 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio
339341
+ "]"
340342
);
341343
}
344+
345+
if (context.mappingLookup().isSourceSynthetic()) {
346+
// Remember which documents had value in source so that it can be correctly
347+
// reconstructed in synthetic source
348+
context.doc().add(new SortedNumericDocValuesField(fieldType().name(), 1));
349+
}
342350
}
343351

344352
@Override
@@ -348,20 +356,19 @@ protected String contentType() {
348356

349357
@Override
350358
protected SyntheticSourceSupport syntheticSourceSupport() {
351-
String value = fieldType().value();
359+
String const_value = fieldType().value();
352360

353-
if (value == null) {
361+
if (const_value == null) {
354362
return new SyntheticSourceSupport.Native(SourceLoader.SyntheticFieldLoader.NOTHING);
355363
}
356364

357-
/*
358-
If there was no value in the document, synthetic source should not have the value too.
359-
This is consistent with stored source behavior and is important for scenarios
360-
like reindexing into an index that has a different value of this value in the mapping.
365+
var loader = new SortedNumericDocValuesSyntheticFieldLoader(fullPath(), leafName(), false) {
366+
@Override
367+
protected void writeValue(XContentBuilder b, long ignored) throws IOException {
368+
b.value(const_value);
369+
}
370+
};
361371

362-
In order to do that we use fallback logic which implements exactly such logic (_source only contains value
363-
if it was in the original document).
364-
*/
365-
return new SyntheticSourceSupport.Fallback();
372+
return new SyntheticSourceSupport.Native(loader);
366373
}
367374
}

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

+100
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,103 @@ constant_keyword:
5959
hits.hits.0._source:
6060
kwd: foo
6161
const_kwd: bar
62+
63+
---
64+
constant_keyword update value:
65+
- requires:
66+
cluster_features: [ "mapper.constant_keyword.synthetic_source_write_fix" ]
67+
reason: "Behavior fix"
68+
69+
- do:
70+
indices.create:
71+
index: test
72+
body:
73+
settings:
74+
index:
75+
mapping.source.mode: synthetic
76+
mappings:
77+
properties:
78+
const_kwd:
79+
type: constant_keyword
80+
kwd:
81+
type: keyword
82+
83+
- do:
84+
index:
85+
index: test
86+
id: 1
87+
refresh: true
88+
body:
89+
kwd: foo
90+
91+
- do:
92+
index:
93+
index: test
94+
id: 2
95+
refresh: true
96+
body:
97+
const_kwd: bar
98+
99+
- do:
100+
index:
101+
index: test
102+
id: 3
103+
refresh: true
104+
body:
105+
kwd: foo
106+
107+
- do:
108+
index:
109+
index: test
110+
id: 4
111+
refresh: true
112+
body:
113+
const_kwd: bar
114+
115+
- do:
116+
search:
117+
index: test
118+
body:
119+
query:
120+
ids:
121+
values: [1]
122+
123+
- match:
124+
hits.hits.0._source:
125+
kwd: foo
126+
127+
- do:
128+
search:
129+
index: test
130+
body:
131+
query:
132+
ids:
133+
values: [2]
134+
135+
- match:
136+
hits.hits.0._source:
137+
const_kwd: bar
138+
139+
- do:
140+
search:
141+
index: test
142+
body:
143+
query:
144+
ids:
145+
values: [3]
146+
147+
- match:
148+
hits.hits.0._source:
149+
kwd: foo
150+
151+
- do:
152+
search:
153+
index: test
154+
body:
155+
query:
156+
ids:
157+
values: [4]
158+
159+
- match:
160+
hits.hits.0._source:
161+
const_kwd: bar

0 commit comments

Comments
 (0)