Skip to content

Commit 46fcc58

Browse files
authored
Add tests for keyword script field's fielddata (#59523)
Adds tests for the `keyword` typed `script` field's fielddata implementation. One unit test which runs quickly and an integretion test for fetching and an integration test for the `terms` agg.
1 parent 4eb18b2 commit 46fcc58

File tree

4 files changed

+87
-5
lines changed

4 files changed

+87
-5
lines changed

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/fielddata/ScriptBinaryFieldData.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public Builder(StringScriptFieldScript.Factory scriptFactory) {
5151
}
5252

5353
@Override
54-
public IndexFieldData<?> build(
54+
public ScriptBinaryFieldData build(
5555
IndexSettings indexSettings,
5656
MappedFieldType fieldType,
5757
IndexFieldDataCache cache,
@@ -130,7 +130,7 @@ public void clear() {
130130

131131
}
132132

133-
static class ScriptBinaryLeafFieldData implements LeafFieldData {
133+
public static class ScriptBinaryLeafFieldData implements LeafFieldData {
134134
private final ScriptBinaryDocValues scriptBinaryDocValues;
135135

136136
ScriptBinaryLeafFieldData(ScriptBinaryDocValues scriptBinaryDocValues) {

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/mapper/RuntimeKeywordMappedFieldType.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88

99
import org.apache.lucene.search.Query;
1010
import org.apache.lucene.util.BytesRef;
11-
import org.elasticsearch.common.xcontent.ToXContent.Params;
1211
import org.elasticsearch.common.lucene.BytesRefs;
12+
import org.elasticsearch.common.xcontent.ToXContent.Params;
1313
import org.elasticsearch.common.xcontent.XContentBuilder;
14-
import org.elasticsearch.index.fielddata.IndexFieldData;
1514
import org.elasticsearch.index.mapper.MappedFieldType;
1615
import org.elasticsearch.index.mapper.TextSearchInfo;
1716
import org.elasticsearch.index.query.QueryShardContext;
@@ -53,7 +52,7 @@ public String typeName() {
5352
}
5453

5554
@Override
56-
public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName) {
55+
public ScriptBinaryFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName) {
5756
// TODO once we get SearchLookup as an argument, we can already call scriptFactory.newFactory here and pass through the result
5857
return new ScriptBinaryFieldData.Builder(scriptFactory);
5958
}

x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/mapper/RuntimeKeywordMappedFieldTypeTests.java

+56
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,21 @@
88

99
import org.apache.lucene.document.StoredField;
1010
import org.apache.lucene.index.DirectoryReader;
11+
import org.apache.lucene.index.LeafReaderContext;
1112
import org.apache.lucene.index.RandomIndexWriter;
13+
import org.apache.lucene.search.Collector;
1214
import org.apache.lucene.search.IndexSearcher;
15+
import org.apache.lucene.search.LeafCollector;
16+
import org.apache.lucene.search.MatchAllDocsQuery;
17+
import org.apache.lucene.search.Scorable;
18+
import org.apache.lucene.search.ScoreMode;
1319
import org.apache.lucene.store.Directory;
1420
import org.apache.lucene.util.BytesRef;
21+
import org.elasticsearch.Version;
22+
import org.elasticsearch.cluster.metadata.IndexMetadata;
1523
import org.elasticsearch.common.settings.Settings;
24+
import org.elasticsearch.index.IndexSettings;
25+
import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
1626
import org.elasticsearch.index.mapper.MapperService;
1727
import org.elasticsearch.index.query.QueryShardContext;
1828
import org.elasticsearch.painless.PainlessPlugin;
@@ -25,8 +35,10 @@
2535
import org.elasticsearch.xpack.runtimefields.RuntimeFields;
2636
import org.elasticsearch.xpack.runtimefields.RuntimeFieldsPainlessExtension;
2737
import org.elasticsearch.xpack.runtimefields.StringScriptFieldScript;
38+
import org.elasticsearch.xpack.runtimefields.fielddata.ScriptBinaryFieldData;
2839

2940
import java.io.IOException;
41+
import java.util.ArrayList;
3042
import java.util.List;
3143

3244
import static java.util.Collections.emptyMap;
@@ -35,6 +47,50 @@
3547
import static org.mockito.Mockito.when;
3648

3749
public class RuntimeKeywordMappedFieldTypeTests extends ESTestCase {
50+
public void testDocValues() throws IOException {
51+
try (Directory directory = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
52+
iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": [1]}"))));
53+
iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": [2, 1]}"))));
54+
List<String> results = new ArrayList<>();
55+
try (DirectoryReader reader = iw.getReader()) {
56+
IndexSearcher searcher = newSearcher(reader);
57+
RuntimeKeywordMappedFieldType ft = build("for (def v : source.foo) {value(v.toString())}");
58+
IndexMetadata imd = IndexMetadata.builder("test")
59+
.settings(Settings.builder().put("index.version.created", Version.CURRENT))
60+
.numberOfShards(1)
61+
.numberOfReplicas(1)
62+
.build();
63+
ScriptBinaryFieldData ifd = ft.fielddataBuilder("test").build(new IndexSettings(imd, Settings.EMPTY), ft, null, null, null);
64+
ifd.setSearchLookup(mockContext().lookup());
65+
searcher.search(new MatchAllDocsQuery(), new Collector() {
66+
@Override
67+
public ScoreMode scoreMode() {
68+
return ScoreMode.COMPLETE_NO_SCORES;
69+
}
70+
71+
@Override
72+
public LeafCollector getLeafCollector(LeafReaderContext context) throws IOException {
73+
SortedBinaryDocValues dv = ifd.load(context).getBytesValues();
74+
return new LeafCollector() {
75+
@Override
76+
public void setScorer(Scorable scorer) throws IOException {}
77+
78+
@Override
79+
public void collect(int doc) throws IOException {
80+
if (dv.advanceExact(doc)) {
81+
for (int i = 0; i < dv.docValueCount(); i++) {
82+
results.add(dv.nextValue().utf8ToString());
83+
}
84+
}
85+
}
86+
};
87+
}
88+
});
89+
assertThat(results, equalTo(List.of("1", "1", "2")));
90+
}
91+
}
92+
}
93+
3894
public void testTermQuery() throws IOException {
3995
try (Directory directory = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
4096
iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": 1}"))));

x-pack/plugin/src/test/resources/rest-api-spec/test/runtime_fields/10_keyword.yml

+27
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,33 @@ setup:
4040
{"index":{}}
4141
{"timestamp": 1516297294000, "temperature": 202, "voltage": 4.0, "node": "c"}
4242
43+
---
44+
"docvalue_fields":
45+
- do:
46+
search:
47+
index: sensor
48+
body:
49+
sort: timestamp
50+
docvalue_fields: [day_of_week]
51+
- match: {hits.total.value: 6}
52+
- match: {hits.hits.0.fields.day_of_week: [Thursday] }
53+
54+
---
55+
"terms agg":
56+
- do:
57+
search:
58+
index: sensor
59+
body:
60+
aggs:
61+
dow:
62+
terms:
63+
field: day_of_week
64+
- match: {hits.total.value: 6}
65+
- match: {aggregations.dow.buckets.0.key: Friday}
66+
- match: {aggregations.dow.buckets.0.doc_count: 1}
67+
- match: {aggregations.dow.buckets.1.key: Monday}
68+
- match: {aggregations.dow.buckets.1.doc_count: 1}
69+
4370
---
4471
"term query":
4572
- do:

0 commit comments

Comments
 (0)