Skip to content

Commit daf23f6

Browse files
authored
Small fixes to flattened field value fetching. (#63443)
* Remove FlatObjectFieldTypeTests, as it's redundant. * Do not apply null_value when fetching root-level values. * Remove a TODO in favor of opening an issue.
1 parent 6dfb376 commit daf23f6

File tree

3 files changed

+20
-48
lines changed

3 files changed

+20
-48
lines changed

x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapper.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ public Builder store(boolean store) {
177177

178178
@Override
179179
public FlatObjectFieldMapper build(BuilderContext context) {
180-
MappedFieldType ft
181-
= new RootFlatObjectFieldType(buildFullName(context), indexed, hasDocValues, meta, splitQueriesOnWhitespace, nullValue);
180+
MappedFieldType ft = new RootFlatObjectFieldType(
181+
buildFullName(context), indexed, hasDocValues, meta, splitQueriesOnWhitespace);
182182
if (eagerGlobalOrdinals) {
183183
ft.setEagerGlobalOrdinals(true);
184184
}
@@ -323,7 +323,7 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, S
323323

324324
@Override
325325
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
326-
throw new UnsupportedOperationException(); // TODO can we implement this?
326+
throw new UnsupportedOperationException();
327327
}
328328
}
329329

@@ -438,15 +438,13 @@ public IndexFieldData<?> build(IndexFieldDataCache cache, CircuitBreakerService
438438
*/
439439
public static final class RootFlatObjectFieldType extends StringFieldType {
440440
private final boolean splitQueriesOnWhitespace;
441-
private final String nullValue;
442441

443442
public RootFlatObjectFieldType(String name, boolean indexed, boolean hasDocValues, Map<String, String> meta,
444-
boolean splitQueriesOnWhitespace, String nullValue) {
443+
boolean splitQueriesOnWhitespace) {
445444
super(name, indexed, false, hasDocValues,
446445
splitQueriesOnWhitespace ? TextSearchInfo.WHITESPACE_MATCH_ONLY : TextSearchInfo.SIMPLE_MATCH_ONLY, meta);
447446
this.splitQueriesOnWhitespace = splitQueriesOnWhitespace;
448447
setIndexAnalyzer(Lucene.KEYWORD_ANALYZER);
449-
this.nullValue = nullValue;
450448
}
451449

452450
@Override
@@ -474,7 +472,7 @@ public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searc
474472
if (format != null) {
475473
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
476474
}
477-
return new SourceValueFetcher(name(), mapperService, nullValue) {
475+
return new SourceValueFetcher(name(), mapperService) {
478476
@Override
479477
protected Object parseSourceValue(Object value) {
480478
return value;

x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldTypeTests.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
import org.elasticsearch.index.mapper.FieldTypeTestCase;
2323
import org.elasticsearch.xpack.flattened.mapper.FlatObjectFieldMapper.RootFlatObjectFieldType;
2424

25+
import java.io.IOException;
2526
import java.util.Collections;
27+
import java.util.List;
28+
import java.util.Map;
2629

2730
public class RootFlatObjectFieldTypeTests extends FieldTypeTestCase {
2831

2932
private static RootFlatObjectFieldType createDefaultFieldType() {
30-
return new RootFlatObjectFieldType("field", true, true, Collections.emptyMap(), false, null);
33+
return new RootFlatObjectFieldType("field", true, true, Collections.emptyMap(), false);
3134
}
3235

3336
public void testValueForDisplay() {
@@ -49,19 +52,19 @@ public void testTermQuery() {
4952

5053

5154
RootFlatObjectFieldType unsearchable = new RootFlatObjectFieldType("field", false, true,
52-
Collections.emptyMap(), false, null);
55+
Collections.emptyMap(), false);
5356
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
5457
() -> unsearchable.termQuery("field", null));
5558
assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage());
5659
}
5760

5861
public void testExistsQuery() {
59-
RootFlatObjectFieldType ft = new RootFlatObjectFieldType("field", true, false, Collections.emptyMap(), false, null);
62+
RootFlatObjectFieldType ft = new RootFlatObjectFieldType("field", true, false, Collections.emptyMap(), false);
6063
assertEquals(
6164
new TermQuery(new Term(FieldNamesFieldMapper.NAME, new BytesRef("field"))),
6265
ft.existsQuery(null));
6366

64-
RootFlatObjectFieldType withDv = new RootFlatObjectFieldType("field", true, true, Collections.emptyMap(), false, null);
67+
RootFlatObjectFieldType withDv = new RootFlatObjectFieldType("field", true, true, Collections.emptyMap(), false);
6568
assertEquals(new DocValuesFieldExistsQuery("field"), withDv.existsQuery(null));
6669
}
6770

@@ -122,4 +125,12 @@ public void testWildcardQuery() {
122125
assertEquals("[wildcard] queries cannot be executed when 'search.allow_expensive_queries' is set to false.",
123126
ee.getMessage());
124127
}
128+
129+
public void testFetchSourceValue() throws IOException {
130+
Map<String, Object> sourceValue = Map.of("key", "value");
131+
RootFlatObjectFieldType ft = createDefaultFieldType();
132+
133+
assertEquals(List.of(sourceValue), fetchSourceValue(ft, sourceValue));
134+
assertEquals(List.of(), fetchSourceValue(ft, null));
135+
}
125136
}

0 commit comments

Comments
 (0)