Skip to content

Commit

Permalink
Enable synthetic source support on constant keyword fields (#88603)
Browse files Browse the repository at this point in the history
This commit implements synthetic source support on constant keyword fields,
which will now always emit their value as part of the retrieved source.
  • Loading branch information
romseygeek authored Jul 19, 2022
1 parent 0d7e4c2 commit 2f5ec51
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/88603.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 88603
summary: Enable synthetic source support on constant keyword fields
area: Mapping
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,10 @@ public final void testSyntheticSource() throws IOException {
assertThat(syntheticSource(mapper, b -> b.field("field", syntheticSourceExample.inputValue)), equalTo(expected));
}

protected boolean supportsEmptyInputArray() {
return true;
}

public final void testSyntheticSourceMany() throws IOException {
int maxValues = randomBoolean() ? 1 : 5;
SyntheticSourceSupport support = syntheticSourceSupport();
Expand All @@ -810,7 +814,7 @@ public final void testSyntheticSourceMany() throws IOException {
)
) {
for (int i = 0; i < count; i++) {
if (rarely()) {
if (rarely() && supportsEmptyInputArray()) {
expected[i] = "{}";
iw.addDocument(mapper.parse(source(b -> b.startArray("field").endArray())).rootDoc());
continue;
Expand Down Expand Up @@ -868,6 +872,7 @@ public final void testSyntheticSourceInObject() throws IOException {
}

public final void testSyntheticEmptyList() throws IOException {
assumeTrue("Field does not support [] as input", supportsEmptyInputArray());
SyntheticSourceExample syntheticSourceExample = syntheticSourceSupport().example(5);
DocumentMapper mapper = createDocumentMapper(syntheticSourceMapping(b -> {
b.startObject("field");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.List;

import static org.elasticsearch.index.mapper.MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;

public class ConstantKeywordFieldMapperTests extends MapperTestCase {
Expand Down Expand Up @@ -207,11 +208,39 @@ protected boolean allowsNullValues() {

@Override
protected SyntheticSourceSupport syntheticSourceSupport() {
throw new AssumptionViolatedException("not supported");
String value = randomUnicodeOfLength(5);
return new SyntheticSourceSupport() {
@Override
public SyntheticSourceExample example(int maxValues) {
return new SyntheticSourceExample(value, value, b -> {
b.field("type", "constant_keyword");
b.field("value", value);
});
}

@Override
public List<SyntheticSourceInvalidExample> invalidExample() throws IOException {
throw new AssumptionViolatedException("copy_to on constant_keyword not supported");
}
};
}

@Override
protected IngestScriptSupport ingestScriptSupport() {
throw new AssumptionViolatedException("not supported");
}

public void testNullValueSyntheticSource() throws IOException {
DocumentMapper mapper = createDocumentMapper(syntheticSourceMapping(b -> {
b.startObject("field");
b.field("type", "constant_keyword");
b.endObject();
}));
assertThat(syntheticSource(mapper, b -> {}), equalTo("{}"));
}

@Override
protected boolean supportsEmptyInputArray() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
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.SearchExecutionContext;
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
Expand Down Expand Up @@ -307,4 +308,25 @@ protected String contentType() {
return CONTENT_TYPE;
}

@Override
public SourceLoader.SyntheticFieldLoader syntheticFieldLoader() {
return (reader, docIdsInLeaf) -> new SourceLoader.SyntheticFieldLoader.Leaf() {
@Override
public boolean empty() {
return fieldType().value == null;
}

@Override
public boolean advanceToDoc(int docId) throws IOException {
return fieldType().value != null;
}

@Override
public void write(XContentBuilder b) throws IOException {
if (fieldType().value != null) {
b.field(simpleName(), fieldType().value);
}
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
constant_keyword:
- skip:
version: " - 8.3.99"
reason: introduced in 8.4.0

- do:
indices.create:
index: test
body:
mappings:
_source:
mode: synthetic
properties:
const_kwd:
type: constant_keyword
value: bar
kwd:
type: keyword

- do:
index:
index: test
id: 1
refresh: true
body:
kwd: foo

- do:
search:
index: test
body:
query:
ids:
values: [1]
- match:
hits.hits.0._source:
kwd: foo
const_kwd: bar

0 comments on commit 2f5ec51

Please sign in to comment.