|
27 | 27 | import org.apache.lucene.search.Weight; |
28 | 28 | import org.apache.lucene.util.BitSet; |
29 | 29 | import org.elasticsearch.ExceptionsHelper; |
| 30 | +import org.elasticsearch.Version; |
30 | 31 | import org.elasticsearch.common.bytes.BytesReference; |
31 | 32 | import org.elasticsearch.common.collect.Tuple; |
32 | 33 | import org.elasticsearch.common.document.DocumentField; |
|
35 | 36 | import org.elasticsearch.common.xcontent.XContentHelper; |
36 | 37 | import org.elasticsearch.common.xcontent.XContentType; |
37 | 38 | import org.elasticsearch.common.xcontent.support.XContentMapValues; |
| 39 | +import org.elasticsearch.index.IndexSettings; |
38 | 40 | import org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor; |
39 | 41 | import org.elasticsearch.index.fieldvisitor.FieldsVisitor; |
40 | 42 | import org.elasticsearch.index.mapper.DocumentMapper; |
@@ -344,6 +346,7 @@ private SearchHit.NestedIdentity getInternalNestedIdentity(SearchContext context |
344 | 346 | ObjectMapper current = nestedObjectMapper; |
345 | 347 | String originalName = nestedObjectMapper.name(); |
346 | 348 | SearchHit.NestedIdentity nestedIdentity = null; |
| 349 | + final IndexSettings indexSettings = context.getQueryShardContext().getIndexSettings(); |
347 | 350 | do { |
348 | 351 | Query parentFilter; |
349 | 352 | nestedParentObjectMapper = current.getParentObjectMapper(mapperService); |
@@ -373,12 +376,32 @@ private SearchHit.NestedIdentity getInternalNestedIdentity(SearchContext context |
373 | 376 | BitSet parentBits = context.bitsetFilterCache().getBitSetProducer(parentFilter).getBitSet(subReaderContext); |
374 | 377 |
|
375 | 378 | int offset = 0; |
376 | | - int nextParent = parentBits.nextSetBit(currentParent); |
377 | | - for (int docId = childIter.advance(currentParent + 1); docId < nextParent && docId != DocIdSetIterator.NO_MORE_DOCS; |
378 | | - docId = childIter.nextDoc()) { |
379 | | - offset++; |
| 379 | + if (indexSettings.getIndexVersionCreated().onOrAfter(Version.V_6_5_0)) { |
| 380 | + /** |
| 381 | + * Starts from the previous parent and finds the offset of the |
| 382 | + * <code>nestedSubDocID</code> within the nested children. Nested documents |
| 383 | + * are indexed in the same order than in the source array so the offset |
| 384 | + * of the nested child is the number of nested document with the same parent |
| 385 | + * that appear before him. |
| 386 | + */ |
| 387 | + int previousParent = parentBits.prevSetBit(currentParent); |
| 388 | + for (int docId = childIter.advance(previousParent + 1); docId < nestedSubDocId && docId != DocIdSetIterator.NO_MORE_DOCS; |
| 389 | + docId = childIter.nextDoc()) { |
| 390 | + offset++; |
| 391 | + } |
| 392 | + currentParent = nestedSubDocId; |
| 393 | + } else { |
| 394 | + /** |
| 395 | + * Nested documents are in reverse order in this version so we start from the current nested document |
| 396 | + * and find the number of documents with the same parent that appear after it. |
| 397 | + */ |
| 398 | + int nextParent = parentBits.nextSetBit(currentParent); |
| 399 | + for (int docId = childIter.advance(currentParent + 1); docId < nextParent && docId != DocIdSetIterator.NO_MORE_DOCS; |
| 400 | + docId = childIter.nextDoc()) { |
| 401 | + offset++; |
| 402 | + } |
| 403 | + currentParent = nextParent; |
380 | 404 | } |
381 | | - currentParent = nextParent; |
382 | 405 | current = nestedObjectMapper = nestedParentObjectMapper; |
383 | 406 | int currentPrefix = current == null ? 0 : current.name().length() + 1; |
384 | 407 | nestedIdentity = new SearchHit.NestedIdentity(originalName.substring(currentPrefix), offset, nestedIdentity); |
|
0 commit comments