Skip to content

Commit 3a1bc05

Browse files
authored
Change synthetic source logic for constant_keyword (#117182)
* Change synthetic source logic for constant_keyword * Update docs/changelog/117182.yaml
1 parent 53e5fab commit 3a1bc05

File tree

3 files changed

+25
-36
lines changed

3 files changed

+25
-36
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;
@@ -58,7 +57,6 @@
5857
import java.util.Locale;
5958
import java.util.Map;
6059
import java.util.Objects;
61-
import java.util.stream.Stream;
6260

6361
/**
6462
* A {@link FieldMapper} that assigns every document the same value.
@@ -356,40 +354,14 @@ protected SyntheticSourceSupport syntheticSourceSupport() {
356354
return new SyntheticSourceSupport.Native(SourceLoader.SyntheticFieldLoader.NOTHING);
357355
}
358356

359-
var loader = new SourceLoader.SyntheticFieldLoader() {
360-
@Override
361-
public Stream<Map.Entry<String, StoredFieldLoader>> storedFieldLoaders() {
362-
return Stream.of();
363-
}
364-
365-
@Override
366-
public DocValuesLoader docValuesLoader(LeafReader reader, int[] docIdsInLeaf) {
367-
return docId -> true;
368-
}
369-
370-
@Override
371-
public boolean hasValue() {
372-
return true;
373-
}
374-
375-
@Override
376-
public void write(XContentBuilder b) throws IOException {
377-
if (fieldType().value != null) {
378-
b.field(leafName(), fieldType().value);
379-
}
380-
}
381-
382-
@Override
383-
public void reset() {
384-
// NOOP
385-
}
386-
387-
@Override
388-
public String fieldName() {
389-
return fullPath();
390-
}
391-
};
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.
392361
393-
return new SyntheticSourceSupport.Native(loader);
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();
394366
}
395367
}

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 = createSytheticSourceMapperService(mapping(b -> {
338+
b.startObject("field");
339+
b.field("type", "constant_keyword");
340+
b.field("value", randomAlphaOfLength(5));
341+
b.endObject();
342+
})).documentMapper();
343+
344+
assertThat(syntheticSource(mapper, b -> {}), equalTo("{}"));
345+
}
346+
336347
@Override
337348
protected boolean supportsEmptyInputArray() {
338349
return false;

0 commit comments

Comments
 (0)