diff --git a/src/main/java/org/elasticsearch/index/query/HasChildFilterParser.java b/src/main/java/org/elasticsearch/index/query/HasChildFilterParser.java index b9c552a8750..af94400f4f8 100644 --- a/src/main/java/org/elasticsearch/index/query/HasChildFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/HasChildFilterParser.java @@ -21,6 +21,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.Filter; import org.apache.lucene.search.Query; +import org.elasticsearch.ElasticSearchIllegalStateException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.search.XConstantScoreQuery; @@ -106,13 +107,13 @@ public class HasChildFilterParser implements FilterParser { } } if (!queryFound) { - throw new QueryParsingException(parseContext.index(), "[child] filter requires 'query' field"); + throw new QueryParsingException(parseContext.index(), "[has_child] filter requires 'query' field"); } if (query == null) { return null; } if (childType == null) { - throw new QueryParsingException(parseContext.index(), "[child] filter requires 'type' field"); + throw new QueryParsingException(parseContext.index(), "[has_child] filter requires 'type' field"); } DocumentMapper childDocMapper = parseContext.mapperService().documentMapper(childType); @@ -128,6 +129,9 @@ public class HasChildFilterParser implements FilterParser { query = new XFilteredQuery(query, parseContext.cacheFilter(childDocMapper.typeFilter(), null)); SearchContext searchContext = SearchContext.current(); + if (searchContext == null) { + throw new ElasticSearchIllegalStateException("[has_child] Can't execute, search context not set."); + } HasChildFilter childFilter = HasChildFilter.create(query, parentType, childType, searchContext); searchContext.addRewrite(childFilter); diff --git a/src/main/java/org/elasticsearch/index/query/HasChildQueryParser.java b/src/main/java/org/elasticsearch/index/query/HasChildQueryParser.java index ce729c98008..88474292dc4 100644 --- a/src/main/java/org/elasticsearch/index/query/HasChildQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/HasChildQueryParser.java @@ -22,6 +22,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.Filter; import org.apache.lucene.search.Query; +import org.elasticsearch.ElasticSearchIllegalStateException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.xcontent.XContentParser; @@ -118,6 +119,9 @@ public class HasChildQueryParser implements QueryParser { // wrap the query with type query SearchContext searchContext = SearchContext.current(); + if (searchContext == null) { + throw new ElasticSearchIllegalStateException("[has_child] Can't execute, search context not set."); + } Query query; if (scoreType != null) { Filter parentFilter = parseContext.cacheFilter(parentDocMapper.typeFilter(), null); diff --git a/src/main/java/org/elasticsearch/index/query/HasParentFilterParser.java b/src/main/java/org/elasticsearch/index/query/HasParentFilterParser.java index 6105f410fc9..e4e1bd8fb41 100644 --- a/src/main/java/org/elasticsearch/index/query/HasParentFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/HasParentFilterParser.java @@ -21,6 +21,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.Filter; import org.apache.lucene.search.Query; +import org.elasticsearch.ElasticSearchIllegalStateException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.search.XConstantScoreQuery; @@ -105,25 +106,29 @@ public class HasParentFilterParser implements FilterParser { } } if (!queryFound) { - throw new QueryParsingException(parseContext.index(), "[parent] filter requires 'query' field"); + throw new QueryParsingException(parseContext.index(), "[has_parent] filter requires 'query' field"); } if (query == null) { return null; } if (parentType == null) { - throw new QueryParsingException(parseContext.index(), "[parent] filter requires 'parent_type' field"); + throw new QueryParsingException(parseContext.index(), "[has_parent] filter requires 'parent_type' field"); } DocumentMapper parentDocMapper = parseContext.mapperService().documentMapper(parentType); if (parentDocMapper == null) { - throw new QueryParsingException(parseContext.index(), "[parent] filter configured 'parent_type' [" + parentType + "] is not a valid type"); + throw new QueryParsingException(parseContext.index(), "[has_parent] filter configured 'parent_type' [" + parentType + "] is not a valid type"); } // wrap the query with type query query = new XFilteredQuery(query, parseContext.cacheFilter(parentDocMapper.typeFilter(), null)); SearchContext searchContext = SearchContext.current(); + // In case of delete by query api + if (searchContext == null) { + throw new ElasticSearchIllegalStateException("[has_parent] Can't execute, search context not set"); + } HasParentFilter parentFilter = HasParentFilter.create(query, parentType, searchContext); searchContext.addRewrite(parentFilter); diff --git a/src/main/java/org/elasticsearch/index/query/HasParentQueryParser.java b/src/main/java/org/elasticsearch/index/query/HasParentQueryParser.java index df6fc3253a4..f7927df53f9 100644 --- a/src/main/java/org/elasticsearch/index/query/HasParentQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/HasParentQueryParser.java @@ -23,6 +23,7 @@ import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.Filter; import org.apache.lucene.search.Query; +import org.elasticsearch.ElasticSearchIllegalStateException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.search.XBooleanFilter; @@ -99,19 +100,19 @@ public class HasParentQueryParser implements QueryParser { } } if (!queryFound) { - throw new QueryParsingException(parseContext.index(), "[parent] query requires 'query' field"); + throw new QueryParsingException(parseContext.index(), "[has_parent] query requires 'query' field"); } if (innerQuery == null) { return null; } if (parentType == null) { - throw new QueryParsingException(parseContext.index(), "[parent] query requires 'parent_type' field"); + throw new QueryParsingException(parseContext.index(), "[has_parent] query requires 'parent_type' field"); } DocumentMapper parentDocMapper = parseContext.mapperService().documentMapper(parentType); if (parentDocMapper == null) { - throw new QueryParsingException(parseContext.index(), "[parent] query configured 'parent_type' [" + parentType + "] is not a valid type"); + throw new QueryParsingException(parseContext.index(), "[has_parent] query configured 'parent_type' [" + parentType + "] is not a valid type"); } List childTypes = new ArrayList(2); @@ -144,6 +145,10 @@ public class HasParentQueryParser implements QueryParser { // wrap the query with type query innerQuery = new XFilteredQuery(innerQuery, parseContext.cacheFilter(parentDocMapper.typeFilter(), null)); SearchContext searchContext = SearchContext.current(); + if (searchContext == null) { + throw new ElasticSearchIllegalStateException("[has_parent] Can't execute, search context not set."); + } + Query query; if (score) { ParentQuery parentQuery = new ParentQuery(searchContext, innerQuery, parentType, childTypes, childFilter); diff --git a/src/main/java/org/elasticsearch/index/query/TopChildrenQueryParser.java b/src/main/java/org/elasticsearch/index/query/TopChildrenQueryParser.java index 31811bba8e7..fa33b9cb31b 100644 --- a/src/main/java/org/elasticsearch/index/query/TopChildrenQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/TopChildrenQueryParser.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.Query; +import org.elasticsearch.ElasticSearchIllegalStateException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.search.XFilteredQuery; @@ -97,10 +98,10 @@ public class TopChildrenQueryParser implements QueryParser { } } if (!queryFound) { - throw new QueryParsingException(parseContext.index(), "[child] requires 'query' field"); + throw new QueryParsingException(parseContext.index(), "[top_children] requires 'query' field"); } if (childType == null) { - throw new QueryParsingException(parseContext.index(), "[child] requires 'type' field"); + throw new QueryParsingException(parseContext.index(), "[top_children] requires 'type' field"); } if (query == null) { @@ -121,6 +122,9 @@ public class TopChildrenQueryParser implements QueryParser { query = new XFilteredQuery(query, parseContext.cacheFilter(childDocMapper.typeFilter(), null)); SearchContext searchContext = SearchContext.current(); + if (searchContext == null) { + throw new ElasticSearchIllegalStateException("[top_children] Can't execute, search context not set."); + } TopChildrenQuery childQuery = new TopChildrenQuery(searchContext, query, childType, parentType, scoreType, factor, incrementalFactor); searchContext.addRewrite(childQuery); return childQuery;