Skip to content

Commit

Permalink
Fixed named filter and query support for the top_children, has_child …
Browse files Browse the repository at this point in the history
…and has_parent queries and filters.

Closes #4534
  • Loading branch information
martijnvg committed Dec 23, 2013
1 parent 9c67be5 commit 27b53b8
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 16 deletions.
Expand Up @@ -144,7 +144,7 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
Query childrenConstantScoreQuery = new ChildrenConstantScoreQuery(query, parentType, childType, parentFilter, shortCircuitParentDocSet, nonNestedDocsFilter);

if (filterName != null) {
parseContext.addNamedQuery(filterName, childrenConstantScoreQuery);
parseContext.addNamedFilter(filterName, new CustomQueryWrappingFilter(childrenConstantScoreQuery));
}

boolean deleteByQuery = "delete_by_query".equals(SearchContext.current().source());
Expand Down
Expand Up @@ -27,10 +27,7 @@
import org.elasticsearch.common.lucene.search.XFilteredQuery;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.search.child.ChildrenConstantScoreQuery;
import org.elasticsearch.index.search.child.ChildrenQuery;
import org.elasticsearch.index.search.child.DeleteByQueryWrappingFilter;
import org.elasticsearch.index.search.child.ScoreType;
import org.elasticsearch.index.search.child.*;
import org.elasticsearch.index.search.nested.NonNestedDocsFilter;
import org.elasticsearch.search.internal.SearchContext;

Expand Down Expand Up @@ -154,7 +151,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
}
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
parseContext.addNamedFilter(queryName, new CustomQueryWrappingFilter(query));
}
query.setBoost(boost);
return query;
Expand Down
Expand Up @@ -161,7 +161,7 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
Query parentConstantScoreQuery = new ParentConstantScoreQuery(query, parentType, childrenFilter);

if (filterName != null) {
parseContext.addNamedQuery(filterName, parentConstantScoreQuery);
parseContext.addNamedFilter(filterName, new CustomQueryWrappingFilter(parentConstantScoreQuery));
}

boolean deleteByQuery = "delete_by_query".equals(SearchContext.current().source());
Expand Down
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.internal.ParentFieldMapper;
import org.elasticsearch.index.search.child.CustomQueryWrappingFilter;
import org.elasticsearch.index.search.child.DeleteByQueryWrappingFilter;
import org.elasticsearch.index.search.child.ParentConstantScoreQuery;
import org.elasticsearch.index.search.child.ParentQuery;
Expand Down Expand Up @@ -170,7 +171,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
query.setBoost(boost);
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
parseContext.addNamedFilter(queryName, new CustomQueryWrappingFilter(query));
}
return query;
}
Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.elasticsearch.common.lucene.search.XFilteredQuery;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.search.child.CustomQueryWrappingFilter;
import org.elasticsearch.index.search.child.ScoreType;
import org.elasticsearch.index.search.child.TopChildrenQuery;
import org.elasticsearch.search.internal.SearchContext;
Expand All @@ -51,7 +52,7 @@ public String[] names() {
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
XContentParser parser = parseContext.parser();

Query query = null;
Query innerQuery = null;
boolean queryFound = false;
float boost = 1.0f;
String childType = null;
Expand All @@ -72,7 +73,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
// since we switch types, make sure we change the context
String[] origTypes = QueryParseContext.setTypesWithPrevious(childType == null ? null : new String[]{childType});
try {
query = parseContext.parseInnerQuery();
innerQuery = parseContext.parseInnerQuery();
} finally {
QueryParseContext.setTypes(origTypes);
}
Expand Down Expand Up @@ -108,7 +109,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
throw new QueryParsingException(parseContext.index(), "[top_children] requires 'type' field");
}

if (query == null) {
if (innerQuery == null) {
return null;
}

Expand All @@ -125,13 +126,13 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
String parentType = childDocMapper.parentFieldMapper().type();

query.setBoost(boost);
innerQuery.setBoost(boost);
// wrap the query with type query
query = new XFilteredQuery(query, parseContext.cacheFilter(childDocMapper.typeFilter(), null));
TopChildrenQuery childQuery = new TopChildrenQuery(query, childType, parentType, scoreType, factor, incrementalFactor, parseContext.cacheRecycler());
innerQuery = new XFilteredQuery(innerQuery, parseContext.cacheFilter(childDocMapper.typeFilter(), null));
TopChildrenQuery query = new TopChildrenQuery(innerQuery, childType, parentType, scoreType, factor, incrementalFactor, parseContext.cacheRecycler());
if (queryName != null) {
parseContext.addNamedQuery(queryName, childQuery);
parseContext.addNamedFilter(queryName, new CustomQueryWrappingFilter(query));
}
return childQuery;
return query;
}
}
Expand Up @@ -2004,6 +2004,54 @@ public void testHasChildQueryWithNestedInnerObjects() throws Exception {
assertThat(searchResponse.getHits().totalHits(), equalTo(2l));
}

@Test
public void testNamedFilters() throws Exception {
client().admin().indices().prepareCreate("test")
.setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0))
.execute().actionGet();
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
client().admin()
.indices()
.preparePutMapping("test")
.setType("child")
.setSource("_parent", "type=parent").execute().actionGet();

String parentId = "p1";
client().prepareIndex("test", "parent", parentId).setSource("p_field", "1").execute().actionGet();
client().prepareIndex("test", "child", "c1").setSource("c_field", "1").setParent(parentId).execute().actionGet();
client().admin().indices().prepareRefresh().execute().actionGet();

SearchResponse searchResponse = client().prepareSearch("test").setQuery(topChildrenQuery("child", termQuery("c_field", "1")).queryName("test"))
.execute().actionGet();
assertHitCount(searchResponse, 1l);
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1));
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("test"));

searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("child", termQuery("c_field", "1")).scoreType("max").queryName("test"))
.execute().actionGet();
assertHitCount(searchResponse, 1l);
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1));
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("test"));

searchResponse = client().prepareSearch("test").setQuery(hasParentQuery("parent", termQuery("p_field", "1")).scoreType("score").queryName("test"))
.execute().actionGet();
assertHitCount(searchResponse, 1l);
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1));
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("test"));

searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(hasChildFilter("child", termQuery("c_field", "1")).filterName("test")))
.execute().actionGet();
assertHitCount(searchResponse, 1l);
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1));
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("test"));

searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(hasParentFilter("parent", termQuery("p_field", "1")).filterName("test")))
.execute().actionGet();
assertHitCount(searchResponse, 1l);
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1));
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("test"));
}

private static HasChildFilterBuilder hasChildFilter(String type, QueryBuilder queryBuilder) {
HasChildFilterBuilder hasChildFilterBuilder = FilterBuilders.hasChildFilter(type, queryBuilder);
hasChildFilterBuilder.setShortCircuitCutoff(randomInt(10));
Expand Down

0 comments on commit 27b53b8

Please sign in to comment.