Skip to content

Commit 6e31278

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 02b28dd commit 6e31278

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, S
263263

264264
@Override
265265
public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) {
266-
throw new UnsupportedOperationException();
266+
// This is an internal field but it can match a field pattern so we return an empty list.
267+
return lookup -> List.of();
267268
}
268269
}
269270

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

Lines changed: 10 additions & 0 deletions
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.FlattenedFieldMapper.KeyedFlattenedFieldType;
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 KeyedFlattenedFieldTypeTests extends FieldTypeTestCase {
2830

@@ -148,4 +150,12 @@ public void testWildcardQuery() {
148150
() -> ft.wildcardQuery("valu*", null, false, randomMockShardContext()));
149151
assertEquals("[wildcard] queries are not currently supported on keyed [flattened] fields.", e.getMessage());
150152
}
153+
154+
public void testFetchIsEmpty() throws IOException {
155+
Map<String, Object> sourceValue = Map.of("key", "value");
156+
KeyedFlattenedFieldType ft = createFieldType();
157+
158+
assertEquals(List.of(), fetchSourceValue(ft, sourceValue));
159+
assertEquals(List.of(), fetchSourceValue(ft, null));
160+
}
151161
}

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

Lines changed: 47 additions & 0 deletions
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.10.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)