Prechádzať zdrojové kódy

percolator: Fail nicely if `nested` query with `inner_hits` is used in a percolator query.

Closes #11672
Martijn van Groningen 10 rokov pred
rodič
commit
fe330b868a

+ 4 - 0
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<String, InnerHitsContext.BaseInnerHits>());

+ 29 - 7
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"));
+        }
     }
 }
 

+ 2 - 0
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).