From 697174dcb0b625cf33d39c6cda06a629baaaac74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Tue, 22 Mar 2016 12:33:11 +0100 Subject: [PATCH] Make sure to use nestedScope levels when building nested filters --- .../java/org/elasticsearch/index/query/QueryShardContext.java | 2 ++ .../main/java/org/elasticsearch/search/sort/SortBuilder.java | 4 +++- .../org/elasticsearch/search/sort/AbstractSortTestCase.java | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/query/QueryShardContext.java b/core/src/main/java/org/elasticsearch/index/query/QueryShardContext.java index a21b53cdf51..63eff82ddb0 100644 --- a/core/src/main/java/org/elasticsearch/index/query/QueryShardContext.java +++ b/core/src/main/java/org/elasticsearch/index/query/QueryShardContext.java @@ -105,6 +105,7 @@ public class QueryShardContext extends QueryRewriteContext { this.allowUnmappedFields = indexSettings.isDefaultAllowUnmappedFields(); this.indicesQueriesRegistry = indicesQueriesRegistry; this.percolatorQueryCache = percolatorQueryCache; + this.nestedScope = new NestedScope(); } public QueryShardContext(QueryShardContext source) { @@ -113,6 +114,7 @@ public class QueryShardContext extends QueryRewriteContext { } + @Override public QueryShardContext clone() { return new QueryShardContext(indexSettings, bitsetFilterCache, indexFieldDataService, mapperService, similarityService, scriptService, indicesQueriesRegistry, percolatorQueryCache); } diff --git a/core/src/main/java/org/elasticsearch/search/sort/SortBuilder.java b/core/src/main/java/org/elasticsearch/search/sort/SortBuilder.java index 7f48b86e9af..35d59de011c 100644 --- a/core/src/main/java/org/elasticsearch/search/sort/SortBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/sort/SortBuilder.java @@ -54,7 +54,9 @@ public abstract class SortBuilder> implements ToXConten } Query innerDocumentsQuery; if (nestedFilter != null) { - innerDocumentsQuery = nestedFilter.toFilter(context); + context.nestedScope().nextLevel(nestedObjectMapper); + innerDocumentsQuery = QueryBuilder.rewriteQuery(nestedFilter, context).toFilter(context); + context.nestedScope().previousLevel(); } else { innerDocumentsQuery = nestedObjectMapper.nestedTypeFilter(); } diff --git a/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java b/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java index 0c0c99a2620..84b23fb1449 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java @@ -242,7 +242,7 @@ public abstract class AbstractSortTestCase & SortBuilde } }); return new QueryShardContext(idxSettings, bitsetFilterCache, ifds, null, null, scriptService, - indicesQueriesRegistry) { + indicesQueriesRegistry, null) { @Override public MappedFieldType fieldMapper(String name) { return provideMappedFieldType(name);