diff --git a/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java index 6c624665a99..a171139b746 100644 --- a/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java @@ -272,4 +272,12 @@ public abstract class AbstractQueryBuilder> protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) throws IOException { return this; } + + // Like Objects.requireNotNull(...) but instead throws a IllegalArgumentException + protected static T requireValue(T value, String message) { + if (value == null) { + throw new IllegalArgumentException(message); + } + return value; + } } diff --git a/core/src/main/java/org/elasticsearch/index/query/HasChildQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/HasChildQueryBuilder.java index d1a09fbe852..4687cfceef9 100644 --- a/core/src/main/java/org/elasticsearch/index/query/HasChildQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/HasChildQueryBuilder.java @@ -45,7 +45,7 @@ import java.util.Locale; import java.util.Objects; /** - * A query builder for has_child queries. + * A query builder for has_child query. */ public class HasChildQueryBuilder extends AbstractQueryBuilder { @@ -68,10 +68,6 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder query; - private final String type; - - private ScoreMode scoreMode = DEFAULT_SCORE_MODE; - - private int minChildren = DEFAULT_MIN_CHILDREN; - - private int maxChildren = DEFAULT_MAX_CHILDREN; - + private final ScoreMode scoreMode; private InnerHitBuilder innerHitBuilder; - + private int minChildren = DEFAULT_MIN_CHILDREN; + private int maxChildren = DEFAULT_MAX_CHILDREN; private boolean ignoreUnmapped = false; - - public HasChildQueryBuilder(String type, QueryBuilder query, int maxChildren, int minChildren, ScoreMode scoreMode, - InnerHitBuilder innerHitBuilder) { - this(type, query); - scoreMode(scoreMode); - this.maxChildren = maxChildren; - this.minChildren = minChildren; - this.innerHitBuilder = innerHitBuilder; - if (this.innerHitBuilder != null) { - this.innerHitBuilder.setParentChildType(type); - this.innerHitBuilder.setQuery(query); - } + public HasChildQueryBuilder(String type, QueryBuilder query, ScoreMode scoreMode) { + this(type, query, DEFAULT_MIN_CHILDREN, DEFAULT_MAX_CHILDREN, scoreMode, null); } - public HasChildQueryBuilder(String type, QueryBuilder query) { - if (type == null) { - throw new IllegalArgumentException("[" + NAME + "] requires 'type' field"); - } - if (query == null) { - throw new IllegalArgumentException("[" + NAME + "] requires 'query' field"); - } - this.type = type; - this.query = query; + private HasChildQueryBuilder(String type, QueryBuilder query, int minChildren, int maxChildren, ScoreMode scoreMode, + InnerHitBuilder innerHitBuilder) { + this.type = requireValue(type, "[" + NAME + "] requires 'type' field"); + this.query = requireValue(query, "[" + NAME + "] requires 'query' field"); + this.scoreMode = requireValue(scoreMode, "[" + NAME + "] requires 'score_mode' field"); + this.innerHitBuilder = innerHitBuilder; + this.minChildren = minChildren; + this.maxChildren = maxChildren; } /** @@ -137,8 +116,8 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder 0 && maxChildren < minChildren) { - throw new QueryShardException(context, "[" + NAME + "] 'max_children' is less than 'min_children'"); - } - // wrap the query with type query innerQuery = Queries.filtered(innerQuery, childDocMapper.typeFilter()); @@ -502,7 +460,7 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException { QueryBuilder rewrite = query.rewrite(queryRewriteContext); if (rewrite != query) { - HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder(type, rewrite); - hasChildQueryBuilder.minChildren(minChildren); - hasChildQueryBuilder.maxChildren(maxChildren); - hasChildQueryBuilder.scoreMode(scoreMode); - hasChildQueryBuilder.innerHit(innerHitBuilder); - return hasChildQueryBuilder; + return new HasChildQueryBuilder(type, rewrite, minChildren, minChildren, scoreMode, innerHitBuilder); } return this; } diff --git a/core/src/main/java/org/elasticsearch/index/query/HasParentQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/HasParentQueryBuilder.java index 11d2f62b7df..2384a620925 100644 --- a/core/src/main/java/org/elasticsearch/index/query/HasParentQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/HasParentQueryBuilder.java @@ -48,8 +48,6 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder query; private final String type; - private boolean score = DEFAULT_SCORE; + private final boolean score; private InnerHitBuilder innerHit; private boolean ignoreUnmapped = false; - /** - * @param type The parent type - * @param query The query that will be matched with parent documents - */ - public HasParentQueryBuilder(String type, QueryBuilder query) { - if (type == null) { - throw new IllegalArgumentException("[" + NAME + "] requires 'parent_type' field"); - } - if (query == null) { - throw new IllegalArgumentException("[" + NAME + "] requires 'query' field"); - } - this.type = type; - this.query = query; + public HasParentQueryBuilder(String type, QueryBuilder query, boolean score) { + this(type, query, score, null); } - public HasParentQueryBuilder(String type, QueryBuilder query, boolean score, InnerHitBuilder innerHit) { - this(type, query); + private HasParentQueryBuilder(String type, QueryBuilder query, boolean score, InnerHitBuilder innerHit) { + this.type = requireValue(type, "[" + NAME + "] requires 'type' field"); + this.query = requireValue(query, "[" + NAME + "] requires 'query' field"); this.score = score; this.innerHit = innerHit; - if (this.innerHit != null) { - this.innerHit.setParentChildType(type); - this.innerHit.setQuery(query); - } } /** @@ -114,24 +98,6 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder doRewrite(QueryRewriteContext queryShardContext) throws IOException { QueryBuilder rewrite = query.rewrite(queryShardContext); if (rewrite != query) { - HasParentQueryBuilder hasParentQueryBuilder = new HasParentQueryBuilder(type, rewrite); - hasParentQueryBuilder.score(score); - hasParentQueryBuilder.innerHit(innerHit); - return hasParentQueryBuilder; + return new HasParentQueryBuilder(type, rewrite, score, innerHit); } return this; } diff --git a/core/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java index 77fb8453a72..cd6d43b121c 100644 --- a/core/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java @@ -44,12 +44,6 @@ public class NestedQueryBuilder extends AbstractQueryBuilder */ public static final String NAME = "nested"; public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME); - - /** - * The default score mode for nested queries. - */ - public static final ScoreMode DEFAULT_SCORE_MODE = ScoreMode.Avg; - /** * The default value for ignore_unmapped. */ @@ -61,35 +55,21 @@ public class NestedQueryBuilder extends AbstractQueryBuilder private static final ParseField INNER_HITS_FIELD = new ParseField("inner_hits"); private static final ParseField IGNORE_UNMAPPED_FIELD = new ParseField("ignore_unmapped"); - private final QueryBuilder query; - private final String path; - - private ScoreMode scoreMode = DEFAULT_SCORE_MODE; - + private final ScoreMode scoreMode; + private final QueryBuilder query; private InnerHitBuilder innerHitBuilder; - private boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED; - public NestedQueryBuilder(String path, QueryBuilder query) { - if (path == null) { - throw new IllegalArgumentException("[" + NAME + "] requires 'path' field"); - } - if (query == null) { - throw new IllegalArgumentException("[" + NAME + "] requires 'query' field"); - } - this.path = path; - this.query = query; + public NestedQueryBuilder(String path, QueryBuilder query, ScoreMode scoreMode) { + this(path, query, scoreMode, null); } - public NestedQueryBuilder(String path, QueryBuilder query, ScoreMode scoreMode, InnerHitBuilder innerHitBuilder) { - this(path, query); - scoreMode(scoreMode); + private NestedQueryBuilder(String path, QueryBuilder query, ScoreMode scoreMode, InnerHitBuilder innerHitBuilder) { + this.path = requireValue(path, "[" + NAME + "] requires 'path' field"); + this.query = requireValue(query, "[" + NAME + "] requires 'query' field"); + this.scoreMode = requireValue(scoreMode, "[" + NAME + "] requires 'score_mode' field"); this.innerHitBuilder = innerHitBuilder; - if (this.innerHitBuilder != null) { - this.innerHitBuilder.setNestedPath(path); - this.innerHitBuilder.setQuery(query); - } } /** @@ -114,24 +94,32 @@ public class NestedQueryBuilder extends AbstractQueryBuilder } /** - * The score mode how the scores from the matching child documents are mapped into the nested parent document. + * Returns the nested query to execute. */ - public NestedQueryBuilder scoreMode(ScoreMode scoreMode) { - if (scoreMode == null) { - throw new IllegalArgumentException("[" + NAME + "] requires 'score_mode' field"); - } - this.scoreMode = scoreMode; + public QueryBuilder query() { + return query; + } + + /** + * Returns inner hit definition in the scope of this query and reusing the defined type and query. + */ + + public InnerHitBuilder innerHit() { + return innerHitBuilder; + } + + public NestedQueryBuilder innerHit(InnerHitBuilder innerHit) { + innerHit.setNestedPath(path); + innerHit.setQuery(query); + this.innerHitBuilder = innerHit; return this; } /** - * Sets inner hit definition in the scope of this nested query and reusing the defined path and query. + * Returns how the scores from the matching child documents are mapped into the nested parent document. */ - public NestedQueryBuilder innerHit(InnerHitBuilder innerHit) { - this.innerHitBuilder = Objects.requireNonNull(innerHit); - this.innerHitBuilder.setNestedPath(path); - this.innerHitBuilder.setQuery(query); - return this; + public ScoreMode scoreMode() { + return scoreMode; } /** @@ -153,27 +141,6 @@ public class NestedQueryBuilder extends AbstractQueryBuilder return ignoreUnmapped; } - /** - * Returns the nested query to execute. - */ - public QueryBuilder query() { - return query; - } - - /** - * Returns inner hit definition in the scope of this query and reusing the defined type and query. - */ - public InnerHitBuilder innerHit() { - return innerHitBuilder; - } - - /** - * Returns how the scores from the matching child documents are mapped into the nested parent document. - */ - public ScoreMode scoreMode() { - return scoreMode; - } - @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); @@ -194,7 +161,7 @@ public class NestedQueryBuilder extends AbstractQueryBuilder public static NestedQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { XContentParser parser = parseContext.parser(); float boost = AbstractQueryBuilder.DEFAULT_BOOST; - ScoreMode scoreMode = NestedQueryBuilder.DEFAULT_SCORE_MODE; + ScoreMode scoreMode = ScoreMode.Avg; String queryName = null; QueryBuilder query = null; String path = null; @@ -243,7 +210,7 @@ public class NestedQueryBuilder extends AbstractQueryBuilder return Objects.equals(query, that.query) && Objects.equals(path, that.path) && Objects.equals(scoreMode, that.scoreMode) - && Objects.equals(innerHitBuilder, that.innerHitBuilder) + && Objects.equals(innerHitBuilder, that.innerHitBuilder) && Objects.equals(ignoreUnmapped, that.ignoreUnmapped); } @@ -294,7 +261,7 @@ public class NestedQueryBuilder extends AbstractQueryBuilder protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException { QueryBuilder rewrite = query.rewrite(queryRewriteContext); if (rewrite != query) { - return new NestedQueryBuilder(path, rewrite).scoreMode(scoreMode).innerHit(innerHitBuilder); + return new NestedQueryBuilder(path, rewrite, scoreMode, innerHitBuilder); } return this; } diff --git a/core/src/main/java/org/elasticsearch/index/query/QueryBuilders.java b/core/src/main/java/org/elasticsearch/index/query/QueryBuilders.java index f04f03fcbcd..44ce3d38bda 100644 --- a/core/src/main/java/org/elasticsearch/index/query/QueryBuilders.java +++ b/core/src/main/java/org/elasticsearch/index/query/QueryBuilders.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.query; +import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.ShapeRelation; @@ -26,6 +27,7 @@ import org.elasticsearch.common.geo.builders.ShapeBuilder; import org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item; import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder; +import org.elasticsearch.index.query.support.InnerHitBuilder; import org.elasticsearch.index.search.MatchQuery; import org.elasticsearch.indices.TermsLookup; import org.elasticsearch.script.Script; @@ -483,25 +485,27 @@ public abstract class QueryBuilders { } /** - * Constructs a new NON scoring child query, with the child type and the query to run on the child documents. The + * Constructs a new has_child query, with the child type and the query to run on the child documents. The * results of this query are the parent docs that those child docs matched. * - * @param type The child type. - * @param query The query. + * @param type The child type. + * @param query The query. + * @param scoreMode How the scores from the children hits should be aggregated into the parent hit. */ - public static HasChildQueryBuilder hasChildQuery(String type, QueryBuilder query) { - return new HasChildQueryBuilder(type, query); + public static HasChildQueryBuilder hasChildQuery(String type, QueryBuilder query, ScoreMode scoreMode) { + return new HasChildQueryBuilder(type, query, scoreMode); } /** - * Constructs a new NON scoring parent query, with the parent type and the query to run on the parent documents. The + * Constructs a new parent query, with the parent type and the query to run on the parent documents. The * results of this query are the children docs that those parent docs matched. * - * @param type The parent type. - * @param query The query. + * @param type The parent type. + * @param query The query. + * @param score Whether the score from the parent hit should propogate to the child hit */ - public static HasParentQueryBuilder hasParentQuery(String type, QueryBuilder query) { - return new HasParentQueryBuilder(type, query); + public static HasParentQueryBuilder hasParentQuery(String type, QueryBuilder query, boolean score) { + return new HasParentQueryBuilder(type, query, score); } /** @@ -512,8 +516,8 @@ public abstract class QueryBuilders { return new ParentIdQueryBuilder(type, id); } - public static NestedQueryBuilder nestedQuery(String path, QueryBuilder query) { - return new NestedQueryBuilder(path, query); + public static NestedQueryBuilder nestedQuery(String path, QueryBuilder query, ScoreMode scoreMode) { + return new NestedQueryBuilder(path, query, scoreMode); } /** diff --git a/core/src/test/java/org/elasticsearch/aliases/IndexAliasesIT.java b/core/src/test/java/org/elasticsearch/aliases/IndexAliasesIT.java index b2a3a8b222f..08f8970f1e4 100644 --- a/core/src/test/java/org/elasticsearch/aliases/IndexAliasesIT.java +++ b/core/src/test/java/org/elasticsearch/aliases/IndexAliasesIT.java @@ -19,9 +19,9 @@ package org.elasticsearch.aliases; +import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.admin.indices.alias.Alias; -import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequestBuilder; import org.elasticsearch.action.admin.indices.alias.exists.AliasesExistResponse; @@ -1056,8 +1056,8 @@ public class IndexAliasesIT extends ESIntegTestCase { client().prepareIndex("my-index", "child", "2").setSource("{}").setParent("1").get(); refresh(); - assertAcked(admin().indices().prepareAliases().addAlias("my-index", "filter1", hasChildQuery("child", matchAllQuery()))); - assertAcked(admin().indices().prepareAliases().addAlias("my-index", "filter2", hasParentQuery("parent", matchAllQuery()))); + assertAcked(admin().indices().prepareAliases().addAlias("my-index", "filter1", hasChildQuery("child", matchAllQuery(), ScoreMode.None))); + assertAcked(admin().indices().prepareAliases().addAlias("my-index", "filter2", hasParentQuery("parent", matchAllQuery(), false))); SearchResponse response = client().prepareSearch("filter1").get(); assertHitCount(response, 1); diff --git a/core/src/test/java/org/elasticsearch/cluster/SpecificMasterNodesIT.java b/core/src/test/java/org/elasticsearch/cluster/SpecificMasterNodesIT.java index 792c28680e3..72db338ab80 100644 --- a/core/src/test/java/org/elasticsearch/cluster/SpecificMasterNodesIT.java +++ b/core/src/test/java/org/elasticsearch/cluster/SpecificMasterNodesIT.java @@ -19,6 +19,7 @@ package org.elasticsearch.cluster; +import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.cluster.metadata.MappingMetaData; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.discovery.MasterNotDiscoveredException; @@ -130,6 +131,6 @@ public class SpecificMasterNodesIT extends ESIntegTestCase { internalCluster().startNode(settingsBuilder().put(Node.NODE_DATA_SETTING.getKey(), true).put(Node.NODE_MASTER_SETTING.getKey(), false)); assertAcked(prepareCreate("test").addMapping("type1", "{\"type1\" : {\"properties\" : {\"table_a\" : { \"type\" : \"nested\", \"properties\" : {\"field_a\" : { \"type\" : \"keyword\" },\"field_b\" :{ \"type\" : \"keyword\" }}}}}}")); - client().admin().indices().prepareAliases().addAlias("test", "a_test", QueryBuilders.nestedQuery("table_a", QueryBuilders.termQuery("table_a.field_b", "y"))).get(); + client().admin().indices().prepareAliases().addAlias("test", "a_test", QueryBuilders.nestedQuery("table_a", QueryBuilders.termQuery("table_a.field_b", "y"), ScoreMode.Avg)).get(); } } diff --git a/core/src/test/java/org/elasticsearch/index/query/HasChildQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/HasChildQueryBuilderTests.java index 4b9b847b776..08a207940e0 100644 --- a/core/src/test/java/org/elasticsearch/index/query/HasChildQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/HasChildQueryBuilderTests.java @@ -120,13 +120,18 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase query = RandomQueryBuilder.createQuery(random()); - try { - new HasChildQueryBuilder(null, query); - fail("must not be null"); - } catch (IllegalArgumentException ex) { + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, + () -> QueryBuilders.hasChildQuery(null, query, ScoreMode.None)); + assertEquals("[has_child] requires 'type' field", e.getMessage()); - } + e = expectThrows(IllegalArgumentException.class, () -> QueryBuilders.hasChildQuery("foo", null, ScoreMode.None)); + assertEquals("[has_child] requires 'query' field", e.getMessage()); - try { - new HasChildQueryBuilder("foo", null); - fail("must not be null"); - } catch (IllegalArgumentException ex) { + e = expectThrows(IllegalArgumentException.class, () -> QueryBuilders.hasChildQuery("foo", query, null)); + assertEquals("[has_child] requires 'score_mode' field", e.getMessage()); - } - HasChildQueryBuilder foo = new HasChildQueryBuilder("foo", query);// all good - try { - foo.scoreMode(null); - fail("must not be null"); - } catch (IllegalArgumentException ex) { + int positiveValue = randomIntBetween(0, Integer.MAX_VALUE); + HasChildQueryBuilder foo = QueryBuilders.hasChildQuery("foo", query, ScoreMode.None); // all good + e = expectThrows(IllegalArgumentException.class, () -> foo.minMaxChildren(randomIntBetween(Integer.MIN_VALUE, -1), positiveValue)); + assertEquals("[has_child] requires non-negative 'min_children' field", e.getMessage()); - } - final int positiveValue = randomIntBetween(0, Integer.MAX_VALUE); - try { - foo.minChildren(randomIntBetween(Integer.MIN_VALUE, -1)); - fail("must not be negative"); - } catch (IllegalArgumentException ex) { + e = expectThrows(IllegalArgumentException.class, () -> foo.minMaxChildren(positiveValue, randomIntBetween(Integer.MIN_VALUE, -1))); + assertEquals("[has_child] requires non-negative 'max_children' field", e.getMessage()); - } - foo.minChildren(positiveValue); - assertEquals(positiveValue, foo.minChildren()); - try { - foo.maxChildren(randomIntBetween(Integer.MIN_VALUE, -1)); - fail("must not be negative"); - } catch (IllegalArgumentException ex) { - - } - - foo.maxChildren(positiveValue); - assertEquals(positiveValue, foo.maxChildren()); + e = expectThrows(IllegalArgumentException.class, () -> foo.minMaxChildren(positiveValue, positiveValue - 10)); + assertEquals("[has_child] 'max_children' is less than 'min_children'", e.getMessage()); } public void testFromJson() throws IOException { @@ -269,7 +256,7 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase failingQueryBuilder.toQuery(queryShardContext())); assertThat(e.getMessage(), containsString("[" + HasChildQueryBuilder.NAME + "] no mapping found for type [unmapped]")); diff --git a/core/src/test/java/org/elasticsearch/index/query/HasParentQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/HasParentQueryBuilderTests.java index c8c75be6696..7cd87f93fae 100644 --- a/core/src/test/java/org/elasticsearch/index/query/HasParentQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/HasParentQueryBuilderTests.java @@ -107,12 +107,16 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase QueryBuilders.hasParentQuery(null, query, false)); + assertThat(e.getMessage(), equalTo("[has_parent] requires 'type' field")); - try { - new HasParentQueryBuilder("foo", null); - fail("must not be null"); - } catch (IllegalArgumentException ex) { - } + e = expectThrows(IllegalArgumentException.class, + () -> QueryBuilders.hasParentQuery("foo", null, false)); + assertThat(e.getMessage(), equalTo("[has_parent] requires 'query' field")); QueryShardContext context = createShardContext(); - HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder("just_a_type", new MatchAllQueryBuilder()); - try { - queryBuilder.doToQuery(context); - } catch (QueryShardException e) { - assertThat(e.getMessage(), equalTo("[has_parent] no child types found for type [just_a_type]")); - } + HasParentQueryBuilder qb = QueryBuilders.hasParentQuery("just_a_type", new MatchAllQueryBuilder(), false); + QueryShardException qse = expectThrows(QueryShardException.class, () -> qb.doToQuery(context)); + assertThat(qse.getMessage(), equalTo("[has_parent] no child types found for type [just_a_type]")); } public void testDeprecatedXContent() throws IOException { @@ -210,7 +207,8 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase failingQueryBuilder.toQuery(queryShardContext())); assertThat(e.getMessage(), diff --git a/core/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java index 1ebca55cea0..4c679879ad5 100644 --- a/core/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java @@ -86,12 +86,16 @@ public class NestedQueryBuilderTests extends AbstractQueryTestCase innerQuery = RandomQueryBuilder.createQuery(random()); + IllegalArgumentException e = + expectThrows(IllegalArgumentException.class, () -> QueryBuilders.nestedQuery(null, innerQuery, ScoreMode.Avg)); + assertThat(e.getMessage(), equalTo("[nested] requires 'path' field")); - try { - new NestedQueryBuilder("path", null); - fail("cannot be null"); - } catch (IllegalArgumentException e) { - // expected - } + e = expectThrows(IllegalArgumentException.class, () -> QueryBuilders.nestedQuery("foo", null, ScoreMode.Avg)); + assertThat(e.getMessage(), equalTo("[nested] requires 'query' field")); - NestedQueryBuilder nestedQueryBuilder = new NestedQueryBuilder("path", new MatchAllQueryBuilder()); - try { - nestedQueryBuilder.scoreMode(null); - fail("cannot be null"); - } catch (IllegalArgumentException e) { - // expected - } + e = expectThrows(IllegalArgumentException.class, () -> QueryBuilders.nestedQuery("foo", innerQuery, null)); + assertThat(e.getMessage(), equalTo("[nested] requires 'score_mode' field")); } public void testFromJson() throws IOException { @@ -193,13 +186,13 @@ public class NestedQueryBuilderTests extends AbstractQueryTestCase failingQueryBuilder.toQuery(queryShardContext())); assertThat(e.getMessage(), containsString("[" + NestedQueryBuilder.NAME + "] failed to find nested object under path [unmapped]")); diff --git a/core/src/test/java/org/elasticsearch/index/query/QueryDSLDocumentationTests.java b/core/src/test/java/org/elasticsearch/index/query/QueryDSLDocumentationTests.java index cb9177369cf..4b634a2b3f0 100644 --- a/core/src/test/java/org/elasticsearch/index/query/QueryDSLDocumentationTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/QueryDSLDocumentationTests.java @@ -205,15 +205,15 @@ public class QueryDSLDocumentationTests extends ESTestCase { public void testHasChild() { hasChildQuery( "blog_tag", - termQuery("tag","something") - ); + termQuery("tag","something"), + ScoreMode.None); } public void testHasParent() { hasParentQuery( "blog", - termQuery("tag","something") - ); + termQuery("tag","something"), + false); } public void testIds() { @@ -262,9 +262,8 @@ public class QueryDSLDocumentationTests extends ESTestCase { "obj1", boolQuery() .must(matchQuery("obj1.name", "blue")) - .must(rangeQuery("obj1.count").gt(5)) - ) - .scoreMode(ScoreMode.Avg); + .must(rangeQuery("obj1.count").gt(5)), + ScoreMode.Avg); } public void testPrefix() { diff --git a/core/src/test/java/org/elasticsearch/percolator/MultiPercolatorIT.java b/core/src/test/java/org/elasticsearch/percolator/MultiPercolatorIT.java index 654d2b5934f..2d22a133811 100644 --- a/core/src/test/java/org/elasticsearch/percolator/MultiPercolatorIT.java +++ b/core/src/test/java/org/elasticsearch/percolator/MultiPercolatorIT.java @@ -396,7 +396,7 @@ public class MultiPercolatorIT extends ESIntegTestCase { ensureGreen("nestedindex"); client().prepareIndex("nestedindex", PercolatorFieldMapper.TYPE_NAME, "Q").setSource(jsonBuilder().startObject() - .field("query", QueryBuilders.nestedQuery("employee", QueryBuilders.matchQuery("employee.name", "virginia potts").operator(Operator.AND)).scoreMode(ScoreMode.Avg)).endObject()).get(); + .field("query", QueryBuilders.nestedQuery("employee", QueryBuilders.matchQuery("employee.name", "virginia potts").operator(Operator.AND), ScoreMode.Avg)).endObject()).get(); refresh(); diff --git a/core/src/test/java/org/elasticsearch/percolator/PercolatorIT.java b/core/src/test/java/org/elasticsearch/percolator/PercolatorIT.java index c0c6532bf94..799b84dd6d2 100644 --- a/core/src/test/java/org/elasticsearch/percolator/PercolatorIT.java +++ b/core/src/test/java/org/elasticsearch/percolator/PercolatorIT.java @@ -1564,7 +1564,7 @@ public class PercolatorIT extends ESIntegTestCase { ensureGreen("nestedindex"); client().prepareIndex("nestedindex", PercolatorFieldMapper.TYPE_NAME, "Q").setSource(jsonBuilder().startObject() - .field("query", QueryBuilders.nestedQuery("employee", QueryBuilders.matchQuery("employee.name", "virginia potts").operator(Operator.AND)).scoreMode(ScoreMode.Avg)).endObject()).get(); + .field("query", QueryBuilders.nestedQuery("employee", QueryBuilders.matchQuery("employee.name", "virginia potts").operator(Operator.AND), ScoreMode.Avg)).endObject()).get(); refresh(); @@ -1788,7 +1788,7 @@ public class PercolatorIT extends ESIntegTestCase { assertAcked(prepareCreate("index").addMapping("mapping", mapping)); try { client().prepareIndex("index", PercolatorFieldMapper.TYPE_NAME, "1") - .setSource(jsonBuilder().startObject().field("query", nestedQuery("nested", matchQuery("nested.name", "value")).innerHit(new InnerHitBuilder())).endObject()) + .setSource(jsonBuilder().startObject().field("query", nestedQuery("nested", matchQuery("nested.name", "value"), ScoreMode.Avg).innerHit(new InnerHitBuilder())).endObject()) .execute().actionGet(); fail("Expected a parse error, because inner_hits isn't supported in the percolate api"); } catch (Exception e) { @@ -1803,7 +1803,7 @@ public class PercolatorIT extends ESIntegTestCase { assertAcked(prepareCreate("index").addMapping("child", "_parent", "type=parent").addMapping("parent")); client().prepareIndex("index", PercolatorFieldMapper.TYPE_NAME, "1") - .setSource(jsonBuilder().startObject().field("query", hasChildQuery("child", matchAllQuery())).endObject()) + .setSource(jsonBuilder().startObject().field("query", hasChildQuery("child", matchAllQuery(), ScoreMode.None)).endObject()) .execute().actionGet(); } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/ChildrenIT.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/ChildrenIT.java index a6670c61d2e..6f2d7c8fb93 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/ChildrenIT.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/ChildrenIT.java @@ -18,6 +18,7 @@ */ package org.elasticsearch.search.aggregations.bucket; +import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.update.UpdateResponse; @@ -319,7 +320,7 @@ children("non-existing", "xyz") indexRandom(true, requests); SearchResponse response = client().prepareSearch(indexName).setTypes(masterType) - .setQuery(hasChildQuery(childType, termQuery("color", "orange"))) + .setQuery(hasChildQuery(childType, termQuery("color", "orange"), ScoreMode.None)) .addAggregation(children("my-refinements", childType) .subAggregation(terms("my-colors").field("color")) .subAggregation(terms("my-sizes").field("size")) diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsIT.java b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsIT.java index 3a0a0f25a62..36d021a5f3f 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsIT.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsIT.java @@ -19,6 +19,7 @@ package org.elasticsearch.search.aggregations.metrics; import org.apache.lucene.search.Explanation; +import org.apache.lucene.search.join.ScoreMode; import org.apache.lucene.util.ArrayUtil; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.search.SearchPhaseExecutionException; @@ -816,7 +817,7 @@ public class TopHitsIT extends ESIntegTestCase { SearchResponse searchResponse = client() .prepareSearch("articles") - .setQuery(nestedQuery("comments", matchQuery("comments.message", "comment").queryName("test"))) + .setQuery(nestedQuery("comments", matchQuery("comments.message", "comment").queryName("test"), ScoreMode.Avg)) .addAggregation( nested("to-comments", "comments").subAggregation( topHits("top-comments").size(1).highlighter(new HighlightBuilder().field(hlField)).explain(true) diff --git a/core/src/test/java/org/elasticsearch/search/child/ChildQuerySearchIT.java b/core/src/test/java/org/elasticsearch/search/child/ChildQuerySearchIT.java index d308c17ce54..1a258abc9cc 100644 --- a/core/src/test/java/org/elasticsearch/search/child/ChildQuerySearchIT.java +++ b/core/src/test/java/org/elasticsearch/search/child/ChildQuerySearchIT.java @@ -62,6 +62,7 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.boolQuery; import static org.elasticsearch.index.query.QueryBuilders.constantScoreQuery; import static org.elasticsearch.index.query.QueryBuilders.hasParentQuery; +import static org.elasticsearch.index.query.QueryBuilders.hasChildQuery; import static org.elasticsearch.index.query.QueryBuilders.idsQuery; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.elasticsearch.index.query.QueryBuilders.matchQuery; @@ -87,9 +88,6 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; -/** - * - */ @ClusterScope(scope = Scope.SUITE) public class ChildQuerySearchIT extends ESIntegTestCase { @@ -134,32 +132,33 @@ public class ChildQuerySearchIT extends ESIntegTestCase { .filter(hasChildQuery( "child", boolQuery().must(termQuery("c_field", "c_value1")) - .filter(hasChildQuery("grandchild", termQuery("gc_field", "gc_value1")))))).get(); + .filter(hasChildQuery("grandchild", termQuery("gc_field", "gc_value1"), ScoreMode.None)) + , ScoreMode.None))).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1")); searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", termQuery("p_field", "p_value1")))).execute() + .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", termQuery("p_field", "p_value1"), false))).execute() .actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("c1")); searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("child", termQuery("c_field", "c_value1")))).execute() + .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("child", termQuery("c_field", "c_value1"), false))).execute() .actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("gc1")); - searchResponse = client().prepareSearch("test").setQuery(hasParentQuery("parent", termQuery("p_field", "p_value1"))).execute() + searchResponse = client().prepareSearch("test").setQuery(hasParentQuery("parent", termQuery("p_field", "p_value1"), false)).execute() .actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("c1")); - searchResponse = client().prepareSearch("test").setQuery(hasParentQuery("child", termQuery("c_field", "c_value1"))).execute() + searchResponse = client().prepareSearch("test").setQuery(hasParentQuery("child", termQuery("c_field", "c_value1"), false)).execute() .actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); @@ -177,8 +176,9 @@ public class ChildQuerySearchIT extends ESIntegTestCase { client().prepareIndex("test", "foo", "1").setSource("foo", 1).get(); client().prepareIndex("test", "test").setSource("foo", 1).setParent("1").get(); refresh(); - SearchResponse searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("test", matchQuery("foo", 1))).execute() - .actionGet(); + SearchResponse searchResponse = client().prepareSearch("test"). + setQuery(hasChildQuery("test", matchQuery("foo", 1), ScoreMode.None)) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1")); @@ -287,11 +287,11 @@ public class ChildQuerySearchIT extends ESIntegTestCase { for (int i = 1; i <= 10; i++) { logger.info("Round {}", i); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasChildQuery("child", matchAllQuery()).scoreMode(ScoreMode.Max))) + .setQuery(constantScoreQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.Max))) .get(); assertNoFailures(searchResponse); searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasParentQuery("parent", matchAllQuery()).score(true))) + .setQuery(constantScoreQuery(hasParentQuery("parent", matchAllQuery(), true))) .get(); assertNoFailures(searchResponse); } @@ -305,7 +305,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { Map> parentToChildren = new HashMap<>(); // Childless parent client().prepareIndex("test", "parent", "p0").setSource("p_field", "p0").get(); - parentToChildren.put("p0", new HashSet()); + parentToChildren.put("p0", new HashSet<>()); String previousParentId = null; int numChildDocs = 32; @@ -332,7 +332,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { assertThat(parentToChildren.isEmpty(), equalTo(false)); for (Map.Entry> parentToChildrenEntry : parentToChildren.entrySet()) { SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasParentQuery("parent", termQuery("p_field", parentToChildrenEntry.getKey())))) + .setQuery(constantScoreQuery(hasParentQuery("parent", termQuery("p_field", parentToChildrenEntry.getKey()), false))) .setSize(numChildDocsPerParent).get(); assertNoFailures(searchResponse); @@ -369,39 +369,45 @@ public class ChildQuerySearchIT extends ESIntegTestCase { // HAS CHILD QUERY - SearchResponse searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("child", termQuery("c_field", "yellow"))).execute() - .actionGet(); + SearchResponse searchResponse = client().prepareSearch("test") + .setQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.None)) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1")); - searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("child", termQuery("c_field", "blue"))).execute() - .actionGet(); + searchResponse = client().prepareSearch("test") + .setQuery(hasChildQuery("child", termQuery("c_field", "blue"), ScoreMode.None)) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p2")); - searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("child", termQuery("c_field", "red"))).get(); + searchResponse = client().prepareSearch("test") + .setQuery(hasChildQuery("child", termQuery("c_field", "red"), ScoreMode.None)) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(2L)); assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("p2"), equalTo("p1"))); assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("p2"), equalTo("p1"))); // HAS CHILD FILTER - searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "yellow")))).get(); + .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.None))) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1")); - searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "blue")))) + searchResponse = client().prepareSearch("test") + .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "blue"), ScoreMode.None))) .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p2")); - searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "red")))) + searchResponse = client().prepareSearch("test") + .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "red"), ScoreMode.None))) .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(2L)); @@ -427,7 +433,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { SearchResponse searchResponse = client() .prepareSearch("test") - .setQuery(hasChildQuery("child", boolQuery().should(termQuery("c_field", "red")).should(termQuery("c_field", "yellow")))) + .setQuery(hasChildQuery("child", boolQuery().should(termQuery("c_field", "red")).should(termQuery("c_field", "yellow")), ScoreMode.None)) .addAggregation(AggregationBuilders.global("global").subAggregation( AggregationBuilders.filter("filter", boolQuery().should(termQuery("c_field", "red")).should(termQuery("c_field", "yellow"))).subAggregation( AggregationBuilders.terms("facet1").field("c_field")))).get(); @@ -462,7 +468,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { refresh(); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "yellow")))).get(); + .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.None))).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1")); @@ -474,7 +480,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { client().admin().indices().prepareRefresh().get(); searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "yellow")))).get(); + .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.None))).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1")); @@ -498,11 +504,12 @@ public class ChildQuerySearchIT extends ESIntegTestCase { refresh(); SearchResponse searchResponse = client().prepareSearch("test").setSearchType(SearchType.DFS_QUERY_THEN_FETCH) - .setQuery(boolQuery().mustNot(hasChildQuery("child", boolQuery().should(queryStringQuery("c_field:*"))))).get(); + .setQuery(boolQuery().mustNot(hasChildQuery("child", boolQuery().should(queryStringQuery("c_field:*")), ScoreMode.None))) + .get(); assertNoFailures(searchResponse); searchResponse = client().prepareSearch("test").setSearchType(SearchType.DFS_QUERY_THEN_FETCH) - .setQuery(boolQuery().mustNot(hasParentQuery("parent", boolQuery().should(queryStringQuery("p_field:*"))))).execute() + .setQuery(boolQuery().mustNot(hasParentQuery("parent", boolQuery().should(queryStringQuery("p_field:*")), false))).execute() .actionGet(); assertNoFailures(searchResponse); } @@ -521,12 +528,12 @@ public class ChildQuerySearchIT extends ESIntegTestCase { client().admin().indices().prepareFlush("test").get(); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", matchAllQuery()))).get(); + .setQuery(boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", matchAllQuery(), ScoreMode.None))).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", matchAllQuery()))).get(); + .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", matchAllQuery(), false))).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); } @@ -542,19 +549,21 @@ public class ChildQuerySearchIT extends ESIntegTestCase { client().prepareIndex("test", "child", "c1").setSource("c_field", "1").setParent(parentId).get(); refresh(); - SearchResponse countResponse = client().prepareSearch("test").setSize(0).setQuery(hasChildQuery("child", termQuery("c_field", "1")).scoreMode(ScoreMode.Max)) + SearchResponse countResponse = client().prepareSearch("test").setSize(0) + .setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max)) .get(); assertHitCount(countResponse, 1L); - countResponse = client().prepareSearch("test").setSize(0).setQuery(hasParentQuery("parent", termQuery("p_field", "1")).score(true)) + countResponse = client().prepareSearch("test").setSize(0).setQuery(hasParentQuery("parent", termQuery("p_field", "1"), true)) .get(); assertHitCount(countResponse, 1L); - countResponse = client().prepareSearch("test").setSize(0).setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "1")))) + countResponse = client().prepareSearch("test").setSize(0) + .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.None))) .get(); assertHitCount(countResponse, 1L); - countResponse = client().prepareSearch("test").setSize(0).setQuery(constantScoreQuery(hasParentQuery("parent", termQuery("p_field", "1")))) + countResponse = client().prepareSearch("test").setSize(0).setQuery(constantScoreQuery(hasParentQuery("parent", termQuery("p_field", "1"), false))) .get(); assertHitCount(countResponse, 1L); } @@ -572,20 +581,20 @@ public class ChildQuerySearchIT extends ESIntegTestCase { SearchResponse searchResponse = client().prepareSearch("test") .setExplain(true) - .setQuery(hasChildQuery("child", termQuery("c_field", "1")).scoreMode(ScoreMode.Max)) + .setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max)) .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).explanation().getDescription(), containsString("join value p1")); searchResponse = client().prepareSearch("test") .setExplain(true) - .setQuery(hasParentQuery("parent", termQuery("p_field", "1")).score(true)) + .setQuery(hasParentQuery("parent", termQuery("p_field", "1"), true)) .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).explanation().getDescription(), containsString("join value p1")); ExplainResponse explainResponse = client().prepareExplain("test", "parent", parentId) - .setQuery(hasChildQuery("child", termQuery("c_field", "1")).scoreMode(ScoreMode.Max)) + .setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max)) .get(); assertThat(explainResponse.isExists(), equalTo(true)); assertThat(explainResponse.getExplanation().getDetails()[0].getDescription(), containsString("join value p1")); @@ -662,7 +671,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { "child", QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0), fieldValueFactorFunction("c_field1")) - .boostMode(CombineFunction.REPLACE)).scoreMode(ScoreMode.Total)).get(); + .boostMode(CombineFunction.REPLACE), ScoreMode.Total)).get(); assertThat(response.getHits().totalHits(), equalTo(3L)); assertThat(response.getHits().hits()[0].id(), equalTo("1")); @@ -679,7 +688,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { "child", QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0), fieldValueFactorFunction("c_field1")) - .boostMode(CombineFunction.REPLACE)).scoreMode(ScoreMode.Max)).get(); + .boostMode(CombineFunction.REPLACE), ScoreMode.Max)).get(); assertThat(response.getHits().totalHits(), equalTo(3L)); assertThat(response.getHits().hits()[0].id(), equalTo("3")); @@ -696,7 +705,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { "child", QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0), fieldValueFactorFunction("c_field1")) - .boostMode(CombineFunction.REPLACE)).scoreMode(ScoreMode.Avg)).get(); + .boostMode(CombineFunction.REPLACE), ScoreMode.Avg)).get(); assertThat(response.getHits().totalHits(), equalTo(3L)); assertThat(response.getHits().hits()[0].id(), equalTo("3")); @@ -713,7 +722,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { "parent", QueryBuilders.functionScoreQuery(matchQuery("p_field1", "p_value3"), fieldValueFactorFunction("p_field2")) - .boostMode(CombineFunction.REPLACE)).score(true)) + .boostMode(CombineFunction.REPLACE), true)) .addSort(SortBuilders.fieldSort("c_field3")).addSort(SortBuilders.scoreSort()).get(); assertThat(response.getHits().totalHits(), equalTo(7L)); @@ -741,27 +750,27 @@ public class ChildQuerySearchIT extends ESIntegTestCase { ensureGreen(); SearchResponse response = client().prepareSearch("test") - .setQuery(QueryBuilders.hasChildQuery("child", matchQuery("text", "value"))).get(); + .setQuery(QueryBuilders.hasChildQuery("child", matchQuery("text", "value"), ScoreMode.None)).get(); assertNoFailures(response); assertThat(response.getHits().totalHits(), equalTo(0L)); client().prepareIndex("test", "child1").setSource(jsonBuilder().startObject().field("text", "value").endObject()).setRefresh(true) .get(); - response = client().prepareSearch("test").setQuery(QueryBuilders.hasChildQuery("child", matchQuery("text", "value"))).get(); + response = client().prepareSearch("test").setQuery(QueryBuilders.hasChildQuery("child", matchQuery("text", "value"), ScoreMode.None)).get(); assertNoFailures(response); assertThat(response.getHits().totalHits(), equalTo(0L)); - response = client().prepareSearch("test").setQuery(QueryBuilders.hasChildQuery("child", matchQuery("text", "value")).scoreMode(ScoreMode.Max)) + response = client().prepareSearch("test").setQuery(QueryBuilders.hasChildQuery("child", matchQuery("text", "value"), ScoreMode.Max)) .get(); assertNoFailures(response); assertThat(response.getHits().totalHits(), equalTo(0L)); - response = client().prepareSearch("test").setQuery(QueryBuilders.hasParentQuery("parent", matchQuery("text", "value"))).get(); + response = client().prepareSearch("test").setQuery(QueryBuilders.hasParentQuery("parent", matchQuery("text", "value"), false)).get(); assertNoFailures(response); assertThat(response.getHits().totalHits(), equalTo(0L)); - response = client().prepareSearch("test").setQuery(QueryBuilders.hasParentQuery("parent", matchQuery("text", "value")).score(true)) + response = client().prepareSearch("test").setQuery(QueryBuilders.hasParentQuery("parent", matchQuery("text", "value"), true)) .get(); assertNoFailures(response); assertThat(response.getHits().totalHits(), equalTo(0L)); @@ -781,13 +790,14 @@ public class ChildQuerySearchIT extends ESIntegTestCase { client().admin().indices().prepareFlush("test").get(); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", termQuery("c_field", 1)))).get(); + .setQuery(boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", termQuery("c_field", 1), ScoreMode.None))) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); assertThat(searchResponse.getHits().hits()[0].id(), equalTo("1")); searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", termQuery("p_field", 1)))).get(); + .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", termQuery("p_field", 1), false))).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); assertThat(searchResponse.getHits().hits()[0].id(), equalTo("2")); @@ -806,19 +816,21 @@ public class ChildQuerySearchIT extends ESIntegTestCase { refresh(); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", matchQuery("c_field", 1)))).get(); + .setQuery(boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", matchQuery("c_field", 1), ScoreMode.None))) + .get(); assertSearchHit(searchResponse, 1, hasId("1")); searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", matchQuery("p_field", 1)))).get(); + .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", matchQuery("p_field", 1), false))).get(); assertSearchHit(searchResponse, 1, hasId("2")); searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()).filter(boolQuery().must(hasChildQuery("child", matchQuery("c_field", 1))))).get(); + .setQuery(boolQuery().must(matchAllQuery()).filter(boolQuery().must(hasChildQuery("child", matchQuery("c_field", 1), ScoreMode.None)))) + .get(); assertSearchHit(searchResponse, 1, hasId("1")); searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()).filter(boolQuery().must(hasParentQuery("parent", matchQuery("p_field", 1))))).get(); + .setQuery(boolQuery().must(matchAllQuery()).filter(boolQuery().must(hasParentQuery("parent", matchQuery("p_field", 1), false)))).get(); assertSearchHit(searchResponse, 1, hasId("2")); } @@ -845,7 +857,8 @@ public class ChildQuerySearchIT extends ESIntegTestCase { SearchType[] searchTypes = new SearchType[]{SearchType.QUERY_THEN_FETCH, SearchType.DFS_QUERY_THEN_FETCH}; for (SearchType searchType : searchTypes) { SearchResponse searchResponse = client().prepareSearch("test").setSearchType(searchType) - .setQuery(hasChildQuery("child", prefixQuery("c_field", "c")).scoreMode(ScoreMode.Max)).addSort("p_field", SortOrder.ASC) + .setQuery(hasChildQuery("child", prefixQuery("c_field", "c"), ScoreMode.Max)) + .addSort("p_field", SortOrder.ASC) .setSize(5).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(10L)); @@ -856,7 +869,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { assertThat(searchResponse.getHits().hits()[4].id(), equalTo("p004")); searchResponse = client().prepareSearch("test").setSearchType(searchType) - .setQuery(hasParentQuery("parent", prefixQuery("p_field", "p")).score(true)).addSort("c_field", SortOrder.ASC) + .setQuery(hasParentQuery("parent", prefixQuery("p_field", "p"), true)).addSort("c_field", SortOrder.ASC) .setSize(5).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(500L)); @@ -886,7 +899,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { refresh(); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(hasChildQuery("child", termQuery("c_field", "yellow")).scoreMode(ScoreMode.Total)).get(); + .setQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.Total)).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1")); @@ -896,7 +909,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { .prepareSearch("test") .setQuery( boolQuery().must(matchQuery("c_field", "x")).must( - hasParentQuery("parent", termQuery("p_field", "p_value2")).score(true))).get(); + hasParentQuery("parent", termQuery("p_field", "p_value2"), true))).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(2L)); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("c3")); @@ -911,7 +924,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { client().admin().indices().prepareRefresh("test").get(); } - searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("child", termQuery("c_field", "yellow")).scoreMode(ScoreMode.Total)) + searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.Total)) .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); @@ -922,7 +935,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { .prepareSearch("test") .setQuery( boolQuery().must(matchQuery("c_field", "x")).must( - hasParentQuery("parent", termQuery("p_field", "p_value2")).score(true))).get(); + hasParentQuery("parent", termQuery("p_field", "p_value2"), true))).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(2L)); assertThat(searchResponse.getHits().getAt(0).id(), Matchers.anyOf(equalTo("c3"), equalTo("c4"))); @@ -945,7 +958,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { client().prepareIndex("test", "child", "c5").setSource("c_field", "x").setParent("p2").get(); refresh(); - SearchResponse searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("child", matchAllQuery()).scoreMode(ScoreMode.Total)) + SearchResponse searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.Total)) .setMinScore(3) // Score needs to be 3 or above! .get(); assertNoFailures(searchResponse); @@ -1035,7 +1048,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { client().admin().indices().prepareRefresh("test").get(); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "blue")))) + .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "blue"), ScoreMode.None))) .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); @@ -1044,7 +1057,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { client().admin().indices().prepareRefresh("test").get(); searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "blue")))) + .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "blue"), ScoreMode.None))) .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(2L)); @@ -1053,24 +1066,24 @@ public class ChildQuerySearchIT extends ESIntegTestCase { private QueryBuilder randomHasChild(String type, String field, String value) { if (randomBoolean()) { if (randomBoolean()) { - return constantScoreQuery(hasChildQuery(type, termQuery(field, value))); + return constantScoreQuery(hasChildQuery(type, termQuery(field, value), ScoreMode.None)); } else { - return boolQuery().must(matchAllQuery()).filter(hasChildQuery(type, termQuery(field, value))); + return boolQuery().must(matchAllQuery()).filter(hasChildQuery(type, termQuery(field, value), ScoreMode.None)); } } else { - return hasChildQuery(type, termQuery(field, value)); + return hasChildQuery(type, termQuery(field, value), ScoreMode.None); } } private QueryBuilder randomHasParent(String type, String field, String value) { if (randomBoolean()) { if (randomBoolean()) { - return constantScoreQuery(hasParentQuery(type, termQuery(field, value))); + return constantScoreQuery(hasParentQuery(type, termQuery(field, value), false)); } else { - return boolQuery().must(matchAllQuery()).filter(hasParentQuery(type, termQuery(field, value))); + return boolQuery().must(matchAllQuery()).filter(hasParentQuery(type, termQuery(field, value), false)); } } else { - return hasParentQuery(type, termQuery(field, value)); + return hasParentQuery(type, termQuery(field, value), false); } } @@ -1101,10 +1114,10 @@ public class ChildQuerySearchIT extends ESIntegTestCase { "child_type_one", boolQuery().must( queryStringQuery("name:William*").analyzeWildcard(true) - ) - ) - ) - ) + ), + ScoreMode.None) + ), + ScoreMode.None) ) ).get(); assertHitCount(searchResponse, 1L); @@ -1118,10 +1131,10 @@ public class ChildQuerySearchIT extends ESIntegTestCase { "child_type_two", boolQuery().must( queryStringQuery("name:William*").analyzeWildcard(true) - ) - ) - ) - ) + ), + ScoreMode.None) + ), + ScoreMode.None) ) ).get(); assertHitCount(searchResponse, 0L); @@ -1203,13 +1216,13 @@ public class ChildQuerySearchIT extends ESIntegTestCase { ScoreMode scoreMode = randomFrom(ScoreMode.values()); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(QueryBuilders.hasChildQuery("child", termQuery("c_field", "blue")).scoreMode(scoreMode)).filter(boolQuery().mustNot(termQuery("p_field", "3")))) + .setQuery(boolQuery().must(QueryBuilders.hasChildQuery("child", termQuery("c_field", "blue"), scoreMode)).filter(boolQuery().mustNot(termQuery("p_field", "3")))) .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(QueryBuilders.hasChildQuery("child", termQuery("c_field", "red")).scoreMode(scoreMode)).filter(boolQuery().mustNot(termQuery("p_field", "3")))) + .setQuery(boolQuery().must(QueryBuilders.hasChildQuery("child", termQuery("c_field", "red"), scoreMode)).filter(boolQuery().mustNot(termQuery("p_field", "3")))) .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(2L)); @@ -1226,25 +1239,25 @@ public class ChildQuerySearchIT extends ESIntegTestCase { client().prepareIndex("test", "child", "c1").setSource("c_field", "1").setParent(parentId).get(); refresh(); - SearchResponse searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("child", termQuery("c_field", "1")).scoreMode(ScoreMode.Max).queryName("test")) + SearchResponse searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max).queryName("test")) .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1)); assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("test")); - searchResponse = client().prepareSearch("test").setQuery(hasParentQuery("parent", termQuery("p_field", "1")).score(true).queryName("test")) + searchResponse = client().prepareSearch("test").setQuery(hasParentQuery("parent", termQuery("p_field", "1"), true).queryName("test")) .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1)); assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("test")); - searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "1")).queryName("test"))) + searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.None).queryName("test"))) .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1)); assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("test")); - searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(hasParentQuery("parent", termQuery("p_field", "1")).queryName("test"))) + searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(hasParentQuery("parent", termQuery("p_field", "1"), false).queryName("test"))) .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1)); @@ -1264,7 +1277,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { try { client().prepareSearch("test") - .setQuery(hasChildQuery("child", termQuery("c_field", "1"))) + .setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.None)) .get(); fail(); } catch (SearchPhaseExecutionException e) { @@ -1273,7 +1286,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { try { client().prepareSearch("test") - .setQuery(hasChildQuery("child", termQuery("c_field", "1")).scoreMode(ScoreMode.Max)) + .setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max)) .get(); fail(); } catch (SearchPhaseExecutionException e) { @@ -1282,7 +1295,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { try { client().prepareSearch("test") - .setPostFilter(hasChildQuery("child", termQuery("c_field", "1"))) + .setPostFilter(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.None)) .get(); fail(); } catch (SearchPhaseExecutionException e) { @@ -1291,7 +1304,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { try { client().prepareSearch("test") - .setQuery(hasParentQuery("parent", termQuery("p_field", "1")).score(true)) + .setQuery(hasParentQuery("parent", termQuery("p_field", "1"), true)) .get(); fail(); } catch (SearchPhaseExecutionException e) { @@ -1300,7 +1313,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { try { client().prepareSearch("test") - .setPostFilter(hasParentQuery("parent", termQuery("p_field", "1"))) + .setPostFilter(hasParentQuery("parent", termQuery("p_field", "1"), false)) .get(); fail(); } catch (SearchPhaseExecutionException e) { @@ -1360,7 +1373,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { for (int i = 0; i < 2; i++) { SearchResponse searchResponse = client().prepareSearch() .setQuery(boolQuery().must(matchAllQuery()).filter(boolQuery() - .must(QueryBuilders.hasChildQuery("child", matchQuery("c_field", "red"))) + .must(QueryBuilders.hasChildQuery("child", matchQuery("c_field", "red"), ScoreMode.None)) .must(matchAllQuery()))) .get(); assertThat(searchResponse.getHits().totalHits(), equalTo(2L)); @@ -1372,7 +1385,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { SearchResponse searchResponse = client().prepareSearch() .setQuery(boolQuery().must(matchAllQuery()).filter(boolQuery() - .must(QueryBuilders.hasChildQuery("child", matchQuery("c_field", "red"))) + .must(QueryBuilders.hasChildQuery("child", matchQuery("c_field", "red"), ScoreMode.None)) .must(matchAllQuery()))) .get(); @@ -1392,10 +1405,10 @@ public class ChildQuerySearchIT extends ESIntegTestCase { refresh(); QueryBuilder[] queries = new QueryBuilder[]{ - hasChildQuery("child", matchAllQuery()), - boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", matchAllQuery())), - hasParentQuery("parent", matchAllQuery()), - boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", matchAllQuery())) + hasChildQuery("child", matchAllQuery(), ScoreMode.None), + boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", matchAllQuery(), ScoreMode.None)), + hasParentQuery("parent", matchAllQuery(), false), + boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", matchAllQuery(), false)) }; for (QueryBuilder query : queries) { @@ -1436,7 +1449,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { SearchResponse resp; resp = client().prepareSearch("test") - .setSource(new SearchSourceBuilder().query(QueryBuilders.hasChildQuery("posts", QueryBuilders.matchQuery("field", "bar")))) + .setSource(new SearchSourceBuilder().query(QueryBuilders.hasChildQuery("posts", QueryBuilders.matchQuery("field", "bar"), ScoreMode.None))) .get(); assertHitCount(resp, 1L); } @@ -1473,22 +1486,22 @@ public class ChildQuerySearchIT extends ESIntegTestCase { indexRandom(true, indexRequests); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasParentQuery("parent", boolQuery().mustNot(termQuery("field1", "a"))))) + .setQuery(constantScoreQuery(hasParentQuery("parent", boolQuery().mustNot(termQuery("field1", "a")), false))) .get(); assertHitCount(searchResponse, 0L); searchResponse = client().prepareSearch("test") - .setQuery(hasParentQuery("parent", constantScoreQuery(boolQuery().mustNot(termQuery("field1", "a"))))) + .setQuery(hasParentQuery("parent", constantScoreQuery(boolQuery().mustNot(termQuery("field1", "a"))), false)) .get(); assertHitCount(searchResponse, 0L); searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasParentQuery("parent", termQuery("field1", "a")))) + .setQuery(constantScoreQuery(hasParentQuery("parent", termQuery("field1", "a"), false))) .get(); assertHitCount(searchResponse, 2L); searchResponse = client().prepareSearch("test") - .setQuery(hasParentQuery("parent", constantScoreQuery(termQuery("field1", "a")))) + .setQuery(hasParentQuery("parent", constantScoreQuery(termQuery("field1", "a")), false)) .get(); assertHitCount(searchResponse, 2L); } @@ -1538,11 +1551,8 @@ public class ChildQuerySearchIT extends ESIntegTestCase { new FunctionScoreQueryBuilder.FilterFunctionBuilder(weightFactorFunction(1)), new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("foo", "three"), weightFactorFunction(1)), new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("foo", "four"), weightFactorFunction(1)) - }).boostMode(CombineFunction.REPLACE).scoreMode(FiltersFunctionScoreQuery.ScoreMode.SUM)).scoreMode(scoreMode).minChildren(minChildren); - - if (maxChildren != null) { - hasChildQuery.maxChildren(maxChildren); - } + }).boostMode(CombineFunction.REPLACE).scoreMode(FiltersFunctionScoreQuery.ScoreMode.SUM), scoreMode) + .minMaxChildren(minChildren, maxChildren != null ? maxChildren : HasChildQueryBuilder.DEFAULT_MAX_CHILDREN); return client() .prepareSearch("test") @@ -1632,12 +1642,8 @@ public class ChildQuerySearchIT extends ESIntegTestCase { assertThat(response.getHits().hits()[0].id(), equalTo("3")); assertThat(response.getHits().hits()[0].score(), equalTo(1f)); - try { - response = minMaxQuery(ScoreMode.None, 3, 2); - fail(); - } catch (SearchPhaseExecutionException e) { - assertThat(e.toString(), containsString("[has_child] 'max_children' is less than 'min_children'")); - } + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> minMaxQuery(ScoreMode.None, 3, 2)); + assertThat(e.getMessage(), equalTo("[has_child] 'max_children' is less than 'min_children'")); // Score mode = SUM response = minMaxQuery(ScoreMode.Total, 0, null); @@ -1712,12 +1718,8 @@ public class ChildQuerySearchIT extends ESIntegTestCase { assertThat(response.getHits().hits()[0].id(), equalTo("3")); assertThat(response.getHits().hits()[0].score(), equalTo(3f)); - try { - response = minMaxQuery(ScoreMode.Total, 3, 2); - fail(); - } catch (SearchPhaseExecutionException e) { - assertThat(e.toString(), containsString("[has_child] 'max_children' is less than 'min_children'")); - } + e = expectThrows(IllegalArgumentException.class, () -> minMaxQuery(ScoreMode.Total, 3, 2)); + assertThat(e.getMessage(), equalTo("[has_child] 'max_children' is less than 'min_children'")); // Score mode = MAX response = minMaxQuery(ScoreMode.Max, 0, null); @@ -1792,12 +1794,8 @@ public class ChildQuerySearchIT extends ESIntegTestCase { assertThat(response.getHits().hits()[0].id(), equalTo("3")); assertThat(response.getHits().hits()[0].score(), equalTo(2f)); - try { - response = minMaxQuery(ScoreMode.Max, 3, 2); - fail(); - } catch (SearchPhaseExecutionException e) { - assertThat(e.toString(), containsString("[has_child] 'max_children' is less than 'min_children'")); - } + e = expectThrows(IllegalArgumentException.class, () -> minMaxQuery(ScoreMode.Max, 3, 2)); + assertThat(e.getMessage(), equalTo("[has_child] 'max_children' is less than 'min_children'")); // Score mode = AVG response = minMaxQuery(ScoreMode.Avg, 0, null); @@ -1872,12 +1870,8 @@ public class ChildQuerySearchIT extends ESIntegTestCase { assertThat(response.getHits().hits()[0].id(), equalTo("3")); assertThat(response.getHits().hits()[0].score(), equalTo(1.5f)); - try { - response = minMaxQuery(ScoreMode.Avg, 3, 2); - fail(); - } catch (SearchPhaseExecutionException e) { - assertThat(e.toString(), containsString("[has_child] 'max_children' is less than 'min_children'")); - } + e = expectThrows(IllegalArgumentException.class, () -> minMaxQuery(ScoreMode.Avg, 3, 2)); + assertThat(e.getMessage(), equalTo("[has_child] 'max_children' is less than 'min_children'")); } public void testParentFieldToNonExistingType() { @@ -1888,18 +1882,13 @@ public class ChildQuerySearchIT extends ESIntegTestCase { try { client().prepareSearch("test") - .setQuery(QueryBuilders.hasChildQuery("child", matchAllQuery())) + .setQuery(QueryBuilders.hasChildQuery("child", matchAllQuery(), ScoreMode.None)) .get(); fail(); } catch (SearchPhaseExecutionException e) { } } - static HasChildQueryBuilder hasChildQuery(String type, QueryBuilder queryBuilder) { - HasChildQueryBuilder hasChildQueryBuilder = QueryBuilders.hasChildQuery(type, queryBuilder); - return hasChildQueryBuilder; - } - public void testHasParentInnerQueryType() { assertAcked(prepareCreate("test").addMapping("parent-type").addMapping("child-type", "_parent", "type=parent-type")); client().prepareIndex("test", "child-type", "child-id").setParent("parent-id").setSource("{}").get(); @@ -1907,7 +1896,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { refresh(); //make sure that when we explicitly set a type, the inner query is executed in the context of the parent type instead SearchResponse searchResponse = client().prepareSearch("test").setTypes("child-type").setQuery( - QueryBuilders.hasParentQuery("parent-type", new IdsQueryBuilder().addIds("parent-id"))).get(); + QueryBuilders.hasParentQuery("parent-type", new IdsQueryBuilder().addIds("parent-id"), false)).get(); assertSearchHits(searchResponse, "child-id"); } @@ -1918,7 +1907,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase { refresh(); //make sure that when we explicitly set a type, the inner query is executed in the context of the child type instead SearchResponse searchResponse = client().prepareSearch("test").setTypes("parent-type").setQuery( - QueryBuilders.hasChildQuery("child-type", new IdsQueryBuilder().addIds("child-id"))).get(); + QueryBuilders.hasChildQuery("child-type", new IdsQueryBuilder().addIds("child-id"), ScoreMode.None)).get(); assertSearchHits(searchResponse, "parent-id"); } } diff --git a/core/src/test/java/org/elasticsearch/search/innerhits/InnerHitsIT.java b/core/src/test/java/org/elasticsearch/search/innerhits/InnerHitsIT.java index 630cd37f7e7..40b9086c8d7 100644 --- a/core/src/test/java/org/elasticsearch/search/innerhits/InnerHitsIT.java +++ b/core/src/test/java/org/elasticsearch/search/innerhits/InnerHitsIT.java @@ -19,6 +19,7 @@ package org.elasticsearch.search.innerhits; +import org.apache.lucene.search.join.ScoreMode; import org.apache.lucene.util.ArrayUtil; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.search.SearchRequest; @@ -118,9 +119,9 @@ public class InnerHitsIT extends ESIntegTestCase { ); // Inner hits can be defined in two ways: 1) with the query 2) as separate inner_hit definition SearchRequest[] searchRequests = new SearchRequest[]{ - client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "fox")) - .innerHit(new InnerHitBuilder().setName("comment"))).request(), - client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"))) + client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.Avg).innerHit( + new InnerHitBuilder().setName("comment"))).request(), + client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.Avg)) .innerHits(innerHitsBuilder).request() }; for (SearchRequest searchRequest : searchRequests) { @@ -148,12 +149,12 @@ public class InnerHitsIT extends ESIntegTestCase { // separate inner_hit definition searchRequests = new SearchRequest[] { client().prepareSearch("articles") - .setQuery(nestedQuery("comments", matchQuery("comments.message", "elephant"))) + .setQuery(nestedQuery("comments", matchQuery("comments.message", "elephant"), ScoreMode.Avg)) .innerHits(innerHitsBuilder).request(), client().prepareSearch("articles") - .setQuery(nestedQuery("comments", matchQuery("comments.message", "elephant")).innerHit(new InnerHitBuilder().setName("comment"))).request(), + .setQuery(nestedQuery("comments", matchQuery("comments.message", "elephant"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("comment"))).request(), client().prepareSearch("articles") - .setQuery(nestedQuery("comments", matchQuery("comments.message", "elephant")).innerHit(new InnerHitBuilder().setName("comment").addSort(new FieldSortBuilder("_doc").order(SortOrder.DESC)))).request() + .setQuery(nestedQuery("comments", matchQuery("comments.message", "elephant"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("comment").addSort(new FieldSortBuilder("_doc").order(SortOrder.DESC)))).request() }; for (SearchRequest searchRequest : searchRequests) { SearchResponse response = client().search(searchRequest).actionGet(); @@ -187,10 +188,10 @@ public class InnerHitsIT extends ESIntegTestCase { innerHitsBuilder.addInnerHit("comments", innerHit); searchRequests = new SearchRequest[] { client().prepareSearch("articles") - .setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"))) + .setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.Avg)) .innerHits(innerHitsBuilder).request(), client().prepareSearch("articles") - .setQuery(nestedQuery("comments", matchQuery("comments.message", "fox")).innerHit( + .setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.Avg).innerHit( new InnerHitBuilder().setHighlightBuilder(new HighlightBuilder().field("comments.message")) .setExplain(true) .addFieldDataField("comments.message") @@ -252,14 +253,14 @@ public class InnerHitsIT extends ESIntegTestCase { } else { BoolQueryBuilder boolQuery = new BoolQueryBuilder(); if (randomBoolean()) { - boolQuery.should(nestedQuery("field1", matchAllQuery()).innerHit(new InnerHitBuilder().setName("a").setSize(size) + boolQuery.should(nestedQuery("field1", matchAllQuery(), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("a").setSize(size) .addSort(new FieldSortBuilder("_doc").order(SortOrder.DESC)))); - boolQuery.should(nestedQuery("field2", matchAllQuery()).innerHit(new InnerHitBuilder().setName("b") + boolQuery.should(nestedQuery("field2", matchAllQuery(), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("b") .addSort(new FieldSortBuilder("_doc").order(SortOrder.DESC)).setSize(size))); } else { - boolQuery.should(constantScoreQuery(nestedQuery("field1", matchAllQuery()).innerHit(new InnerHitBuilder().setName("a") + boolQuery.should(constantScoreQuery(nestedQuery("field1", matchAllQuery(), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("a") .setSize(size).addSort(new FieldSortBuilder("_doc").order(SortOrder.DESC))))); - boolQuery.should(constantScoreQuery(nestedQuery("field2", matchAllQuery()).innerHit(new InnerHitBuilder().setName("b") + boolQuery.should(constantScoreQuery(nestedQuery("field2", matchAllQuery(), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("b") .setSize(size).addSort(new FieldSortBuilder("_doc").order(SortOrder.DESC))))); } searchResponse = client().prepareSearch("idx") @@ -317,11 +318,11 @@ public class InnerHitsIT extends ESIntegTestCase { .setQuery(matchQuery("message", "fox"))); SearchRequest[] searchRequests = new SearchRequest[]{ client().prepareSearch("articles") - .setQuery(hasChildQuery("comment", matchQuery("message", "fox"))) + .setQuery(hasChildQuery("comment", matchQuery("message", "fox"), ScoreMode.None)) .innerHits(innerHitsBuilder) .request(), client().prepareSearch("articles") - .setQuery(hasChildQuery("comment", matchQuery("message", "fox")).innerHit(new InnerHitBuilder().setName("comment"))) + .setQuery(hasChildQuery("comment", matchQuery("message", "fox"), ScoreMode.None).innerHit(new InnerHitBuilder().setName("comment"))) .request() }; for (SearchRequest searchRequest : searchRequests) { @@ -346,11 +347,11 @@ public class InnerHitsIT extends ESIntegTestCase { .setQuery(matchQuery("message", "elephant"))); searchRequests = new SearchRequest[] { client().prepareSearch("articles") - .setQuery(hasChildQuery("comment", matchQuery("message", "elephant"))) + .setQuery(hasChildQuery("comment", matchQuery("message", "elephant"), ScoreMode.None)) .innerHits(innerHitsBuilder) .request(), client().prepareSearch("articles") - .setQuery(hasChildQuery("comment", matchQuery("message", "elephant")).innerHit(new InnerHitBuilder())) + .setQuery(hasChildQuery("comment", matchQuery("message", "elephant"), ScoreMode.None).innerHit(new InnerHitBuilder())) .request() }; for (SearchRequest searchRequest : searchRequests) { @@ -382,13 +383,13 @@ public class InnerHitsIT extends ESIntegTestCase { innerHitsBuilder.addInnerHit("comment", innerHit); searchRequests = new SearchRequest[] { client().prepareSearch("articles") - .setQuery(hasChildQuery("comment", matchQuery("message", "fox"))) + .setQuery(hasChildQuery("comment", matchQuery("message", "fox"), ScoreMode.None)) .innerHits(innerHitsBuilder) .request(), client().prepareSearch("articles") .setQuery( - hasChildQuery("comment", matchQuery("message", "fox")).innerHit( + hasChildQuery("comment", matchQuery("message", "fox"), ScoreMode.None).innerHit( new InnerHitBuilder() .addFieldDataField("message") .setHighlightBuilder(new HighlightBuilder().field("message")) @@ -455,11 +456,11 @@ public class InnerHitsIT extends ESIntegTestCase { } else { BoolQueryBuilder boolQuery = new BoolQueryBuilder(); if (randomBoolean()) { - boolQuery.should(hasChildQuery("child1", matchAllQuery()).innerHit(new InnerHitBuilder().setName("a").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size))); - boolQuery.should(hasChildQuery("child2", matchAllQuery()).innerHit(new InnerHitBuilder().setName("b").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size))); + boolQuery.should(hasChildQuery("child1", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("a").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size))); + boolQuery.should(hasChildQuery("child2", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("b").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size))); } else { - boolQuery.should(constantScoreQuery(hasChildQuery("child1", matchAllQuery()).innerHit(new InnerHitBuilder().setName("a").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size)))); - boolQuery.should(constantScoreQuery(hasChildQuery("child2", matchAllQuery()).innerHit(new InnerHitBuilder().setName("b").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size)))); + boolQuery.should(constantScoreQuery(hasChildQuery("child1", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("a").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size)))); + boolQuery.should(constantScoreQuery(hasChildQuery("child2", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("b").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size)))); } searchResponse = client().prepareSearch("idx") .setSize(numDocs) @@ -523,7 +524,7 @@ public class InnerHitsIT extends ESIntegTestCase { .setQuery( boolQuery() .must(matchQuery("body", "fail2ban")) - .must(hasParentQuery("question", matchAllQuery()).innerHit(new InnerHitBuilder())) + .must(hasParentQuery("question", matchAllQuery(), false).innerHit(new InnerHitBuilder())) ).get(); assertNoFailures(response); assertHitCount(response, 2); @@ -567,10 +568,10 @@ public class InnerHitsIT extends ESIntegTestCase { InnerHitsBuilder innerHitsBuilder = new InnerHitsBuilder(); innerHitsBuilder.addInnerHit("comment", new InnerHitBuilder() .setParentChildType("comment") - .setQuery(hasChildQuery("remark", matchQuery("message", "good"))) + .setQuery(hasChildQuery("remark", matchQuery("message", "good"), ScoreMode.None)) .setInnerHitsBuilder(innerInnerHitsBuilder)); SearchResponse response = client().prepareSearch("articles") - .setQuery(hasChildQuery("comment", hasChildQuery("remark", matchQuery("message", "good")))) + .setQuery(hasChildQuery("comment", hasChildQuery("remark", matchQuery("message", "good"), ScoreMode.None), ScoreMode.None)) .innerHits(innerHitsBuilder) .get(); @@ -596,10 +597,10 @@ public class InnerHitsIT extends ESIntegTestCase { innerHitsBuilder = new InnerHitsBuilder(); innerHitsBuilder.addInnerHit("comment", new InnerHitBuilder() .setParentChildType("comment") - .setQuery(hasChildQuery("remark", matchQuery("message", "bad"))) + .setQuery(hasChildQuery("remark", matchQuery("message", "bad"), ScoreMode.None)) .setInnerHitsBuilder(innerInnerHitsBuilder)); response = client().prepareSearch("articles") - .setQuery(hasChildQuery("comment", hasChildQuery("remark", matchQuery("message", "bad")))) + .setQuery(hasChildQuery("comment", hasChildQuery("remark", matchQuery("message", "bad"), ScoreMode.None), ScoreMode.None)) .innerHits(innerHitsBuilder) .get(); @@ -668,11 +669,11 @@ public class InnerHitsIT extends ESIntegTestCase { InnerHitsBuilder innerHitsBuilder = new InnerHitsBuilder(); innerHitsBuilder.addInnerHit("comment", new InnerHitBuilder() .setNestedPath("comments") - .setQuery(nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "good"))) + .setQuery(nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "good"), ScoreMode.Avg)) .setInnerHitsBuilder(innerInnerHitsBuilder) ); SearchResponse response = client().prepareSearch("articles") - .setQuery(nestedQuery("comments", nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "good")))) + .setQuery(nestedQuery("comments", nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "good"), ScoreMode.Avg), ScoreMode.Avg)) .innerHits(innerHitsBuilder).get(); assertNoFailures(response); assertHitCount(response, 1); @@ -695,7 +696,7 @@ public class InnerHitsIT extends ESIntegTestCase { // Directly refer to the second level: response = client().prepareSearch("articles") - .setQuery(nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "bad")).innerHit(new InnerHitBuilder())) + .setQuery(nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "bad"), ScoreMode.Avg).innerHit(new InnerHitBuilder())) .get(); assertNoFailures(response); assertHitCount(response, 1); @@ -717,10 +718,10 @@ public class InnerHitsIT extends ESIntegTestCase { innerHitsBuilder = new InnerHitsBuilder(); innerHitsBuilder.addInnerHit("comment", new InnerHitBuilder() .setNestedPath("comments") - .setQuery(nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "bad"))) + .setQuery(nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "bad"), ScoreMode.Avg)) .setInnerHitsBuilder(innerInnerHitsBuilder)); response = client().prepareSearch("articles") - .setQuery(nestedQuery("comments", nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "bad")))) + .setQuery(nestedQuery("comments", nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "bad"), ScoreMode.Avg), ScoreMode.Avg)) .innerHits(innerHitsBuilder) .get(); assertNoFailures(response); @@ -755,7 +756,7 @@ public class InnerHitsIT extends ESIntegTestCase { indexRandom(true, requests); SearchResponse response = client().prepareSearch("articles") - .setQuery(nestedQuery("comments", matchQuery("comments.message", "fox")).innerHit(new InnerHitBuilder())) + .setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.Avg).innerHit(new InnerHitBuilder())) .get(); assertNoFailures(response); assertHitCount(response, 1); @@ -795,7 +796,7 @@ public class InnerHitsIT extends ESIntegTestCase { indexRandom(true, requests); SearchResponse response = client().prepareSearch("articles") - .setQuery(nestedQuery("comments.messages", matchQuery("comments.messages.message", "fox")).innerHit(new InnerHitBuilder())) + .setQuery(nestedQuery("comments.messages", matchQuery("comments.messages.message", "fox"), ScoreMode.Avg).innerHit(new InnerHitBuilder())) .get(); assertNoFailures(response); assertHitCount(response, 1); @@ -807,7 +808,7 @@ public class InnerHitsIT extends ESIntegTestCase { assertThat(response.getHits().getAt(0).getInnerHits().get("comments.messages").getAt(0).getNestedIdentity().getChild(), nullValue()); response = client().prepareSearch("articles") - .setQuery(nestedQuery("comments.messages", matchQuery("comments.messages.message", "bear")).innerHit(new InnerHitBuilder())) + .setQuery(nestedQuery("comments.messages", matchQuery("comments.messages.message", "bear"), ScoreMode.Avg).innerHit(new InnerHitBuilder())) .get(); assertNoFailures(response); assertHitCount(response, 1); @@ -826,7 +827,7 @@ public class InnerHitsIT extends ESIntegTestCase { .endObject())); indexRandom(true, requests); response = client().prepareSearch("articles") - .setQuery(nestedQuery("comments.messages", matchQuery("comments.messages.message", "fox")).innerHit(new InnerHitBuilder())) + .setQuery(nestedQuery("comments.messages", matchQuery("comments.messages.message", "fox"), ScoreMode.Avg).innerHit(new InnerHitBuilder())) .get(); assertNoFailures(response); assertHitCount(response, 1); @@ -987,8 +988,8 @@ public class InnerHitsIT extends ESIntegTestCase { .setQuery(nestedQuery("nested1", boolQuery() .should(termQuery("nested1.n_field1", "n_value1_1").queryName("test1")) .should(termQuery("nested1.n_field1", "n_value1_3").queryName("test2")) - .should(termQuery("nested1.n_field2", "n_value2_2").queryName("test3")) - ).innerHit(new InnerHitBuilder().addSort(new FieldSortBuilder("nested1.n_field1").order(SortOrder.ASC)))) + .should(termQuery("nested1.n_field2", "n_value2_2").queryName("test3")), + ScoreMode.Avg).innerHit(new InnerHitBuilder().addSort(new FieldSortBuilder("nested1.n_field1").order(SortOrder.ASC)))) .setSize(numDocs) .addSort("field1", SortOrder.ASC) .get(); @@ -1027,7 +1028,7 @@ public class InnerHitsIT extends ESIntegTestCase { indexRandom(true, requests); SearchResponse response = client().prepareSearch("index") - .setQuery(hasChildQuery("child", matchQuery("field", "value1").queryName("_name1")).innerHit(new InnerHitBuilder())) + .setQuery(hasChildQuery("child", matchQuery("field", "value1").queryName("_name1"), ScoreMode.None).innerHit(new InnerHitBuilder())) .addSort("_uid", SortOrder.ASC) .get(); assertHitCount(response, 2); @@ -1042,7 +1043,7 @@ public class InnerHitsIT extends ESIntegTestCase { assertThat(response.getHits().getAt(1).getInnerHits().get("child").getAt(0).getMatchedQueries()[0], equalTo("_name1")); response = client().prepareSearch("index") - .setQuery(hasChildQuery("child", matchQuery("field", "value2").queryName("_name2")).innerHit(new InnerHitBuilder())) + .setQuery(hasChildQuery("child", matchQuery("field", "value2").queryName("_name2"), ScoreMode.None).innerHit(new InnerHitBuilder())) .addSort("_uid", SortOrder.ASC) .get(); assertHitCount(response, 1); @@ -1060,7 +1061,7 @@ public class InnerHitsIT extends ESIntegTestCase { indexRandom(true, requests); SearchResponse response = client().prepareSearch("index1") - .setQuery(hasChildQuery("child", matchQuery("field", "value1")).innerHit(new InnerHitBuilder().setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1))) + .setQuery(hasChildQuery("child", matchQuery("field", "value1"), ScoreMode.None).innerHit(new InnerHitBuilder().setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1))) .addSort("_uid", SortOrder.ASC) .get(); assertNoFailures(response); @@ -1078,7 +1079,7 @@ public class InnerHitsIT extends ESIntegTestCase { .get(); response = client().prepareSearch("index2") - .setQuery(nestedQuery("nested", matchQuery("nested.field", "value1")).innerHit(new InnerHitBuilder().setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1))) + .setQuery(nestedQuery("nested", matchQuery("nested.field", "value1"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1))) .addSort("_uid", SortOrder.ASC) .get(); assertNoFailures(response); @@ -1097,7 +1098,7 @@ public class InnerHitsIT extends ESIntegTestCase { InnerHitsBuilder innerHitsBuilder = new InnerHitsBuilder(); innerHitsBuilder.addInnerHit("my-inner-hit", new InnerHitBuilder().setParentChildType("child")); SearchResponse response = client().prepareSearch("index1") - .setQuery(hasChildQuery("child", new MatchAllQueryBuilder()).innerHit(new InnerHitBuilder())) + .setQuery(hasChildQuery("child", new MatchAllQueryBuilder(), ScoreMode.None).innerHit(new InnerHitBuilder())) .innerHits(innerHitsBuilder) .get(); assertHitCount(response, 1); diff --git a/core/src/test/java/org/elasticsearch/search/nested/SimpleNestedIT.java b/core/src/test/java/org/elasticsearch/search/nested/SimpleNestedIT.java index 2b235b1b3fc..4d2f92c3c6c 100644 --- a/core/src/test/java/org/elasticsearch/search/nested/SimpleNestedIT.java +++ b/core/src/test/java/org/elasticsearch/search/nested/SimpleNestedIT.java @@ -100,11 +100,11 @@ public class SimpleNestedIT extends ESIntegTestCase { assertThat(searchResponse.getHits().totalHits(), equalTo(0L)); // now, do a nested query - searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"))).get(); + searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"), ScoreMode.Avg)).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); - searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"))).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).get(); + searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"), ScoreMode.Avg)).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); @@ -129,19 +129,19 @@ public class SimpleNestedIT extends ESIntegTestCase { assertDocumentCount("test", 6); searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", - boolQuery().must(termQuery("nested1.n_field1", "n_value1_1")).must(termQuery("nested1.n_field2", "n_value2_1")))).execute().actionGet(); + boolQuery().must(termQuery("nested1.n_field1", "n_value1_1")).must(termQuery("nested1.n_field2", "n_value2_1")), ScoreMode.Avg)).execute().actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); // filter searchResponse = client().prepareSearch("test").setQuery(boolQuery().must(matchAllQuery()).mustNot(nestedQuery("nested1", - boolQuery().must(termQuery("nested1.n_field1", "n_value1_1")).must(termQuery("nested1.n_field2", "n_value2_1"))))).execute().actionGet(); + boolQuery().must(termQuery("nested1.n_field1", "n_value1_1")).must(termQuery("nested1.n_field2", "n_value2_1")), ScoreMode.Avg))).execute().actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); // check with type prefix searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", - boolQuery().must(termQuery("nested1.n_field1", "n_value1_1")).must(termQuery("nested1.n_field2", "n_value2_1")))).execute().actionGet(); + boolQuery().must(termQuery("nested1.n_field1", "n_value1_1")).must(termQuery("nested1.n_field2", "n_value2_1")), ScoreMode.Avg)).execute().actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); @@ -153,11 +153,11 @@ public class SimpleNestedIT extends ESIntegTestCase { flush(); assertDocumentCount("test", 3); - searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"))).execute().actionGet(); + searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"), ScoreMode.Avg)).execute().actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); - searchResponse = client().prepareSearch("test").setTypes("type1", "type2").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"))).execute().actionGet(); + searchResponse = client().prepareSearch("test").setTypes("type1", "type2").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"), ScoreMode.Avg)).execute().actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); } @@ -191,42 +191,42 @@ public class SimpleNestedIT extends ESIntegTestCase { // do some multi nested queries SearchResponse searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", - termQuery("nested1.field1", "1"))).execute().actionGet(); + termQuery("nested1.field1", "1"), ScoreMode.Avg)).execute().actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1.nested2", - termQuery("nested1.nested2.field2", "2"))).execute().actionGet(); + termQuery("nested1.nested2.field2", "2"), ScoreMode.Avg)).execute().actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", - boolQuery().must(termQuery("nested1.field1", "1")).must(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "2"))))).execute().actionGet(); + boolQuery().must(termQuery("nested1.field1", "1")).must(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "2"), ScoreMode.Avg)), ScoreMode.Avg)).execute().actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", - boolQuery().must(termQuery("nested1.field1", "1")).must(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "3"))))).execute().actionGet(); + boolQuery().must(termQuery("nested1.field1", "1")).must(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "3"), ScoreMode.Avg)), ScoreMode.Avg)).execute().actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", - boolQuery().must(termQuery("nested1.field1", "1")).must(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "4"))))).execute().actionGet(); + boolQuery().must(termQuery("nested1.field1", "1")).must(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "4"), ScoreMode.Avg)), ScoreMode.Avg)).execute().actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(0L)); searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", - boolQuery().must(termQuery("nested1.field1", "1")).must(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "5"))))).execute().actionGet(); + boolQuery().must(termQuery("nested1.field1", "1")).must(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "5"), ScoreMode.Avg)), ScoreMode.Avg)).execute().actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(0L)); searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", - boolQuery().must(termQuery("nested1.field1", "4")).must(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "5"))))).execute().actionGet(); + boolQuery().must(termQuery("nested1.field1", "4")).must(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "5"), ScoreMode.Avg)), ScoreMode.Avg)).execute().actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(1L)); searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", - boolQuery().must(termQuery("nested1.field1", "4")).must(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "2"))))).execute().actionGet(); + boolQuery().must(termQuery("nested1.field1", "4")).must(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "2"), ScoreMode.Avg)), ScoreMode.Avg)).execute().actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(0L)); } @@ -310,7 +310,7 @@ public class SimpleNestedIT extends ESIntegTestCase { .execute().actionGet(); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1")).scoreMode(ScoreMode.Total)) + .setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1"), ScoreMode.Total)) .setExplain(true) .execute().actionGet(); assertNoFailures(searchResponse); @@ -988,7 +988,7 @@ public class SimpleNestedIT extends ESIntegTestCase { .addSort(SortBuilders.fieldSort("users.first") .order(SortOrder.ASC) .setNestedPath("users") - .setNestedFilter(nestedQuery("users.workstations", termQuery("users.workstations.stationid", "s5")))) + .setNestedFilter(nestedQuery("users.workstations", termQuery("users.workstations.stationid", "s5"), ScoreMode.Avg))) .get(); assertNoFailures(searchResponse); assertHitCount(searchResponse, 2); @@ -1044,7 +1044,7 @@ public class SimpleNestedIT extends ESIntegTestCase { // only when querying with nested the fixed bitsets are loaded SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(nestedQuery("array1", termQuery("array1.field1", "value1"))) + .setQuery(nestedQuery("array1", termQuery("array1.field1", "value1"), ScoreMode.Avg)) .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(5L)); diff --git a/core/src/test/java/org/elasticsearch/search/query/SearchQueryIT.java b/core/src/test/java/org/elasticsearch/search/query/SearchQueryIT.java index fecdf6ff074..3bcad294a81 100644 --- a/core/src/test/java/org/elasticsearch/search/query/SearchQueryIT.java +++ b/core/src/test/java/org/elasticsearch/search/query/SearchQueryIT.java @@ -19,6 +19,7 @@ package org.elasticsearch.search.query; +import org.apache.lucene.search.join.ScoreMode; import org.apache.lucene.util.English; import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder; import org.elasticsearch.action.index.IndexRequestBuilder; @@ -1773,7 +1774,7 @@ public class SearchQueryIT extends ESIntegTestCase { //has_child fails if executed on "simple" index try { client().prepareSearch("simple") - .setQuery(hasChildQuery("child", matchQuery("text", "value"))).get(); + .setQuery(hasChildQuery("child", matchQuery("text", "value"), ScoreMode.None)).get(); fail("Should have failed as has_child query can only be executed against parent-child types"); } catch (SearchPhaseExecutionException e) { assertThat(e.shardFailures().length, greaterThan(0)); @@ -1784,7 +1785,7 @@ public class SearchQueryIT extends ESIntegTestCase { //has_child doesn't get parsed for "simple" index SearchResponse searchResponse = client().prepareSearch("related", "simple") - .setQuery(indicesQuery(hasChildQuery("child", matchQuery("text", "value2")), "related") + .setQuery(indicesQuery(hasChildQuery("child", matchQuery("text", "value2"), ScoreMode.None), "related") .noMatchQuery(matchQuery("text", "value1"))).get(); assertHitCount(searchResponse, 2L); assertSearchHits(searchResponse, "1", "2"); diff --git a/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/BulkTests.java b/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/BulkTests.java index 356d0f28164..dd9daa75e76 100644 --- a/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/BulkTests.java +++ b/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/BulkTests.java @@ -433,7 +433,7 @@ public class BulkTests extends ESIntegTestCase { //we check that the _parent field was set on the child document by using the has parent query SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(QueryBuilders.hasParentQuery("parent", QueryBuilders.matchAllQuery())) + .setQuery(QueryBuilders.hasParentQuery("parent", QueryBuilders.matchAllQuery(), false)) .get(); assertNoFailures(searchResponse); @@ -468,7 +468,7 @@ public class BulkTests extends ESIntegTestCase { client().admin().indices().prepareRefresh("test").get(); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(QueryBuilders.hasParentQuery("parent", QueryBuilders.matchAllQuery())) + .setQuery(QueryBuilders.hasParentQuery("parent", QueryBuilders.matchAllQuery(), false)) .get(); assertSearchHits(searchResponse, "child1"); diff --git a/modules/reindex/src/test/java/org/elasticsearch/index/reindex/ReindexParentChildTests.java b/modules/reindex/src/test/java/org/elasticsearch/index/reindex/ReindexParentChildTests.java index fcc678a7d6d..b971e125b94 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/index/reindex/ReindexParentChildTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/index/reindex/ReindexParentChildTests.java @@ -102,8 +102,8 @@ public class ReindexParentChildTests extends ReindexTestCase { .setSource("foo", "bar").setRouting("united states")); findsCountry = idsQuery("country").addIds("united states"); - findsCity = hasParentQuery("country", findsCountry); - findsNeighborhood = hasParentQuery("city", findsCity); + findsCity = hasParentQuery("country", findsCountry, false); + findsNeighborhood = hasParentQuery("city", findsCity, false); // Make sure we built the parent/child relationship assertSearchHits(client().prepareSearch(indexName).setQuery(findsCity).get(), "pittsburgh");