|
8 | 8 |
|
9 | 9 | import org.apache.lucene.document.StoredField;
|
10 | 10 | import org.apache.lucene.index.DirectoryReader;
|
| 11 | +import org.apache.lucene.index.LeafReaderContext; |
11 | 12 | import org.apache.lucene.index.RandomIndexWriter;
|
| 13 | +import org.apache.lucene.search.Collector; |
12 | 14 | 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; |
13 | 19 | import org.apache.lucene.store.Directory;
|
14 | 20 | import org.apache.lucene.util.BytesRef;
|
| 21 | +import org.elasticsearch.Version; |
| 22 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
15 | 23 | import org.elasticsearch.common.settings.Settings;
|
| 24 | +import org.elasticsearch.index.IndexSettings; |
| 25 | +import org.elasticsearch.index.fielddata.SortedBinaryDocValues; |
16 | 26 | import org.elasticsearch.index.mapper.MapperService;
|
17 | 27 | import org.elasticsearch.index.query.QueryShardContext;
|
18 | 28 | import org.elasticsearch.painless.PainlessPlugin;
|
|
25 | 35 | import org.elasticsearch.xpack.runtimefields.RuntimeFields;
|
26 | 36 | import org.elasticsearch.xpack.runtimefields.RuntimeFieldsPainlessExtension;
|
27 | 37 | import org.elasticsearch.xpack.runtimefields.StringScriptFieldScript;
|
| 38 | +import org.elasticsearch.xpack.runtimefields.fielddata.ScriptBinaryFieldData; |
28 | 39 |
|
29 | 40 | import java.io.IOException;
|
| 41 | +import java.util.ArrayList; |
30 | 42 | import java.util.List;
|
31 | 43 |
|
32 | 44 | import static java.util.Collections.emptyMap;
|
|
35 | 47 | import static org.mockito.Mockito.when;
|
36 | 48 |
|
37 | 49 | 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 | + |
38 | 94 | public void testTermQuery() throws IOException {
|
39 | 95 | try (Directory directory = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
|
40 | 96 | iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": 1}"))));
|
|
0 commit comments