If searchContext not set, abort parsing and throw ISE

This commit is contained in:
Martijn van Groningen 2013-04-24 10:24:15 +02:00
parent c884304753
commit dd12e0b86c
5 changed files with 32 additions and 10 deletions

View File

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

View File

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

View File

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

View File

@ -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<String> childTypes = new ArrayList<String>(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);

View File

@ -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;