Browse Source

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
Christoph Büscher 4 năm trước cách đây
mục cha
commit
e55e0f1792

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

@@ -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:
@@ -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.99.99'
+      reason:  'Added in 8.0 - change on backport'
+  -  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: [ "*" ]

+ 6 - 0
server/src/main/java/org/elasticsearch/index/mapper/NestedValueFetcher.java

@@ -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;
@@ -80,4 +81,9 @@ public class NestedValueFetcher implements ValueFetcher {
         }
         return next;
     }
+
+    @Override
+    public void setNextReader(LeafReaderContext context) {
+        this.nestedFieldFetcher.setNextReader(context);
+    }
 }