Skip to content

Commit 41c3769

Browse files
authored
Change synthetic source logic for constant_keyword to not rely on fallback synthetic source (#119323) (#119329)
1 parent 43620b2 commit 41c3769

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

Lines changed: 17 additions & 10 deletions
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;
@@ -40,6 +41,7 @@
4041
import org.elasticsearch.index.mapper.Mapper;
4142
import org.elasticsearch.index.mapper.MapperBuilderContext;
4243
import org.elasticsearch.index.mapper.MapperParsingException;
44+
import org.elasticsearch.index.mapper.SortedNumericDocValuesSyntheticFieldLoader;
4345
import org.elasticsearch.index.mapper.SourceLoader;
4446
import org.elasticsearch.index.mapper.ValueFetcher;
4547
import org.elasticsearch.index.query.QueryRewriteContext;
@@ -335,6 +337,12 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio
335337
+ "]"
336338
);
337339
}
340+
341+
if (context.mappingLookup().isSourceSynthetic()) {
342+
// Remember which documents had value in source so that it can be correctly
343+
// reconstructed in synthetic source
344+
context.doc().add(new SortedNumericDocValuesField(fieldType().name(), 1));
345+
}
338346
}
339347

340348
@Override
@@ -344,20 +352,19 @@ protected String contentType() {
344352

345353
@Override
346354
protected SyntheticSourceSupport syntheticSourceSupport() {
347-
String value = fieldType().value();
355+
String const_value = fieldType().value();
348356

349-
if (value == null) {
357+
if (const_value == null) {
350358
return new SyntheticSourceSupport.Native(SourceLoader.SyntheticFieldLoader.NOTHING);
351359
}
352360

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.
361+
var loader = new SortedNumericDocValuesSyntheticFieldLoader(fullPath(), leafName(), false) {
362+
@Override
363+
protected void writeValue(XContentBuilder b, long ignored) throws IOException {
364+
b.value(const_value);
365+
}
366+
};
357367

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();
368+
return new SyntheticSourceSupport.Native(loader);
362369
}
363370
}

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

Lines changed: 100 additions & 0 deletions
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)