From fe330b868a8324a74714f1be01aa498336e5ff23 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Sun, 21 Jun 2015 21:32:44 +0200 Subject: [PATCH] percolator: Fail nicely if `nested` query with `inner_hits` is used in a percolator query. Closes #11672 --- .../index/query/QueryParseContext.java | 4 +++ .../percolator/PercolatorTests.java | 36 +++++++++++++++---- docs/reference/search/percolate.asciidoc | 2 ++ 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/query/QueryParseContext.java b/core/src/main/java/org/elasticsearch/index/query/QueryParseContext.java index 75f0c630054..070b9a0cb2e 100644 --- a/core/src/main/java/org/elasticsearch/index/query/QueryParseContext.java +++ b/core/src/main/java/org/elasticsearch/index/query/QueryParseContext.java @@ -204,6 +204,10 @@ public class QueryParseContext { public void addInnerHits(String name, InnerHitsContext.BaseInnerHits context) { SearchContext sc = SearchContext.current(); + if (sc == null) { + throw new QueryParsingException(this, "inner_hits unsupported"); + } + InnerHitsContext innerHitsContext; if (sc.innerHits() == null) { innerHitsContext = new InnerHitsContext(new HashMap()); diff --git a/core/src/test/java/org/elasticsearch/percolator/PercolatorTests.java b/core/src/test/java/org/elasticsearch/percolator/PercolatorTests.java index 1168642a273..6b7ca3fca91 100644 --- a/core/src/test/java/org/elasticsearch/percolator/PercolatorTests.java +++ b/core/src/test/java/org/elasticsearch/percolator/PercolatorTests.java @@ -45,6 +45,7 @@ import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryParsingException; import org.elasticsearch.index.query.functionscore.factor.FactorBuilder; +import org.elasticsearch.index.query.support.QueryInnerHitBuilder; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.script.Script; import org.elasticsearch.search.highlight.HighlightBuilder; @@ -71,13 +72,7 @@ import static org.elasticsearch.common.settings.Settings.settingsBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.smileBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.yamlBuilder; -import static org.elasticsearch.index.query.QueryBuilders.boolQuery; -import static org.elasticsearch.index.query.QueryBuilders.constantScoreQuery; -import static org.elasticsearch.index.query.QueryBuilders.functionScoreQuery; -import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; -import static org.elasticsearch.index.query.QueryBuilders.matchQuery; -import static org.elasticsearch.index.query.QueryBuilders.rangeQuery; -import static org.elasticsearch.index.query.QueryBuilders.termQuery; +import static org.elasticsearch.index.query.QueryBuilders.*; import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.scriptFunction; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAllSuccessful; @@ -2028,7 +2023,34 @@ public class PercolatorTests extends ElasticsearchIntegrationTest { .execute().actionGet(); assertMatchCount(response1, 1l); assertThat(response1.getMatches(), arrayWithSize(1)); + } + @Test + public void testFailNicelyWithInnerHits() throws Exception { + XContentBuilder mapping = XContentFactory.jsonBuilder().startObject() + .startObject("mapping") + .startObject("properties") + .startObject("nested") + .field("type", "nested") + .startObject("properties") + .startObject("name") + .field("type", "string") + .endObject() + .endObject() + .endObject() + .endObject() + .endObject(); + + assertAcked(prepareCreate("index").addMapping("mapping", mapping)); + try { + client().prepareIndex("index", PercolatorService.TYPE_NAME, "1") + .setSource(jsonBuilder().startObject().field("query", nestedQuery("nested", matchQuery("nested.name", "value")).innerHit(new QueryInnerHitBuilder())).endObject()) + .execute().actionGet(); + fail("Expected a parse error, because inner_hits isn't supported in the percolate api"); + } catch (Exception e) { + assertThat(e.getCause(), instanceOf(QueryParsingException.class)); + assertThat(e.getCause().getMessage(), containsString("inner_hits unsupported")); + } } } diff --git a/docs/reference/search/percolate.asciidoc b/docs/reference/search/percolate.asciidoc index 3d3ef666838..9dfda915626 100644 --- a/docs/reference/search/percolate.asciidoc +++ b/docs/reference/search/percolate.asciidoc @@ -493,6 +493,8 @@ the time the percolate API needs to run can be decreased. Because the percolator API is processing one document at a time, it doesn't support queries and filters that run against child documents such as `has_child` and `has_parent`. +The `inner_hits` feature on the `nested` query isn't supported in the percolate api. + The `wildcard` and `regexp` query natively use a lot of memory and because the percolator keeps the queries into memory this can easily take up the available memory in the heap space. If possible try to use a `prefix` query or ngramming to achieve the same result (with way less memory being used).