Skip to content

Commit dcc433c

Browse files
committed
Fix UOE when fetching flattened field (#64241)
The new fields option allows to fetch the value of all fields in the mapping. However, internal fields that are used by some field mappers are also shown when concrete fields retrieved through a pattern (`*` or `foo*`). We have a [long term plan](#63446) to hide these fields in field_caps and from pattern resolution so this change is just a hot fix to ensure that they don't break the retrieval in the meantime. The `flattened._keyed field will show up as an empty field when using a pattern that match the flattened field. Relates #63446
1 parent dfc8ae4 commit dcc433c

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.elasticsearch.search.sort.SortOrder;
5656

5757
import java.io.IOException;
58+
import java.util.Collections;
5859
import java.util.Iterator;
5960
import java.util.List;
6061
import java.util.Map;
@@ -328,7 +329,8 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, S
328329

329330
@Override
330331
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
331-
throw new UnsupportedOperationException();
332+
// This is an internal field but it can match a field pattern so we return an empty list.
333+
return lookup -> Collections.emptyList();
332334
}
333335
}
334336

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

+10
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
import org.elasticsearch.index.mapper.FieldTypeTestCase;
2121
import org.elasticsearch.xpack.flattened.mapper.FlatObjectFieldMapper.KeyedFlatObjectFieldType;
2222

23+
import java.io.IOException;
2324
import java.util.ArrayList;
2425
import java.util.Collections;
2526
import java.util.List;
27+
import java.util.Map;
2628

2729
public class KeyedFlatObjectFieldTypeTests extends FieldTypeTestCase {
2830

@@ -151,4 +153,12 @@ public void testWildcardQuery() {
151153
() -> ft.wildcardQuery("valu*", null, false, randomMockShardContext()));
152154
assertEquals("[wildcard] queries are not currently supported on keyed [flattened] fields.", e.getMessage());
153155
}
156+
157+
public void testFetchIsEmpty() throws IOException {
158+
Map<String, Object> sourceValue = Map.of("key", "value");
159+
KeyedFlattenedFieldType ft = createFieldType();
160+
161+
assertEquals(List.of(), fetchSourceValue(ft, sourceValue));
162+
assertEquals(List.of(), fetchSourceValue(ft, null));
163+
}
154164
}

x-pack/plugin/src/test/resources/rest-api-spec/test/flattened/10_basic.yml

+47
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,50 @@
109109
- match: { hits.total.value: 1 }
110110
- length: { hits.hits: 1 }
111111
- match: { hits.hits.0._id: "1" }
112+
113+
114+
---
115+
"Test fields option on flattened object field":
116+
- skip:
117+
version: " - 7.9.99"
118+
reason: "Fields option on search request was added in 7.10"
119+
120+
- do:
121+
indices.create:
122+
index: test
123+
body:
124+
mappings:
125+
properties:
126+
flattened:
127+
type: flattened
128+
129+
- do:
130+
index:
131+
index: test
132+
id: 1
133+
body:
134+
flattened:
135+
some_field: some_value
136+
refresh: true
137+
138+
- do:
139+
search:
140+
index: test
141+
body:
142+
fields: ["flattened"]
143+
144+
- match: { hits.total.value: 1 }
145+
- length: { hits.hits: 1 }
146+
- length: { hits.hits.0.fields: 1 }
147+
- match: { hits.hits.0.fields.flattened: [ { "some_field": "some_value" } ] }
148+
149+
- do:
150+
search:
151+
index: test
152+
body:
153+
fields: ["flat*"]
154+
155+
- match: { hits.total.value: 1 }
156+
- length: { hits.hits: 1 }
157+
- length: { hits.hits.0.fields: 1 }
158+
- match: { hits.hits.0.fields.flattened: [ { "some_field": "some_value" } ] }

0 commit comments

Comments
 (0)