Skip to content

Commit 0fec6e4

Browse files
author
Christoph Büscher
committed
Prevent field API NPEs from token_count fields inside nested (#69068)
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
1 parent b3d3430 commit 0fec6e4

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

rest-api-spec/src/main/resources/rest-api-spec/test/search/330_fetch_fields.yml

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,6 @@ Test nested field with sibling field resolving to DocValueFetcher:
815815
hits.hits.0.fields.products.0: { "manufacturer" : ["Supersoft"]}
816816
- match:
817817
hits.hits.0.fields.products.1: { "manufacturer" : ["HyperSmart"]}
818-
819818
---
820819
"Test ignores malformed values while returning valid ones":
821820
- skip:
@@ -850,3 +849,40 @@ Test nested field with sibling field resolving to DocValueFetcher:
850849
- match: { hits.hits.0.fields.number.2 : 3 }
851850
- match: { hits.hits.0.fields.number.3 : 5 }
852851
- match: { hits.hits.0.fields.number.4 : 6 }
852+
---
853+
Test token_count inside nested field doesn't fail:
854+
- skip:
855+
version: ' - 7.11.99'
856+
reason: 'fix added in 7.12'
857+
- do:
858+
indices.create:
859+
index: test
860+
body:
861+
mappings:
862+
properties:
863+
user:
864+
type: nested
865+
properties:
866+
name:
867+
type: text
868+
fields:
869+
length:
870+
type: token_count
871+
analyzer: standard
872+
873+
- do:
874+
index:
875+
index: test
876+
id: 1
877+
refresh: true
878+
body:
879+
user:
880+
- { "name" : "Ann Marie Smith"}
881+
- { "name" : "James Brown"}
882+
883+
- do:
884+
search:
885+
index: test
886+
body:
887+
_source: false
888+
fields: [ "*" ]

server/src/main/java/org/elasticsearch/index/mapper/NestedValueFetcher.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
package org.elasticsearch.index.mapper;
1010

11+
import org.apache.lucene.index.LeafReaderContext;
1112
import org.elasticsearch.common.document.DocumentField;
1213
import org.elasticsearch.common.xcontent.support.XContentMapValues;
1314
import org.elasticsearch.search.fetch.subphase.FieldFetcher;
@@ -80,4 +81,9 @@ private Map<String, Object> createSourceMapStub(Map<String, Object> filteredSour
8081
}
8182
return next;
8283
}
84+
85+
@Override
86+
public void setNextReader(LeafReaderContext context) {
87+
this.nestedFieldFetcher.setNextReader(context);
88+
}
8389
}

0 commit comments

Comments
 (0)