Skip to content

Commit

Permalink
Made sure that named filters and queries defined in a wrapped query a…
Browse files Browse the repository at this point in the history
…nd filter are not lost.

Closes #6871
  • Loading branch information
martijnvg committed Jan 7, 2015
1 parent 3edf07c commit 93cf485
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
Expand Up @@ -236,6 +236,10 @@ public ImmutableMap<String, Filter> copyNamedFilters() {
return ImmutableMap.copyOf(namedFilters);
}

public void combineNamedFilters(QueryParseContext context) {
namedFilters.putAll(context.namedFilters);
}

@Nullable
public Query parseInnerQuery() throws IOException, QueryParsingException {
// move to START object
Expand Down
Expand Up @@ -62,6 +62,7 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
context.reset(qSourceParser);
Filter result = context.parseInnerFilter();
parser.nextToken();
parseContext.combineNamedFilters(context);
return result;
}
}
Expand Down
Expand Up @@ -62,6 +62,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
context.reset(qSourceParser);
Query result = context.parseInnerQuery();
parser.nextToken();
parseContext.combineNamedFilters(context);
return result;
}
}
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -243,4 +244,25 @@ public void testMatchedWithShould() throws Exception {
}
}
}

@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"));
}
}
}

0 comments on commit 93cf485

Please sign in to comment.