Enable validation of queries with has_child and script filters
This commit is contained in:
parent
dbeda1ab2b
commit
a4ad84b5e4
|
@ -36,7 +36,12 @@ import org.elasticsearch.common.settings.Settings;
|
|||
import org.elasticsearch.index.query.IndexQueryParserService;
|
||||
import org.elasticsearch.index.query.ParsedQuery;
|
||||
import org.elasticsearch.index.query.QueryParsingException;
|
||||
import org.elasticsearch.index.service.IndexService;
|
||||
import org.elasticsearch.index.shard.service.IndexShard;
|
||||
import org.elasticsearch.indices.IndicesService;
|
||||
import org.elasticsearch.script.ScriptService;
|
||||
import org.elasticsearch.search.internal.InternalSearchRequest;
|
||||
import org.elasticsearch.search.internal.SearchContext;
|
||||
import org.elasticsearch.threadpool.ThreadPool;
|
||||
import org.elasticsearch.transport.TransportService;
|
||||
|
||||
|
@ -54,10 +59,13 @@ public class TransportValidateQueryAction extends TransportBroadcastOperationAct
|
|||
|
||||
private final IndicesService indicesService;
|
||||
|
||||
private final ScriptService scriptService;
|
||||
|
||||
@Inject
|
||||
public TransportValidateQueryAction(Settings settings, ThreadPool threadPool, ClusterService clusterService, TransportService transportService, IndicesService indicesService) {
|
||||
public TransportValidateQueryAction(Settings settings, ThreadPool threadPool, ClusterService clusterService, TransportService transportService, IndicesService indicesService, ScriptService scriptService) {
|
||||
super(settings, threadPool, clusterService, transportService);
|
||||
this.indicesService = indicesService;
|
||||
this.scriptService = scriptService;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -148,12 +156,19 @@ public class TransportValidateQueryAction extends TransportBroadcastOperationAct
|
|||
@Override
|
||||
protected ShardValidateQueryResponse shardOperation(ShardValidateQueryRequest request) throws ElasticSearchException {
|
||||
IndexQueryParserService queryParserService = indicesService.indexServiceSafe(request.index()).queryParserService();
|
||||
IndexService indexService = indicesService.indexServiceSafe(request.index());
|
||||
IndexShard indexShard = indexService.shardSafe(request.shardId());
|
||||
|
||||
boolean valid;
|
||||
String explanation = null;
|
||||
String error = null;
|
||||
if (request.querySource().length() == 0) {
|
||||
valid = true;
|
||||
} else {
|
||||
SearchContext.setCurrent(new SearchContext(0,
|
||||
new InternalSearchRequest().types(request.types()),
|
||||
null, indexShard.searcher(), indexService, indexShard,
|
||||
scriptService));
|
||||
try {
|
||||
ParsedQuery parsedQuery = queryParserService.parse(request.querySource().bytes(), request.querySource().offset(), request.querySource().length());
|
||||
valid = true;
|
||||
|
@ -166,6 +181,9 @@ public class TransportValidateQueryAction extends TransportBroadcastOperationAct
|
|||
} catch (AssertionError e) {
|
||||
valid = false;
|
||||
error = e.getMessage();
|
||||
} finally {
|
||||
SearchContext.current().release();
|
||||
SearchContext.removeCurrent();
|
||||
}
|
||||
}
|
||||
return new ShardValidateQueryResponse(request.index(), request.shardId(), valid, explanation, error);
|
||||
|
|
|
@ -104,6 +104,14 @@ public class SimpleValidateQueryTests extends AbstractNodesTests {
|
|||
.startObject("pin").startObject("properties").startObject("location").field("type", "geo_point").endObject().endObject().endObject()
|
||||
.endObject().endObject().endObject())
|
||||
.execute().actionGet();
|
||||
client.admin().indices().preparePutMapping("test").setType("child-type")
|
||||
.setSource(XContentFactory.jsonBuilder().startObject().startObject("child-type")
|
||||
.startObject("_parent").field("type", "type1").endObject()
|
||||
.startObject("properties")
|
||||
.startObject("foo").field("type", "string").endObject()
|
||||
.endObject()
|
||||
.endObject().endObject())
|
||||
.execute().actionGet();
|
||||
|
||||
client.admin().indices().prepareRefresh().execute().actionGet();
|
||||
|
||||
|
@ -179,6 +187,19 @@ public class SimpleValidateQueryTests extends AbstractNodesTests {
|
|||
assertExplanation(QueryBuilders.constantScoreQuery(FilterBuilders.notFilter(FilterBuilders.termFilter("foo", "bar"))),
|
||||
equalTo("ConstantScore(NotDeleted(NotFilter(cache(foo:bar))))"));
|
||||
|
||||
assertExplanation(QueryBuilders.filteredQuery(
|
||||
QueryBuilders.termQuery("foo", "1"),
|
||||
FilterBuilders.hasChildFilter(
|
||||
"child-type",
|
||||
QueryBuilders.fieldQuery("foo", "1")
|
||||
)
|
||||
), equalTo("filtered(foo:1)->child_filter[child-type/type1](filtered(foo:1)->cache(_type:child-type))"));
|
||||
|
||||
assertExplanation(QueryBuilders.filteredQuery(
|
||||
QueryBuilders.termQuery("foo", "1"),
|
||||
FilterBuilders.scriptFilter("true")
|
||||
), equalTo("filtered(foo:1)->ScriptFilter(true)"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -242,6 +263,7 @@ public class SimpleValidateQueryTests extends AbstractNodesTests {
|
|||
|
||||
private void assertExplanation(QueryBuilder queryBuilder, Matcher<String> matcher) {
|
||||
ValidateQueryResponse response = client.admin().indices().prepareValidateQuery("test")
|
||||
.setTypes("type1")
|
||||
.setQuery(queryBuilder)
|
||||
.setExplain(true)
|
||||
.execute().actionGet();
|
||||
|
|
Loading…
Reference in New Issue