Skip to content

Commit

Permalink
Fix UOE when fetching flattened field (#64241)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jimczi authored Oct 28, 2020
1 parent 4388449 commit 3253488
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, S

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import org.elasticsearch.index.mapper.FieldTypeTestCase;
import org.elasticsearch.xpack.flattened.mapper.FlattenedFieldMapper.KeyedFlattenedFieldType;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class KeyedFlattenedFieldTypeTests extends FieldTypeTestCase {

Expand Down Expand Up @@ -148,4 +150,12 @@ public void testWildcardQuery() {
() -> ft.wildcardQuery("valu*", null, false, randomMockShardContext()));
assertEquals("[wildcard] queries are not currently supported on keyed [flattened] fields.", e.getMessage());
}

public void testFetchIsEmpty() throws IOException {
Map<String, Object> sourceValue = Map.of("key", "value");
KeyedFlattenedFieldType ft = createFieldType();

assertEquals(List.of(), fetchSourceValue(ft, sourceValue));
assertEquals(List.of(), fetchSourceValue(ft, null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,50 @@
- match: { hits.total.value: 1 }
- length: { hits.hits: 1 }
- match: { hits.hits.0._id: "1" }


---
"Test fields option on flattened object field":
- skip:
version: " - 7.99.99"
reason: "Fields option on search request was added in 7.10"

- do:
indices.create:
index: test
body:
mappings:
properties:
flattened:
type: flattened

- do:
index:
index: test
id: 1
body:
flattened:
some_field: some_value
refresh: true

- do:
search:
index: test
body:
fields: ["flattened"]

- match: { hits.total.value: 1 }
- length: { hits.hits: 1 }
- length: { hits.hits.0.fields: 1 }
- match: { hits.hits.0.fields.flattened: [ { "some_field": "some_value" } ] }

- do:
search:
index: test
body:
fields: ["flat*"]

- match: { hits.total.value: 1 }
- length: { hits.hits: 1 }
- length: { hits.hits.0.fields: 1 }
- match: { hits.hits.0.fields.flattened: [ { "some_field": "some_value" } ] }

0 comments on commit 3253488

Please sign in to comment.