Skip to content

Commit

Permalink
Prevent field API NPEs from token_count fields inside nested (#69068)
Browse files Browse the repository at this point in the history
Currently when a `token_count` field is defined inside a nested field, we get an
NPE because the underlying DocValueFetcher needs its formattedDocValues to be
loaded and the SourceLookup it sees needs to have a valid docId other than -1.
This change fixes those issues so the whole fields request doesn't error.
However this change doesn't solve the missing support for doc values lookup
under nested fields described in 68983. Fortunately `token_count` seems to be the only
mapping type currently affected.

Relates to #68983
  • Loading branch information
Christoph Büscher committed Feb 19, 2021
1 parent b3d3430 commit 0fec6e4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,6 @@ Test nested field with sibling field resolving to DocValueFetcher:
hits.hits.0.fields.products.0: { "manufacturer" : ["Supersoft"]}
- match:
hits.hits.0.fields.products.1: { "manufacturer" : ["HyperSmart"]}

---
"Test ignores malformed values while returning valid ones":
- skip:
Expand Down Expand Up @@ -850,3 +849,40 @@ Test nested field with sibling field resolving to DocValueFetcher:
- match: { hits.hits.0.fields.number.2 : 3 }
- match: { hits.hits.0.fields.number.3 : 5 }
- match: { hits.hits.0.fields.number.4 : 6 }
---
Test token_count inside nested field doesn't fail:
- skip:
version: ' - 7.11.99'
reason: 'fix added in 7.12'
- do:
indices.create:
index: test
body:
mappings:
properties:
user:
type: nested
properties:
name:
type: text
fields:
length:
type: token_count
analyzer: standard

- do:
index:
index: test
id: 1
refresh: true
body:
user:
- { "name" : "Ann Marie Smith"}
- { "name" : "James Brown"}

- do:
search:
index: test
body:
_source: false
fields: [ "*" ]
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package org.elasticsearch.index.mapper;

import org.apache.lucene.index.LeafReaderContext;
import org.elasticsearch.common.document.DocumentField;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.search.fetch.subphase.FieldFetcher;
Expand Down Expand Up @@ -80,4 +81,9 @@ private Map<String, Object> createSourceMapStub(Map<String, Object> filteredSour
}
return next;
}

@Override
public void setNextReader(LeafReaderContext context) {
this.nestedFieldFetcher.setNextReader(context);
}
}

0 comments on commit 0fec6e4

Please sign in to comment.