Skip to content

Commit c05fdb3

Browse files
committed
Add IgnoredSourceFieldLoaderTests
1 parent 0834dcd commit c05fdb3

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.index.fieldvisitor;
11+
12+
import org.apache.lucene.document.Document;
13+
import org.apache.lucene.document.StoredField;
14+
import org.apache.lucene.index.DirectoryReader;
15+
import org.apache.lucene.index.IndexWriter;
16+
import org.apache.lucene.store.Directory;
17+
import org.apache.lucene.util.BytesRef;
18+
import org.elasticsearch.common.lucene.Lucene;
19+
import org.elasticsearch.index.mapper.BlockLoader;
20+
import org.elasticsearch.index.mapper.IgnoredFieldsSpec;
21+
import org.elasticsearch.index.mapper.IgnoredSourceFieldMapper;
22+
import org.elasticsearch.search.fetch.StoredFieldsSpec;
23+
import org.elasticsearch.test.ESTestCase;
24+
25+
import java.io.IOException;
26+
import java.util.List;
27+
import java.util.Map;
28+
import java.util.Set;
29+
import java.util.function.Consumer;
30+
31+
import static org.hamcrest.Matchers.anEmptyMap;
32+
import static org.hamcrest.Matchers.containsInAnyOrder;
33+
import static org.hamcrest.Matchers.equalTo;
34+
import static org.hamcrest.Matchers.hasEntry;
35+
import static org.hamcrest.Matchers.hasKey;
36+
import static org.hamcrest.Matchers.not;
37+
38+
public class IgnoredSourceFieldLoaderTests extends ESTestCase {
39+
public void testSupports() {
40+
assertTrue(
41+
IgnoredSourceFieldLoader.supports(
42+
new BlockLoader.FieldsSpec(
43+
StoredFieldsSpec.NO_REQUIREMENTS,
44+
new IgnoredFieldsSpec(Set.of("foo"), IgnoredSourceFieldMapper.IgnoredSourceFormat.PER_FIELD_IGNORED_SOURCE)
45+
)
46+
)
47+
);
48+
49+
assertTrue(
50+
IgnoredSourceFieldLoader.supports(
51+
new BlockLoader.FieldsSpec(
52+
StoredFieldsSpec.NO_REQUIREMENTS,
53+
new IgnoredFieldsSpec(Set.of(), IgnoredSourceFieldMapper.IgnoredSourceFormat.PER_FIELD_IGNORED_SOURCE)
54+
)
55+
)
56+
);
57+
58+
assertFalse(
59+
IgnoredSourceFieldLoader.supports(
60+
new BlockLoader.FieldsSpec(
61+
StoredFieldsSpec.NEEDS_SOURCE,
62+
new IgnoredFieldsSpec(Set.of("foo"), IgnoredSourceFieldMapper.IgnoredSourceFormat.PER_FIELD_IGNORED_SOURCE)
63+
)
64+
)
65+
);
66+
67+
assertFalse(
68+
IgnoredSourceFieldLoader.supports(new BlockLoader.FieldsSpec(StoredFieldsSpec.NO_REQUIREMENTS, IgnoredFieldsSpec.NONE))
69+
);
70+
}
71+
72+
public void testLoadSingle() throws IOException {
73+
BytesRef value = new BytesRef("lorem ipsum");
74+
Document doc = new Document();
75+
doc.add(new StoredField("_ignored_source.foo", value));
76+
testLoader(doc, Set.of("foo"), storedFields -> {
77+
assertThat(storedFields, hasEntry(equalTo("_ignored_source.foo"), containsInAnyOrder(value)));
78+
});
79+
}
80+
81+
public void testLoaderNone() throws IOException {
82+
testLoader(new Document(), Set.of(), storedFields -> { assertThat(storedFields, anEmptyMap()); });
83+
}
84+
85+
public void testLoadMultiple() throws IOException {
86+
BytesRef fooValue = new BytesRef("lorem ipsum");
87+
BytesRef barValue = new BytesRef("dolor sit amet");
88+
Document doc = new Document();
89+
doc.add(new StoredField("_ignored_source.foo", fooValue));
90+
doc.add(new StoredField("_ignored_source.bar", barValue));
91+
testLoader(doc, Set.of("foo", "bar"), storedFields -> {
92+
assertThat(storedFields, hasEntry(equalTo("foo"), containsInAnyOrder(fooValue)));
93+
assertThat(storedFields, hasEntry(equalTo("bar"), containsInAnyOrder(barValue)));
94+
});
95+
}
96+
97+
public void testLoadSubset() throws IOException {
98+
BytesRef fooValue = new BytesRef("lorem ipsum");
99+
BytesRef barValue = new BytesRef("dolor sit amet");
100+
101+
Document doc = new Document();
102+
doc.add(new StoredField("_ignored_source.foo", fooValue));
103+
doc.add(new StoredField("_ignored_source.bar", barValue));
104+
105+
testLoader(doc, Set.of("foo"), storedFields -> {
106+
assertThat(storedFields, hasEntry(equalTo("foo"), containsInAnyOrder(fooValue)));
107+
assertThat(storedFields, not(hasKey("bar")));
108+
});
109+
}
110+
111+
private void testLoader(Document doc, Set<String> fieldsToLoad, Consumer<Map<String, List<Object>>> storedFieldsConsumer)
112+
throws IOException {
113+
try (Directory dir = newDirectory(); IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(Lucene.STANDARD_ANALYZER))) {
114+
BlockLoader.FieldsSpec spec = new BlockLoader.FieldsSpec(
115+
StoredFieldsSpec.NO_REQUIREMENTS,
116+
new IgnoredFieldsSpec(fieldsToLoad, IgnoredSourceFieldMapper.IgnoredSourceFormat.PER_FIELD_IGNORED_SOURCE)
117+
);
118+
assertTrue(IgnoredSourceFieldLoader.supports(spec));
119+
iw.addDocument(doc);
120+
try (DirectoryReader reader = DirectoryReader.open(iw)) {
121+
IgnoredSourceFieldLoader loader = new IgnoredSourceFieldLoader(spec, false);
122+
var leafLoader = loader.getLoader(reader.leaves().getFirst(), new int[] { 0 });
123+
leafLoader.advanceTo(0);
124+
}
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)