Skip to content

Commit

Permalink
[8.15] Change synthetic source logic for constant_keyword (#117182) (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
lkts authored Nov 21, 2024
1 parent b954f9e commit b10896b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 48 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/117182.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 117182
summary: Change synthetic source logic for `constant_keyword`
area: Mapping
type: bug
issues:
- 117083
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package org.elasticsearch.xpack.constantkeyword.mapper;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
Expand Down Expand Up @@ -41,7 +40,6 @@
import org.elasticsearch.index.mapper.Mapper;
import org.elasticsearch.index.mapper.MapperBuilderContext;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.SourceLoader;
import org.elasticsearch.index.mapper.ValueFetcher;
import org.elasticsearch.index.query.QueryRewriteContext;
import org.elasticsearch.index.query.SearchExecutionContext;
Expand All @@ -57,7 +55,6 @@
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

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

@Override
protected SyntheticSourceMode syntheticSourceMode() {
return SyntheticSourceMode.NATIVE;
}

@Override
public SourceLoader.SyntheticFieldLoader syntheticFieldLoader() {
String value = fieldType().value();
;
if (value == null) {
return SourceLoader.SyntheticFieldLoader.NOTHING;
}
return new SourceLoader.SyntheticFieldLoader() {
@Override
public Stream<Map.Entry<String, StoredFieldLoader>> storedFieldLoaders() {
return Stream.of();
}

@Override
public DocValuesLoader docValuesLoader(LeafReader reader, int[] docIdsInLeaf) {
return docId -> true;
}

@Override
public boolean hasValue() {
return true;
}

@Override
public void write(XContentBuilder b) throws IOException {
if (fieldType().value != null) {
b.field(leafName(), fieldType().value);
}
}

@Override
public void reset() {
// NOOP
}

@Override
public String fieldName() {
return fullPath();
}
};
/*
If there was no value in the document, synthetic source should not have the value too.
This is consistent with stored source behavior and is important for scenarios
like reindexing into an index that has a different value of this value in the mapping.
In order to do that we use fallback logic which implements exactly such logic (_source only contains value
if it was in the original document).
*/
return SyntheticSourceMode.FALLBACK;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,17 @@ public void testNullValueSyntheticSource() throws IOException {
assertThat(syntheticSource(mapper, b -> {}), equalTo("{}"));
}

public void testNoValueInDocumentSyntheticSource() throws IOException {
DocumentMapper mapper = createDocumentMapper(syntheticSourceMapping(b -> {
b.startObject("field");
b.field("type", "constant_keyword");
b.field("value", randomAlphaOfLength(5));
b.endObject();
}));

assertThat(syntheticSource(mapper, b -> {}), equalTo("{}"));
}

@Override
protected boolean supportsEmptyInputArray() {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
constant_keyword:
- requires:
cluster_features: ["gte_v8.4.0"]
reason: introduced in 8.4.0
cluster_features: "gte_v8.15.0"
reason: behavior change in 8.15

- do:
indices.create:
Expand All @@ -25,13 +25,35 @@ constant_keyword:
body:
kwd: foo

- do:
index:
index: test
id: 2
refresh: true
body:
kwd: foo
const_kwd: bar

- do:
search:
index: test
body:
query:
ids:
values: [1]

- match:
hits.hits.0._source:
kwd: foo

- do:
search:
index: test
body:
query:
ids:
values: [2]

- match:
hits.hits.0._source:
kwd: foo
Expand Down

0 comments on commit b10896b

Please sign in to comment.