From 687be70736ad40752abcf76e36bfa097c1c916e5 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Tue, 6 Jan 2015 17:52:24 +0100 Subject: [PATCH] Made sure that named filters and queries defined in a wrapped query and filter are not lost. Closes #6871 --- .../index/query/QueryParseContext.java | 4 ++++ .../index/query/WrapperFilterParser.java | 1 + .../index/query/WrapperQueryParser.java | 1 + .../matchedqueries/MatchedQueriesTests.java | 22 +++++++++++++++++++ 4 files changed, 28 insertions(+) diff --git a/src/main/java/org/elasticsearch/index/query/QueryParseContext.java b/src/main/java/org/elasticsearch/index/query/QueryParseContext.java index 830d2bb122e..f2cc063890b 100644 --- a/src/main/java/org/elasticsearch/index/query/QueryParseContext.java +++ b/src/main/java/org/elasticsearch/index/query/QueryParseContext.java @@ -250,6 +250,10 @@ public class QueryParseContext { return ImmutableMap.copyOf(namedFilters); } + public void combineNamedFilters(QueryParseContext context) { + namedFilters.putAll(context.namedFilters); + } + public void addInnerHits(String name, InnerHitsContext.BaseInnerHits context) { SearchContext sc = SearchContext.current(); InnerHitsContext innerHitsContext; diff --git a/src/main/java/org/elasticsearch/index/query/WrapperFilterParser.java b/src/main/java/org/elasticsearch/index/query/WrapperFilterParser.java index eb4399b86a5..2346d65943d 100644 --- a/src/main/java/org/elasticsearch/index/query/WrapperFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/WrapperFilterParser.java @@ -62,6 +62,7 @@ public class WrapperFilterParser implements FilterParser { context.reset(qSourceParser); Filter result = context.parseInnerFilter(); parser.nextToken(); + parseContext.combineNamedFilters(context); return result; } } diff --git a/src/main/java/org/elasticsearch/index/query/WrapperQueryParser.java b/src/main/java/org/elasticsearch/index/query/WrapperQueryParser.java index f125b512fcf..3fc16d7af74 100644 --- a/src/main/java/org/elasticsearch/index/query/WrapperQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/WrapperQueryParser.java @@ -62,6 +62,7 @@ public class WrapperQueryParser implements QueryParser { context.reset(qSourceParser); Query result = context.parseInnerQuery(); parser.nextToken(); + parseContext.combineNamedFilters(context); return result; } } diff --git a/src/test/java/org/elasticsearch/search/matchedqueries/MatchedQueriesTests.java b/src/test/java/org/elasticsearch/search/matchedqueries/MatchedQueriesTests.java index c26b9582fa7..dc1bef9e8a4 100644 --- a/src/test/java/org/elasticsearch/search/matchedqueries/MatchedQueriesTests.java +++ b/src/test/java/org/elasticsearch/search/matchedqueries/MatchedQueriesTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.search.matchedqueries; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.SearchHit; import org.elasticsearch.test.ElasticsearchIntegrationTest; import org.junit.Test; @@ -243,4 +244,25 @@ public class MatchedQueriesTests extends ElasticsearchIntegrationTest { } } } + + @Test + public void testMatchedWithWrapperQuery() throws Exception { + createIndex("test"); + ensureGreen(); + + client().prepareIndex("test", "type1", "1").setSource("content", "Lorem ipsum dolor sit amet").get(); + refresh(); + + QueryBuilder[] queries = new QueryBuilder[]{ + wrapperQuery(matchQuery("content", "amet").queryName("abc").buildAsBytes().toUtf8()), + constantScoreQuery(wrapperFilter(termFilter("content", "amet").filterName("abc").buildAsBytes().toUtf8())) + }; + for (QueryBuilder query : queries) { + SearchResponse searchResponse = client().prepareSearch() + .setQuery(query) + .get(); + assertHitCount(searchResponse, 1l); + assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("abc")); + } + } }