mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-08 22:14:59 +00:00
Cleanup query builder for inner hits construction.
* Inner hits can now only be provided and prepared via setter in the nested, has_child and has_parent query. * Also made `score_mode` a required constructor parameter. * Moved has_child's min_child/max_children validation from doToQuery(...) to a setter.
This commit is contained in:
parent
c9ada75e35
commit
2928fd6ef3
@ -272,4 +272,12 @@ public abstract class AbstractQueryBuilder<QB extends AbstractQueryBuilder<QB>>
|
|||||||
protected QueryBuilder<?> doRewrite(QueryRewriteContext queryShardContext) throws IOException {
|
protected QueryBuilder<?> doRewrite(QueryRewriteContext queryShardContext) throws IOException {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Like Objects.requireNotNull(...) but instead throws a IllegalArgumentException
|
||||||
|
protected static <T> T requireValue(T value, String message) {
|
||||||
|
if (value == null) {
|
||||||
|
throw new IllegalArgumentException(message);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ import java.util.Locale;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A query builder for <tt>has_child</tt> queries.
|
* A query builder for <tt>has_child</tt> query.
|
||||||
*/
|
*/
|
||||||
public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuilder> {
|
public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuilder> {
|
||||||
|
|
||||||
@ -68,10 +68,6 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
|
|||||||
* The default value for ignore_unmapped.
|
* The default value for ignore_unmapped.
|
||||||
*/
|
*/
|
||||||
public static final boolean DEFAULT_IGNORE_UNMAPPED = false;
|
public static final boolean DEFAULT_IGNORE_UNMAPPED = false;
|
||||||
/*
|
|
||||||
* The default score mode that is used to combine score coming from multiple parent documents.
|
|
||||||
*/
|
|
||||||
public static final ScoreMode DEFAULT_SCORE_MODE = ScoreMode.None;
|
|
||||||
|
|
||||||
private static final ParseField QUERY_FIELD = new ParseField("query", "filter");
|
private static final ParseField QUERY_FIELD = new ParseField("query", "filter");
|
||||||
private static final ParseField TYPE_FIELD = new ParseField("type", "child_type");
|
private static final ParseField TYPE_FIELD = new ParseField("type", "child_type");
|
||||||
@ -82,42 +78,25 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
|
|||||||
private static final ParseField IGNORE_UNMAPPED_FIELD = new ParseField("ignore_unmapped");
|
private static final ParseField IGNORE_UNMAPPED_FIELD = new ParseField("ignore_unmapped");
|
||||||
|
|
||||||
private final QueryBuilder<?> query;
|
private final QueryBuilder<?> query;
|
||||||
|
|
||||||
private final String type;
|
private final String type;
|
||||||
|
private final ScoreMode scoreMode;
|
||||||
private ScoreMode scoreMode = DEFAULT_SCORE_MODE;
|
|
||||||
|
|
||||||
private int minChildren = DEFAULT_MIN_CHILDREN;
|
|
||||||
|
|
||||||
private int maxChildren = DEFAULT_MAX_CHILDREN;
|
|
||||||
|
|
||||||
private InnerHitBuilder innerHitBuilder;
|
private InnerHitBuilder innerHitBuilder;
|
||||||
|
private int minChildren = DEFAULT_MIN_CHILDREN;
|
||||||
|
private int maxChildren = DEFAULT_MAX_CHILDREN;
|
||||||
private boolean ignoreUnmapped = false;
|
private boolean ignoreUnmapped = false;
|
||||||
|
|
||||||
|
public HasChildQueryBuilder(String type, QueryBuilder<?> query, ScoreMode scoreMode) {
|
||||||
public HasChildQueryBuilder(String type, QueryBuilder<?> query, int maxChildren, int minChildren, ScoreMode scoreMode,
|
this(type, query, DEFAULT_MIN_CHILDREN, DEFAULT_MAX_CHILDREN, scoreMode, null);
|
||||||
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) {
|
private HasChildQueryBuilder(String type, QueryBuilder<?> query, int minChildren, int maxChildren, ScoreMode scoreMode,
|
||||||
if (type == null) {
|
InnerHitBuilder innerHitBuilder) {
|
||||||
throw new IllegalArgumentException("[" + NAME + "] requires 'type' field");
|
this.type = requireValue(type, "[" + NAME + "] requires 'type' field");
|
||||||
}
|
this.query = requireValue(query, "[" + NAME + "] requires 'query' field");
|
||||||
if (query == null) {
|
this.scoreMode = requireValue(scoreMode, "[" + NAME + "] requires 'score_mode' field");
|
||||||
throw new IllegalArgumentException("[" + NAME + "] requires 'query' field");
|
this.innerHitBuilder = innerHitBuilder;
|
||||||
}
|
this.minChildren = minChildren;
|
||||||
this.type = type;
|
this.maxChildren = maxChildren;
|
||||||
this.query = query;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -137,8 +116,8 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
|
|||||||
@Override
|
@Override
|
||||||
protected void doWriteTo(StreamOutput out) throws IOException {
|
protected void doWriteTo(StreamOutput out) throws IOException {
|
||||||
out.writeString(type);
|
out.writeString(type);
|
||||||
out.writeInt(minChildren());
|
out.writeInt(minChildren);
|
||||||
out.writeInt(maxChildren());
|
out.writeInt(maxChildren);
|
||||||
out.writeVInt(scoreMode.ordinal());
|
out.writeVInt(scoreMode.ordinal());
|
||||||
out.writeQuery(query);
|
out.writeQuery(query);
|
||||||
out.writeOptionalWriteable(innerHitBuilder);
|
out.writeOptionalWriteable(innerHitBuilder);
|
||||||
@ -146,48 +125,24 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines how the scores from the matching child documents are mapped into the parent document.
|
* Defines the minimum number of children that are required to match for the parent to be considered a match and
|
||||||
|
* the maximum number of children that are required to match for the parent to be considered a match.
|
||||||
*/
|
*/
|
||||||
public HasChildQueryBuilder scoreMode(ScoreMode scoreMode) {
|
public HasChildQueryBuilder minMaxChildren(int minChildren, int maxChildren) {
|
||||||
if (scoreMode == null) {
|
|
||||||
throw new IllegalArgumentException("[" + NAME + "] requires 'score_mode' field");
|
|
||||||
}
|
|
||||||
this.scoreMode = scoreMode;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Defines the minimum number of children that are required to match for the parent to be considered a match.
|
|
||||||
*/
|
|
||||||
public HasChildQueryBuilder minChildren(int minChildren) {
|
|
||||||
if (minChildren < 0) {
|
if (minChildren < 0) {
|
||||||
throw new IllegalArgumentException("[" + NAME + "] requires non-negative 'min_children' field");
|
throw new IllegalArgumentException("[" + NAME + "] requires non-negative 'min_children' field");
|
||||||
|
}
|
||||||
|
if (maxChildren < 0) {
|
||||||
|
throw new IllegalArgumentException("[" + NAME + "] requires non-negative 'max_children' field");
|
||||||
|
}
|
||||||
|
if (maxChildren < minChildren) {
|
||||||
|
throw new IllegalArgumentException("[" + NAME + "] 'max_children' is less than 'min_children'");
|
||||||
}
|
}
|
||||||
this.minChildren = minChildren;
|
this.minChildren = minChildren;
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Defines the maximum number of children that are required to match for the parent to be considered a match.
|
|
||||||
*/
|
|
||||||
public HasChildQueryBuilder maxChildren(int maxChildren) {
|
|
||||||
if (maxChildren < 0) {
|
|
||||||
throw new IllegalArgumentException("[" + NAME + "] requires non-negative 'max_children' field");
|
|
||||||
}
|
|
||||||
this.maxChildren = maxChildren;
|
this.maxChildren = maxChildren;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
|
|
||||||
*/
|
|
||||||
public HasChildQueryBuilder innerHit(InnerHitBuilder innerHitBuilder) {
|
|
||||||
this.innerHitBuilder = Objects.requireNonNull(innerHitBuilder);
|
|
||||||
this.innerHitBuilder.setParentChildType(type);
|
|
||||||
this.innerHitBuilder.setQuery(query);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns inner hit definition in the scope of this query and reusing the defined type and query.
|
* Returns inner hit definition in the scope of this query and reusing the defined type and query.
|
||||||
*/
|
*/
|
||||||
@ -195,6 +150,13 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
|
|||||||
return innerHitBuilder;
|
return innerHitBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HasChildQueryBuilder innerHit(InnerHitBuilder innerHit) {
|
||||||
|
innerHit.setParentChildType(type);
|
||||||
|
innerHit.setQuery(query);
|
||||||
|
this.innerHitBuilder = innerHit;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the children query to execute.
|
* Returns the children query to execute.
|
||||||
*/
|
*/
|
||||||
@ -270,7 +232,7 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
|
|||||||
XContentParser parser = parseContext.parser();
|
XContentParser parser = parseContext.parser();
|
||||||
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
|
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
|
||||||
String childType = null;
|
String childType = null;
|
||||||
ScoreMode scoreMode = HasChildQueryBuilder.DEFAULT_SCORE_MODE;
|
ScoreMode scoreMode = ScoreMode.None;
|
||||||
int minChildren = HasChildQueryBuilder.DEFAULT_MIN_CHILDREN;
|
int minChildren = HasChildQueryBuilder.DEFAULT_MIN_CHILDREN;
|
||||||
int maxChildren = HasChildQueryBuilder.DEFAULT_MAX_CHILDREN;
|
int maxChildren = HasChildQueryBuilder.DEFAULT_MAX_CHILDREN;
|
||||||
boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
|
boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
|
||||||
@ -312,7 +274,7 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder(childType, iqb, maxChildren, minChildren,
|
HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder(childType, iqb, minChildren, maxChildren,
|
||||||
scoreMode, innerHitBuilder);
|
scoreMode, innerHitBuilder);
|
||||||
hasChildQueryBuilder.queryName(queryName);
|
hasChildQueryBuilder.queryName(queryName);
|
||||||
hasChildQueryBuilder.boost(boost);
|
hasChildQueryBuilder.boost(boost);
|
||||||
@ -386,10 +348,6 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
|
|||||||
"[" + NAME + "] Type [" + type + "] points to a non existent parent type [" + parentType + "]");
|
"[" + NAME + "] Type [" + type + "] points to a non existent parent type [" + parentType + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (maxChildren > 0 && maxChildren < minChildren) {
|
|
||||||
throw new QueryShardException(context, "[" + NAME + "] 'max_children' is less than 'min_children'");
|
|
||||||
}
|
|
||||||
|
|
||||||
// wrap the query with type query
|
// wrap the query with type query
|
||||||
innerQuery = Queries.filtered(innerQuery, childDocMapper.typeFilter());
|
innerQuery = Queries.filtered(innerQuery, childDocMapper.typeFilter());
|
||||||
|
|
||||||
@ -502,7 +460,7 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
|
|||||||
&& Objects.equals(scoreMode, that.scoreMode)
|
&& Objects.equals(scoreMode, that.scoreMode)
|
||||||
&& Objects.equals(minChildren, that.minChildren)
|
&& Objects.equals(minChildren, that.minChildren)
|
||||||
&& Objects.equals(maxChildren, that.maxChildren)
|
&& Objects.equals(maxChildren, that.maxChildren)
|
||||||
&& Objects.equals(innerHitBuilder, that.innerHitBuilder)
|
&& Objects.equals(innerHitBuilder, that.innerHitBuilder)
|
||||||
&& Objects.equals(ignoreUnmapped, that.ignoreUnmapped);
|
&& Objects.equals(ignoreUnmapped, that.ignoreUnmapped);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -515,12 +473,7 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
|
|||||||
protected QueryBuilder<?> doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
|
protected QueryBuilder<?> doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
|
||||||
QueryBuilder<?> rewrite = query.rewrite(queryRewriteContext);
|
QueryBuilder<?> rewrite = query.rewrite(queryRewriteContext);
|
||||||
if (rewrite != query) {
|
if (rewrite != query) {
|
||||||
HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder(type, rewrite);
|
return new HasChildQueryBuilder(type, rewrite, minChildren, minChildren, scoreMode, innerHitBuilder);
|
||||||
hasChildQueryBuilder.minChildren(minChildren);
|
|
||||||
hasChildQueryBuilder.maxChildren(maxChildren);
|
|
||||||
hasChildQueryBuilder.scoreMode(scoreMode);
|
|
||||||
hasChildQueryBuilder.innerHit(innerHitBuilder);
|
|
||||||
return hasChildQueryBuilder;
|
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -48,8 +48,6 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
|
|||||||
public static final String NAME = "has_parent";
|
public static final String NAME = "has_parent";
|
||||||
public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME);
|
public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME);
|
||||||
|
|
||||||
public static final boolean DEFAULT_SCORE = false;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default value for ignore_unmapped.
|
* The default value for ignore_unmapped.
|
||||||
*/
|
*/
|
||||||
@ -64,33 +62,19 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
|
|||||||
|
|
||||||
private final QueryBuilder<?> query;
|
private final QueryBuilder<?> query;
|
||||||
private final String type;
|
private final String type;
|
||||||
private boolean score = DEFAULT_SCORE;
|
private final boolean score;
|
||||||
private InnerHitBuilder innerHit;
|
private InnerHitBuilder innerHit;
|
||||||
private boolean ignoreUnmapped = false;
|
private boolean ignoreUnmapped = false;
|
||||||
|
|
||||||
/**
|
public HasParentQueryBuilder(String type, QueryBuilder<?> query, boolean score) {
|
||||||
* @param type The parent type
|
this(type, query, score, null);
|
||||||
* @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, InnerHitBuilder innerHit) {
|
private HasParentQueryBuilder(String type, QueryBuilder<?> query, boolean score, InnerHitBuilder innerHit) {
|
||||||
this(type, query);
|
this.type = requireValue(type, "[" + NAME + "] requires 'type' field");
|
||||||
|
this.query = requireValue(query, "[" + NAME + "] requires 'query' field");
|
||||||
this.score = score;
|
this.score = score;
|
||||||
this.innerHit = innerHit;
|
this.innerHit = innerHit;
|
||||||
if (this.innerHit != null) {
|
|
||||||
this.innerHit.setParentChildType(type);
|
|
||||||
this.innerHit.setQuery(query);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -114,24 +98,6 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
|
|||||||
out.writeBoolean(ignoreUnmapped);
|
out.writeBoolean(ignoreUnmapped);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Defines if the parent score is mapped into the child documents.
|
|
||||||
*/
|
|
||||||
public HasParentQueryBuilder score(boolean score) {
|
|
||||||
this.score = score;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets inner hit definition in the scope of this query and reusing the defined type and query.
|
|
||||||
*/
|
|
||||||
public HasParentQueryBuilder innerHit(InnerHitBuilder innerHit) {
|
|
||||||
this.innerHit = Objects.requireNonNull(innerHit);
|
|
||||||
this.innerHit.setParentChildType(type);
|
|
||||||
this.innerHit.setQuery(query);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the query to execute.
|
* Returns the query to execute.
|
||||||
*/
|
*/
|
||||||
@ -160,6 +126,13 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
|
|||||||
return innerHit;
|
return innerHit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HasParentQueryBuilder innerHit(InnerHitBuilder innerHit) {
|
||||||
|
innerHit.setParentChildType(type);
|
||||||
|
innerHit.setQuery(query);
|
||||||
|
this.innerHit = innerHit;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets whether the query builder should ignore unmapped types (and run a
|
* Sets whether the query builder should ignore unmapped types (and run a
|
||||||
* {@link MatchNoDocsQuery} in place of this query) or throw an exception if
|
* {@link MatchNoDocsQuery} in place of this query) or throw an exception if
|
||||||
@ -264,7 +237,7 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
|
|||||||
XContentParser parser = parseContext.parser();
|
XContentParser parser = parseContext.parser();
|
||||||
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
|
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
|
||||||
String parentType = null;
|
String parentType = null;
|
||||||
boolean score = HasParentQueryBuilder.DEFAULT_SCORE;
|
boolean score = false;
|
||||||
String queryName = null;
|
String queryName = null;
|
||||||
InnerHitBuilder innerHits = null;
|
InnerHitBuilder innerHits = null;
|
||||||
boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
|
boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
|
||||||
@ -323,7 +296,7 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
|
|||||||
return Objects.equals(query, that.query)
|
return Objects.equals(query, that.query)
|
||||||
&& Objects.equals(type, that.type)
|
&& Objects.equals(type, that.type)
|
||||||
&& Objects.equals(score, that.score)
|
&& Objects.equals(score, that.score)
|
||||||
&& Objects.equals(innerHit, that.innerHit)
|
&& Objects.equals(innerHit, that.innerHit)
|
||||||
&& Objects.equals(ignoreUnmapped, that.ignoreUnmapped);
|
&& Objects.equals(ignoreUnmapped, that.ignoreUnmapped);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -336,10 +309,7 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
|
|||||||
protected QueryBuilder<?> doRewrite(QueryRewriteContext queryShardContext) throws IOException {
|
protected QueryBuilder<?> doRewrite(QueryRewriteContext queryShardContext) throws IOException {
|
||||||
QueryBuilder<?> rewrite = query.rewrite(queryShardContext);
|
QueryBuilder<?> rewrite = query.rewrite(queryShardContext);
|
||||||
if (rewrite != query) {
|
if (rewrite != query) {
|
||||||
HasParentQueryBuilder hasParentQueryBuilder = new HasParentQueryBuilder(type, rewrite);
|
return new HasParentQueryBuilder(type, rewrite, score, innerHit);
|
||||||
hasParentQueryBuilder.score(score);
|
|
||||||
hasParentQueryBuilder.innerHit(innerHit);
|
|
||||||
return hasParentQueryBuilder;
|
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -44,12 +44,6 @@ public class NestedQueryBuilder extends AbstractQueryBuilder<NestedQueryBuilder>
|
|||||||
*/
|
*/
|
||||||
public static final String NAME = "nested";
|
public static final String NAME = "nested";
|
||||||
public static final ParseField QUERY_NAME_FIELD = new ParseField(NAME);
|
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.
|
* The default value for ignore_unmapped.
|
||||||
*/
|
*/
|
||||||
@ -61,35 +55,21 @@ public class NestedQueryBuilder extends AbstractQueryBuilder<NestedQueryBuilder>
|
|||||||
private static final ParseField INNER_HITS_FIELD = new ParseField("inner_hits");
|
private static final ParseField INNER_HITS_FIELD = new ParseField("inner_hits");
|
||||||
private static final ParseField IGNORE_UNMAPPED_FIELD = new ParseField("ignore_unmapped");
|
private static final ParseField IGNORE_UNMAPPED_FIELD = new ParseField("ignore_unmapped");
|
||||||
|
|
||||||
private final QueryBuilder<?> query;
|
|
||||||
|
|
||||||
private final String path;
|
private final String path;
|
||||||
|
private final ScoreMode scoreMode;
|
||||||
private ScoreMode scoreMode = DEFAULT_SCORE_MODE;
|
private final QueryBuilder<?> query;
|
||||||
|
|
||||||
private InnerHitBuilder innerHitBuilder;
|
private InnerHitBuilder innerHitBuilder;
|
||||||
|
|
||||||
private boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
|
private boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
|
||||||
|
|
||||||
public NestedQueryBuilder(String path, QueryBuilder<?> query) {
|
public NestedQueryBuilder(String path, QueryBuilder query, ScoreMode scoreMode) {
|
||||||
if (path == null) {
|
this(path, query, scoreMode, 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, InnerHitBuilder innerHitBuilder) {
|
private NestedQueryBuilder(String path, QueryBuilder query, ScoreMode scoreMode, InnerHitBuilder innerHitBuilder) {
|
||||||
this(path, query);
|
this.path = requireValue(path, "[" + NAME + "] requires 'path' field");
|
||||||
scoreMode(scoreMode);
|
this.query = requireValue(query, "[" + NAME + "] requires 'query' field");
|
||||||
|
this.scoreMode = requireValue(scoreMode, "[" + NAME + "] requires 'score_mode' field");
|
||||||
this.innerHitBuilder = innerHitBuilder;
|
this.innerHitBuilder = innerHitBuilder;
|
||||||
if (this.innerHitBuilder != null) {
|
|
||||||
this.innerHitBuilder.setNestedPath(path);
|
|
||||||
this.innerHitBuilder.setQuery(query);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -114,24 +94,32 @@ public class NestedQueryBuilder extends AbstractQueryBuilder<NestedQueryBuilder>
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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) {
|
public QueryBuilder query() {
|
||||||
if (scoreMode == null) {
|
return query;
|
||||||
throw new IllegalArgumentException("[" + NAME + "] requires 'score_mode' field");
|
}
|
||||||
}
|
|
||||||
this.scoreMode = scoreMode;
|
/**
|
||||||
|
* 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;
|
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) {
|
public ScoreMode scoreMode() {
|
||||||
this.innerHitBuilder = Objects.requireNonNull(innerHit);
|
return scoreMode;
|
||||||
this.innerHitBuilder.setNestedPath(path);
|
|
||||||
this.innerHitBuilder.setQuery(query);
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -153,27 +141,6 @@ public class NestedQueryBuilder extends AbstractQueryBuilder<NestedQueryBuilder>
|
|||||||
return ignoreUnmapped;
|
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
|
@Override
|
||||||
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
|
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
|
||||||
builder.startObject(NAME);
|
builder.startObject(NAME);
|
||||||
@ -194,7 +161,7 @@ public class NestedQueryBuilder extends AbstractQueryBuilder<NestedQueryBuilder>
|
|||||||
public static NestedQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
|
public static NestedQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
|
||||||
XContentParser parser = parseContext.parser();
|
XContentParser parser = parseContext.parser();
|
||||||
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
|
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
|
||||||
ScoreMode scoreMode = NestedQueryBuilder.DEFAULT_SCORE_MODE;
|
ScoreMode scoreMode = ScoreMode.Avg;
|
||||||
String queryName = null;
|
String queryName = null;
|
||||||
QueryBuilder query = null;
|
QueryBuilder query = null;
|
||||||
String path = null;
|
String path = null;
|
||||||
@ -243,7 +210,7 @@ public class NestedQueryBuilder extends AbstractQueryBuilder<NestedQueryBuilder>
|
|||||||
return Objects.equals(query, that.query)
|
return Objects.equals(query, that.query)
|
||||||
&& Objects.equals(path, that.path)
|
&& Objects.equals(path, that.path)
|
||||||
&& Objects.equals(scoreMode, that.scoreMode)
|
&& Objects.equals(scoreMode, that.scoreMode)
|
||||||
&& Objects.equals(innerHitBuilder, that.innerHitBuilder)
|
&& Objects.equals(innerHitBuilder, that.innerHitBuilder)
|
||||||
&& Objects.equals(ignoreUnmapped, that.ignoreUnmapped);
|
&& Objects.equals(ignoreUnmapped, that.ignoreUnmapped);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -294,7 +261,7 @@ public class NestedQueryBuilder extends AbstractQueryBuilder<NestedQueryBuilder>
|
|||||||
protected QueryBuilder<?> doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
|
protected QueryBuilder<?> doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
|
||||||
QueryBuilder rewrite = query.rewrite(queryRewriteContext);
|
QueryBuilder rewrite = query.rewrite(queryRewriteContext);
|
||||||
if (rewrite != query) {
|
if (rewrite != query) {
|
||||||
return new NestedQueryBuilder(path, rewrite).scoreMode(scoreMode).innerHit(innerHitBuilder);
|
return new NestedQueryBuilder(path, rewrite, scoreMode, innerHitBuilder);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
package org.elasticsearch.index.query;
|
package org.elasticsearch.index.query;
|
||||||
|
|
||||||
|
import org.apache.lucene.search.join.ScoreMode;
|
||||||
import org.elasticsearch.common.bytes.BytesReference;
|
import org.elasticsearch.common.bytes.BytesReference;
|
||||||
import org.elasticsearch.common.geo.GeoPoint;
|
import org.elasticsearch.common.geo.GeoPoint;
|
||||||
import org.elasticsearch.common.geo.ShapeRelation;
|
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.MoreLikeThisQueryBuilder.Item;
|
||||||
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
|
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
|
||||||
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder;
|
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder;
|
||||||
|
import org.elasticsearch.index.query.support.InnerHitBuilder;
|
||||||
import org.elasticsearch.index.search.MatchQuery;
|
import org.elasticsearch.index.search.MatchQuery;
|
||||||
import org.elasticsearch.indices.TermsLookup;
|
import org.elasticsearch.indices.TermsLookup;
|
||||||
import org.elasticsearch.script.Script;
|
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.
|
* results of this query are the parent docs that those child docs matched.
|
||||||
*
|
*
|
||||||
* @param type The child type.
|
* @param type The child type.
|
||||||
* @param query The query.
|
* @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) {
|
public static HasChildQueryBuilder hasChildQuery(String type, QueryBuilder query, ScoreMode scoreMode) {
|
||||||
return new HasChildQueryBuilder(type, query);
|
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.
|
* results of this query are the children docs that those parent docs matched.
|
||||||
*
|
*
|
||||||
* @param type The parent type.
|
* @param type The parent type.
|
||||||
* @param query The query.
|
* @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) {
|
public static HasParentQueryBuilder hasParentQuery(String type, QueryBuilder query, boolean score) {
|
||||||
return new HasParentQueryBuilder(type, query);
|
return new HasParentQueryBuilder(type, query, score);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -512,8 +516,8 @@ public abstract class QueryBuilders {
|
|||||||
return new ParentIdQueryBuilder(type, id);
|
return new ParentIdQueryBuilder(type, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static NestedQueryBuilder nestedQuery(String path, QueryBuilder query) {
|
public static NestedQueryBuilder nestedQuery(String path, QueryBuilder query, ScoreMode scoreMode) {
|
||||||
return new NestedQueryBuilder(path, query);
|
return new NestedQueryBuilder(path, query, scoreMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,9 +19,9 @@
|
|||||||
|
|
||||||
package org.elasticsearch.aliases;
|
package org.elasticsearch.aliases;
|
||||||
|
|
||||||
|
import org.apache.lucene.search.join.ScoreMode;
|
||||||
import org.elasticsearch.action.ActionRequestValidationException;
|
import org.elasticsearch.action.ActionRequestValidationException;
|
||||||
import org.elasticsearch.action.admin.indices.alias.Alias;
|
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.IndicesAliasesRequest.AliasActions;
|
||||||
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequestBuilder;
|
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequestBuilder;
|
||||||
import org.elasticsearch.action.admin.indices.alias.exists.AliasesExistResponse;
|
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();
|
client().prepareIndex("my-index", "child", "2").setSource("{}").setParent("1").get();
|
||||||
refresh();
|
refresh();
|
||||||
|
|
||||||
assertAcked(admin().indices().prepareAliases().addAlias("my-index", "filter1", hasChildQuery("child", 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())));
|
assertAcked(admin().indices().prepareAliases().addAlias("my-index", "filter2", hasParentQuery("parent", matchAllQuery(), false)));
|
||||||
|
|
||||||
SearchResponse response = client().prepareSearch("filter1").get();
|
SearchResponse response = client().prepareSearch("filter1").get();
|
||||||
assertHitCount(response, 1);
|
assertHitCount(response, 1);
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
package org.elasticsearch.cluster;
|
package org.elasticsearch.cluster;
|
||||||
|
|
||||||
|
import org.apache.lucene.search.join.ScoreMode;
|
||||||
import org.elasticsearch.cluster.metadata.MappingMetaData;
|
import org.elasticsearch.cluster.metadata.MappingMetaData;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.discovery.MasterNotDiscoveredException;
|
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));
|
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\" }}}}}}"));
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -120,13 +120,18 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase<HasChildQue
|
|||||||
protected HasChildQueryBuilder doCreateTestQueryBuilder() {
|
protected HasChildQueryBuilder doCreateTestQueryBuilder() {
|
||||||
int min = randomIntBetween(0, Integer.MAX_VALUE / 2);
|
int min = randomIntBetween(0, Integer.MAX_VALUE / 2);
|
||||||
int max = randomIntBetween(min, Integer.MAX_VALUE);
|
int max = randomIntBetween(min, Integer.MAX_VALUE);
|
||||||
return new HasChildQueryBuilder(CHILD_TYPE,
|
HasChildQueryBuilder hqb = new HasChildQueryBuilder(CHILD_TYPE,
|
||||||
RandomQueryBuilder.createQuery(random()), max, min,
|
RandomQueryBuilder.createQuery(random()),
|
||||||
RandomPicks.randomFrom(random(), ScoreMode.values()),
|
RandomPicks.randomFrom(random(), ScoreMode.values()));
|
||||||
randomBoolean() ? null : new InnerHitBuilder()
|
hqb.minMaxChildren(min, max);
|
||||||
.setName(randomAsciiOfLengthBetween(1, 10))
|
if (randomBoolean()) {
|
||||||
.setSize(randomIntBetween(0, 100))
|
hqb.innerHit(new InnerHitBuilder()
|
||||||
.addSort(new FieldSortBuilder(STRING_FIELD_NAME_2).order(SortOrder.ASC))).ignoreUnmapped(randomBoolean());
|
.setName(randomAsciiOfLengthBetween(1, 10))
|
||||||
|
.setSize(randomIntBetween(0, 100))
|
||||||
|
.addSort(new FieldSortBuilder(STRING_FIELD_NAME_2).order(SortOrder.ASC)));
|
||||||
|
}
|
||||||
|
hqb.ignoreUnmapped(randomBoolean());
|
||||||
|
return hqb;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -160,44 +165,26 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase<HasChildQue
|
|||||||
|
|
||||||
public void testIllegalValues() {
|
public void testIllegalValues() {
|
||||||
QueryBuilder<?> query = RandomQueryBuilder.createQuery(random());
|
QueryBuilder<?> query = RandomQueryBuilder.createQuery(random());
|
||||||
try {
|
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
|
||||||
new HasChildQueryBuilder(null, query);
|
() -> QueryBuilders.hasChildQuery(null, query, ScoreMode.None));
|
||||||
fail("must not be null");
|
assertEquals("[has_child] requires 'type' field", e.getMessage());
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
|
|
||||||
}
|
e = expectThrows(IllegalArgumentException.class, () -> QueryBuilders.hasChildQuery("foo", null, ScoreMode.None));
|
||||||
|
assertEquals("[has_child] requires 'query' field", e.getMessage());
|
||||||
|
|
||||||
try {
|
e = expectThrows(IllegalArgumentException.class, () -> QueryBuilders.hasChildQuery("foo", query, null));
|
||||||
new HasChildQueryBuilder("foo", null);
|
assertEquals("[has_child] requires 'score_mode' field", e.getMessage());
|
||||||
fail("must not be null");
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
|
|
||||||
}
|
int positiveValue = randomIntBetween(0, Integer.MAX_VALUE);
|
||||||
HasChildQueryBuilder foo = new HasChildQueryBuilder("foo", query);// all good
|
HasChildQueryBuilder foo = QueryBuilders.hasChildQuery("foo", query, ScoreMode.None); // all good
|
||||||
try {
|
e = expectThrows(IllegalArgumentException.class, () -> foo.minMaxChildren(randomIntBetween(Integer.MIN_VALUE, -1), positiveValue));
|
||||||
foo.scoreMode(null);
|
assertEquals("[has_child] requires non-negative 'min_children' field", e.getMessage());
|
||||||
fail("must not be null");
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
|
|
||||||
}
|
e = expectThrows(IllegalArgumentException.class, () -> foo.minMaxChildren(positiveValue, randomIntBetween(Integer.MIN_VALUE, -1)));
|
||||||
final int positiveValue = randomIntBetween(0, Integer.MAX_VALUE);
|
assertEquals("[has_child] requires non-negative 'max_children' field", e.getMessage());
|
||||||
try {
|
|
||||||
foo.minChildren(randomIntBetween(Integer.MIN_VALUE, -1));
|
|
||||||
fail("must not be negative");
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
|
|
||||||
}
|
e = expectThrows(IllegalArgumentException.class, () -> foo.minMaxChildren(positiveValue, positiveValue - 10));
|
||||||
foo.minChildren(positiveValue);
|
assertEquals("[has_child] 'max_children' is less than 'min_children'", e.getMessage());
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testFromJson() throws IOException {
|
public void testFromJson() throws IOException {
|
||||||
@ -269,7 +256,7 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase<HasChildQue
|
|||||||
String[] searchTypes = new String[]{PARENT_TYPE};
|
String[] searchTypes = new String[]{PARENT_TYPE};
|
||||||
QueryShardContext shardContext = createShardContext();
|
QueryShardContext shardContext = createShardContext();
|
||||||
shardContext.setTypes(searchTypes);
|
shardContext.setTypes(searchTypes);
|
||||||
HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder(CHILD_TYPE, new IdsQueryBuilder().addIds("id"));
|
HasChildQueryBuilder hasChildQueryBuilder = QueryBuilders.hasChildQuery(CHILD_TYPE, new IdsQueryBuilder().addIds("id"), ScoreMode.None);
|
||||||
Query query = hasChildQueryBuilder.toQuery(shardContext);
|
Query query = hasChildQueryBuilder.toQuery(shardContext);
|
||||||
//verify that the context types are still the same as the ones we previously set
|
//verify that the context types are still the same as the ones we previously set
|
||||||
assertThat(shardContext.getTypes(), equalTo(searchTypes));
|
assertThat(shardContext.getTypes(), equalTo(searchTypes));
|
||||||
@ -335,7 +322,7 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase<HasChildQue
|
|||||||
|
|
||||||
public void testNonDefaultSimilarity() throws Exception {
|
public void testNonDefaultSimilarity() throws Exception {
|
||||||
QueryShardContext shardContext = createShardContext();
|
QueryShardContext shardContext = createShardContext();
|
||||||
HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder(CHILD_TYPE, new TermQueryBuilder("custom_string", "value"));
|
HasChildQueryBuilder hasChildQueryBuilder = QueryBuilders.hasChildQuery(CHILD_TYPE, new TermQueryBuilder("custom_string", "value"), ScoreMode.None);
|
||||||
HasChildQueryBuilder.LateParsingQuery query = (HasChildQueryBuilder.LateParsingQuery) hasChildQueryBuilder.toQuery(shardContext);
|
HasChildQueryBuilder.LateParsingQuery query = (HasChildQueryBuilder.LateParsingQuery) hasChildQueryBuilder.toQuery(shardContext);
|
||||||
Similarity expected = SimilarityService.BUILT_IN.get(similarity).apply(similarity, Settings.EMPTY).get();
|
Similarity expected = SimilarityService.BUILT_IN.get(similarity).apply(similarity, Settings.EMPTY).get();
|
||||||
assertThat(((PerFieldSimilarityWrapper) query.getSimilarity()).get("custom_string"), instanceOf(expected.getClass()));
|
assertThat(((PerFieldSimilarityWrapper) query.getSimilarity()).get("custom_string"), instanceOf(expected.getClass()));
|
||||||
@ -391,13 +378,13 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase<HasChildQue
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testIgnoreUnmapped() throws IOException {
|
public void testIgnoreUnmapped() throws IOException {
|
||||||
final HasChildQueryBuilder queryBuilder = new HasChildQueryBuilder("unmapped", new MatchAllQueryBuilder());
|
final HasChildQueryBuilder queryBuilder = new HasChildQueryBuilder("unmapped", new MatchAllQueryBuilder(), ScoreMode.None);
|
||||||
queryBuilder.ignoreUnmapped(true);
|
queryBuilder.ignoreUnmapped(true);
|
||||||
Query query = queryBuilder.toQuery(queryShardContext());
|
Query query = queryBuilder.toQuery(queryShardContext());
|
||||||
assertThat(query, notNullValue());
|
assertThat(query, notNullValue());
|
||||||
assertThat(query, instanceOf(MatchNoDocsQuery.class));
|
assertThat(query, instanceOf(MatchNoDocsQuery.class));
|
||||||
|
|
||||||
final HasChildQueryBuilder failingQueryBuilder = new HasChildQueryBuilder("unmapped", new MatchAllQueryBuilder());
|
final HasChildQueryBuilder failingQueryBuilder = new HasChildQueryBuilder("unmapped", new MatchAllQueryBuilder(), ScoreMode.None);
|
||||||
failingQueryBuilder.ignoreUnmapped(false);
|
failingQueryBuilder.ignoreUnmapped(false);
|
||||||
QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(queryShardContext()));
|
QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(queryShardContext()));
|
||||||
assertThat(e.getMessage(), containsString("[" + HasChildQueryBuilder.NAME + "] no mapping found for type [unmapped]"));
|
assertThat(e.getMessage(), containsString("[" + HasChildQueryBuilder.NAME + "] no mapping found for type [unmapped]"));
|
||||||
|
@ -107,12 +107,16 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase<HasParentQ
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected HasParentQueryBuilder doCreateTestQueryBuilder() {
|
protected HasParentQueryBuilder doCreateTestQueryBuilder() {
|
||||||
return new HasParentQueryBuilder(PARENT_TYPE,
|
HasParentQueryBuilder hqb = new HasParentQueryBuilder(PARENT_TYPE,
|
||||||
RandomQueryBuilder.createQuery(random()),randomBoolean(),
|
RandomQueryBuilder.createQuery(random()),randomBoolean());
|
||||||
randomBoolean() ? null : new InnerHitBuilder()
|
if (randomBoolean()) {
|
||||||
.setName(randomAsciiOfLengthBetween(1, 10))
|
hqb.innerHit(new InnerHitBuilder()
|
||||||
.setSize(randomIntBetween(0, 100))
|
.setName(randomAsciiOfLengthBetween(1, 10))
|
||||||
.addSort(new FieldSortBuilder(STRING_FIELD_NAME_2).order(SortOrder.ASC))).ignoreUnmapped(randomBoolean());
|
.setSize(randomIntBetween(0, 100))
|
||||||
|
.addSort(new FieldSortBuilder(STRING_FIELD_NAME_2).order(SortOrder.ASC)));
|
||||||
|
}
|
||||||
|
hqb.ignoreUnmapped(randomBoolean());
|
||||||
|
return hqb;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -144,25 +148,18 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase<HasParentQ
|
|||||||
|
|
||||||
public void testIllegalValues() throws IOException {
|
public void testIllegalValues() throws IOException {
|
||||||
QueryBuilder query = RandomQueryBuilder.createQuery(random());
|
QueryBuilder query = RandomQueryBuilder.createQuery(random());
|
||||||
try {
|
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
|
||||||
new HasParentQueryBuilder(null, query);
|
() -> QueryBuilders.hasParentQuery(null, query, false));
|
||||||
fail("must not be null");
|
assertThat(e.getMessage(), equalTo("[has_parent] requires 'type' field"));
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
e = expectThrows(IllegalArgumentException.class,
|
||||||
new HasParentQueryBuilder("foo", null);
|
() -> QueryBuilders.hasParentQuery("foo", null, false));
|
||||||
fail("must not be null");
|
assertThat(e.getMessage(), equalTo("[has_parent] requires 'query' field"));
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
}
|
|
||||||
|
|
||||||
QueryShardContext context = createShardContext();
|
QueryShardContext context = createShardContext();
|
||||||
HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder("just_a_type", new MatchAllQueryBuilder());
|
HasParentQueryBuilder qb = QueryBuilders.hasParentQuery("just_a_type", new MatchAllQueryBuilder(), false);
|
||||||
try {
|
QueryShardException qse = expectThrows(QueryShardException.class, () -> qb.doToQuery(context));
|
||||||
queryBuilder.doToQuery(context);
|
assertThat(qse.getMessage(), equalTo("[has_parent] no child types found for type [just_a_type]"));
|
||||||
} catch (QueryShardException e) {
|
|
||||||
assertThat(e.getMessage(), equalTo("[has_parent] no child types found for type [just_a_type]"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDeprecatedXContent() throws IOException {
|
public void testDeprecatedXContent() throws IOException {
|
||||||
@ -210,7 +207,8 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase<HasParentQ
|
|||||||
String[] searchTypes = new String[]{CHILD_TYPE};
|
String[] searchTypes = new String[]{CHILD_TYPE};
|
||||||
QueryShardContext shardContext = createShardContext();
|
QueryShardContext shardContext = createShardContext();
|
||||||
shardContext.setTypes(searchTypes);
|
shardContext.setTypes(searchTypes);
|
||||||
HasParentQueryBuilder hasParentQueryBuilder = new HasParentQueryBuilder(PARENT_TYPE, new IdsQueryBuilder().addIds("id"));
|
HasParentQueryBuilder hasParentQueryBuilder = new HasParentQueryBuilder(PARENT_TYPE, new IdsQueryBuilder().addIds("id"),
|
||||||
|
false);
|
||||||
Query query = hasParentQueryBuilder.toQuery(shardContext);
|
Query query = hasParentQueryBuilder.toQuery(shardContext);
|
||||||
//verify that the context types are still the same as the ones we previously set
|
//verify that the context types are still the same as the ones we previously set
|
||||||
assertThat(shardContext.getTypes(), equalTo(searchTypes));
|
assertThat(shardContext.getTypes(), equalTo(searchTypes));
|
||||||
@ -271,13 +269,13 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase<HasParentQ
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testIgnoreUnmapped() throws IOException {
|
public void testIgnoreUnmapped() throws IOException {
|
||||||
final HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder("unmapped", new MatchAllQueryBuilder());
|
final HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder("unmapped", new MatchAllQueryBuilder(), false);
|
||||||
queryBuilder.ignoreUnmapped(true);
|
queryBuilder.ignoreUnmapped(true);
|
||||||
Query query = queryBuilder.toQuery(queryShardContext());
|
Query query = queryBuilder.toQuery(queryShardContext());
|
||||||
assertThat(query, notNullValue());
|
assertThat(query, notNullValue());
|
||||||
assertThat(query, instanceOf(MatchNoDocsQuery.class));
|
assertThat(query, instanceOf(MatchNoDocsQuery.class));
|
||||||
|
|
||||||
final HasParentQueryBuilder failingQueryBuilder = new HasParentQueryBuilder("unmapped", new MatchAllQueryBuilder());
|
final HasParentQueryBuilder failingQueryBuilder = new HasParentQueryBuilder("unmapped", new MatchAllQueryBuilder(), false);
|
||||||
failingQueryBuilder.ignoreUnmapped(false);
|
failingQueryBuilder.ignoreUnmapped(false);
|
||||||
QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(queryShardContext()));
|
QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(queryShardContext()));
|
||||||
assertThat(e.getMessage(),
|
assertThat(e.getMessage(),
|
||||||
|
@ -86,12 +86,16 @@ public class NestedQueryBuilderTests extends AbstractQueryTestCase<NestedQueryBu
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected NestedQueryBuilder doCreateTestQueryBuilder() {
|
protected NestedQueryBuilder doCreateTestQueryBuilder() {
|
||||||
return new NestedQueryBuilder("nested1", RandomQueryBuilder.createQuery(random()),
|
NestedQueryBuilder nqb = new NestedQueryBuilder("nested1", RandomQueryBuilder.createQuery(random()),
|
||||||
RandomPicks.randomFrom(random(), ScoreMode.values()),
|
RandomPicks.randomFrom(random(), ScoreMode.values()));
|
||||||
SearchContext.current() == null ? null : new InnerHitBuilder()
|
if (SearchContext.current() != null) {
|
||||||
.setName(randomAsciiOfLengthBetween(1, 10))
|
nqb.innerHit(new InnerHitBuilder()
|
||||||
.setSize(randomIntBetween(0, 100))
|
.setName(randomAsciiOfLengthBetween(1, 10))
|
||||||
.addSort(new FieldSortBuilder(STRING_FIELD_NAME).order(SortOrder.ASC))).ignoreUnmapped(randomBoolean());
|
.setSize(randomIntBetween(0, 100))
|
||||||
|
.addSort(new FieldSortBuilder(STRING_FIELD_NAME).order(SortOrder.ASC)));
|
||||||
|
}
|
||||||
|
nqb.ignoreUnmapped(randomBoolean());
|
||||||
|
return nqb;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -121,27 +125,16 @@ public class NestedQueryBuilderTests extends AbstractQueryTestCase<NestedQueryBu
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testValidate() {
|
public void testValidate() {
|
||||||
try {
|
QueryBuilder<?> innerQuery = RandomQueryBuilder.createQuery(random());
|
||||||
new NestedQueryBuilder(null, new MatchAllQueryBuilder());
|
IllegalArgumentException e =
|
||||||
fail("cannot be null");
|
expectThrows(IllegalArgumentException.class, () -> QueryBuilders.nestedQuery(null, innerQuery, ScoreMode.Avg));
|
||||||
} catch (IllegalArgumentException e) {
|
assertThat(e.getMessage(), equalTo("[nested] requires 'path' field"));
|
||||||
// expected
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
e = expectThrows(IllegalArgumentException.class, () -> QueryBuilders.nestedQuery("foo", null, ScoreMode.Avg));
|
||||||
new NestedQueryBuilder("path", null);
|
assertThat(e.getMessage(), equalTo("[nested] requires 'query' field"));
|
||||||
fail("cannot be null");
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
// expected
|
|
||||||
}
|
|
||||||
|
|
||||||
NestedQueryBuilder nestedQueryBuilder = new NestedQueryBuilder("path", new MatchAllQueryBuilder());
|
e = expectThrows(IllegalArgumentException.class, () -> QueryBuilders.nestedQuery("foo", innerQuery, null));
|
||||||
try {
|
assertThat(e.getMessage(), equalTo("[nested] requires 'score_mode' field"));
|
||||||
nestedQueryBuilder.scoreMode(null);
|
|
||||||
fail("cannot be null");
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
// expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testFromJson() throws IOException {
|
public void testFromJson() throws IOException {
|
||||||
@ -193,13 +186,13 @@ public class NestedQueryBuilderTests extends AbstractQueryTestCase<NestedQueryBu
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testIgnoreUnmapped() throws IOException {
|
public void testIgnoreUnmapped() throws IOException {
|
||||||
final NestedQueryBuilder queryBuilder = new NestedQueryBuilder("unmapped", new MatchAllQueryBuilder());
|
final NestedQueryBuilder queryBuilder = new NestedQueryBuilder("unmapped", new MatchAllQueryBuilder(), ScoreMode.None);
|
||||||
queryBuilder.ignoreUnmapped(true);
|
queryBuilder.ignoreUnmapped(true);
|
||||||
Query query = queryBuilder.toQuery(queryShardContext());
|
Query query = queryBuilder.toQuery(queryShardContext());
|
||||||
assertThat(query, notNullValue());
|
assertThat(query, notNullValue());
|
||||||
assertThat(query, instanceOf(MatchNoDocsQuery.class));
|
assertThat(query, instanceOf(MatchNoDocsQuery.class));
|
||||||
|
|
||||||
final NestedQueryBuilder failingQueryBuilder = new NestedQueryBuilder("unmapped", new MatchAllQueryBuilder());
|
final NestedQueryBuilder failingQueryBuilder = new NestedQueryBuilder("unmapped", new MatchAllQueryBuilder(), ScoreMode.None);
|
||||||
failingQueryBuilder.ignoreUnmapped(false);
|
failingQueryBuilder.ignoreUnmapped(false);
|
||||||
IllegalStateException e = expectThrows(IllegalStateException.class, () -> failingQueryBuilder.toQuery(queryShardContext()));
|
IllegalStateException e = expectThrows(IllegalStateException.class, () -> failingQueryBuilder.toQuery(queryShardContext()));
|
||||||
assertThat(e.getMessage(), containsString("[" + NestedQueryBuilder.NAME + "] failed to find nested object under path [unmapped]"));
|
assertThat(e.getMessage(), containsString("[" + NestedQueryBuilder.NAME + "] failed to find nested object under path [unmapped]"));
|
||||||
|
@ -205,15 +205,15 @@ public class QueryDSLDocumentationTests extends ESTestCase {
|
|||||||
public void testHasChild() {
|
public void testHasChild() {
|
||||||
hasChildQuery(
|
hasChildQuery(
|
||||||
"blog_tag",
|
"blog_tag",
|
||||||
termQuery("tag","something")
|
termQuery("tag","something"),
|
||||||
);
|
ScoreMode.None);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testHasParent() {
|
public void testHasParent() {
|
||||||
hasParentQuery(
|
hasParentQuery(
|
||||||
"blog",
|
"blog",
|
||||||
termQuery("tag","something")
|
termQuery("tag","something"),
|
||||||
);
|
false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testIds() {
|
public void testIds() {
|
||||||
@ -262,9 +262,8 @@ public class QueryDSLDocumentationTests extends ESTestCase {
|
|||||||
"obj1",
|
"obj1",
|
||||||
boolQuery()
|
boolQuery()
|
||||||
.must(matchQuery("obj1.name", "blue"))
|
.must(matchQuery("obj1.name", "blue"))
|
||||||
.must(rangeQuery("obj1.count").gt(5))
|
.must(rangeQuery("obj1.count").gt(5)),
|
||||||
)
|
ScoreMode.Avg);
|
||||||
.scoreMode(ScoreMode.Avg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testPrefix() {
|
public void testPrefix() {
|
||||||
|
@ -396,7 +396,7 @@ public class MultiPercolatorIT extends ESIntegTestCase {
|
|||||||
ensureGreen("nestedindex");
|
ensureGreen("nestedindex");
|
||||||
|
|
||||||
client().prepareIndex("nestedindex", PercolatorFieldMapper.TYPE_NAME, "Q").setSource(jsonBuilder().startObject()
|
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();
|
refresh();
|
||||||
|
|
||||||
|
@ -1564,7 +1564,7 @@ public class PercolatorIT extends ESIntegTestCase {
|
|||||||
ensureGreen("nestedindex");
|
ensureGreen("nestedindex");
|
||||||
|
|
||||||
client().prepareIndex("nestedindex", PercolatorFieldMapper.TYPE_NAME, "Q").setSource(jsonBuilder().startObject()
|
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();
|
refresh();
|
||||||
|
|
||||||
@ -1788,7 +1788,7 @@ public class PercolatorIT extends ESIntegTestCase {
|
|||||||
assertAcked(prepareCreate("index").addMapping("mapping", mapping));
|
assertAcked(prepareCreate("index").addMapping("mapping", mapping));
|
||||||
try {
|
try {
|
||||||
client().prepareIndex("index", PercolatorFieldMapper.TYPE_NAME, "1")
|
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();
|
.execute().actionGet();
|
||||||
fail("Expected a parse error, because inner_hits isn't supported in the percolate api");
|
fail("Expected a parse error, because inner_hits isn't supported in the percolate api");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -1803,7 +1803,7 @@ public class PercolatorIT extends ESIntegTestCase {
|
|||||||
|
|
||||||
assertAcked(prepareCreate("index").addMapping("child", "_parent", "type=parent").addMapping("parent"));
|
assertAcked(prepareCreate("index").addMapping("child", "_parent", "type=parent").addMapping("parent"));
|
||||||
client().prepareIndex("index", PercolatorFieldMapper.TYPE_NAME, "1")
|
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();
|
.execute().actionGet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.elasticsearch.search.aggregations.bucket;
|
package org.elasticsearch.search.aggregations.bucket;
|
||||||
|
|
||||||
|
import org.apache.lucene.search.join.ScoreMode;
|
||||||
import org.elasticsearch.action.index.IndexRequestBuilder;
|
import org.elasticsearch.action.index.IndexRequestBuilder;
|
||||||
import org.elasticsearch.action.search.SearchResponse;
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
import org.elasticsearch.action.update.UpdateResponse;
|
import org.elasticsearch.action.update.UpdateResponse;
|
||||||
@ -319,7 +320,7 @@ children("non-existing", "xyz")
|
|||||||
indexRandom(true, requests);
|
indexRandom(true, requests);
|
||||||
|
|
||||||
SearchResponse response = client().prepareSearch(indexName).setTypes(masterType)
|
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)
|
.addAggregation(children("my-refinements", childType)
|
||||||
.subAggregation(terms("my-colors").field("color"))
|
.subAggregation(terms("my-colors").field("color"))
|
||||||
.subAggregation(terms("my-sizes").field("size"))
|
.subAggregation(terms("my-sizes").field("size"))
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
package org.elasticsearch.search.aggregations.metrics;
|
package org.elasticsearch.search.aggregations.metrics;
|
||||||
|
|
||||||
import org.apache.lucene.search.Explanation;
|
import org.apache.lucene.search.Explanation;
|
||||||
|
import org.apache.lucene.search.join.ScoreMode;
|
||||||
import org.apache.lucene.util.ArrayUtil;
|
import org.apache.lucene.util.ArrayUtil;
|
||||||
import org.elasticsearch.action.index.IndexRequestBuilder;
|
import org.elasticsearch.action.index.IndexRequestBuilder;
|
||||||
import org.elasticsearch.action.search.SearchPhaseExecutionException;
|
import org.elasticsearch.action.search.SearchPhaseExecutionException;
|
||||||
@ -816,7 +817,7 @@ public class TopHitsIT extends ESIntegTestCase {
|
|||||||
|
|
||||||
SearchResponse searchResponse = client()
|
SearchResponse searchResponse = client()
|
||||||
.prepareSearch("articles")
|
.prepareSearch("articles")
|
||||||
.setQuery(nestedQuery("comments", matchQuery("comments.message", "comment").queryName("test")))
|
.setQuery(nestedQuery("comments", matchQuery("comments.message", "comment").queryName("test"), ScoreMode.Avg))
|
||||||
.addAggregation(
|
.addAggregation(
|
||||||
nested("to-comments", "comments").subAggregation(
|
nested("to-comments", "comments").subAggregation(
|
||||||
topHits("top-comments").size(1).highlighter(new HighlightBuilder().field(hlField)).explain(true)
|
topHits("top-comments").size(1).highlighter(new HighlightBuilder().field(hlField)).explain(true)
|
||||||
|
@ -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.boolQuery;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.constantScoreQuery;
|
import static org.elasticsearch.index.query.QueryBuilders.constantScoreQuery;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.hasParentQuery;
|
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.idsQuery;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
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.is;
|
||||||
import static org.hamcrest.Matchers.notNullValue;
|
import static org.hamcrest.Matchers.notNullValue;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@ClusterScope(scope = Scope.SUITE)
|
@ClusterScope(scope = Scope.SUITE)
|
||||||
public class ChildQuerySearchIT extends ESIntegTestCase {
|
public class ChildQuerySearchIT extends ESIntegTestCase {
|
||||||
|
|
||||||
@ -134,32 +132,33 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
.filter(hasChildQuery(
|
.filter(hasChildQuery(
|
||||||
"child",
|
"child",
|
||||||
boolQuery().must(termQuery("c_field", "c_value1"))
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1"));
|
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1"));
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test")
|
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();
|
.actionGet();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("c1"));
|
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("c1"));
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test")
|
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();
|
.actionGet();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("gc1"));
|
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();
|
.actionGet();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("c1"));
|
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();
|
.actionGet();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
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", "foo", "1").setSource("foo", 1).get();
|
||||||
client().prepareIndex("test", "test").setSource("foo", 1).setParent("1").get();
|
client().prepareIndex("test", "test").setSource("foo", 1).setParent("1").get();
|
||||||
refresh();
|
refresh();
|
||||||
SearchResponse searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("test", matchQuery("foo", 1))).execute()
|
SearchResponse searchResponse = client().prepareSearch("test").
|
||||||
.actionGet();
|
setQuery(hasChildQuery("test", matchQuery("foo", 1), ScoreMode.None))
|
||||||
|
.get();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1"));
|
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1"));
|
||||||
@ -287,11 +287,11 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
for (int i = 1; i <= 10; i++) {
|
for (int i = 1; i <= 10; i++) {
|
||||||
logger.info("Round {}", i);
|
logger.info("Round {}", i);
|
||||||
SearchResponse searchResponse = client().prepareSearch("test")
|
SearchResponse searchResponse = client().prepareSearch("test")
|
||||||
.setQuery(constantScoreQuery(hasChildQuery("child", matchAllQuery()).scoreMode(ScoreMode.Max)))
|
.setQuery(constantScoreQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.Max)))
|
||||||
.get();
|
.get();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
searchResponse = client().prepareSearch("test")
|
searchResponse = client().prepareSearch("test")
|
||||||
.setQuery(constantScoreQuery(hasParentQuery("parent", matchAllQuery()).score(true)))
|
.setQuery(constantScoreQuery(hasParentQuery("parent", matchAllQuery(), true)))
|
||||||
.get();
|
.get();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
}
|
}
|
||||||
@ -305,7 +305,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
Map<String, Set<String>> parentToChildren = new HashMap<>();
|
Map<String, Set<String>> parentToChildren = new HashMap<>();
|
||||||
// Childless parent
|
// Childless parent
|
||||||
client().prepareIndex("test", "parent", "p0").setSource("p_field", "p0").get();
|
client().prepareIndex("test", "parent", "p0").setSource("p_field", "p0").get();
|
||||||
parentToChildren.put("p0", new HashSet<String>());
|
parentToChildren.put("p0", new HashSet<>());
|
||||||
|
|
||||||
String previousParentId = null;
|
String previousParentId = null;
|
||||||
int numChildDocs = 32;
|
int numChildDocs = 32;
|
||||||
@ -332,7 +332,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
assertThat(parentToChildren.isEmpty(), equalTo(false));
|
assertThat(parentToChildren.isEmpty(), equalTo(false));
|
||||||
for (Map.Entry<String, Set<String>> parentToChildrenEntry : parentToChildren.entrySet()) {
|
for (Map.Entry<String, Set<String>> parentToChildrenEntry : parentToChildren.entrySet()) {
|
||||||
SearchResponse searchResponse = client().prepareSearch("test")
|
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();
|
.setSize(numChildDocsPerParent).get();
|
||||||
|
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
@ -369,39 +369,45 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
|
|
||||||
// HAS CHILD QUERY
|
// HAS CHILD QUERY
|
||||||
|
|
||||||
SearchResponse searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("child", termQuery("c_field", "yellow"))).execute()
|
SearchResponse searchResponse = client().prepareSearch("test")
|
||||||
.actionGet();
|
.setQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.None))
|
||||||
|
.get();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1"));
|
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1"));
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("child", termQuery("c_field", "blue"))).execute()
|
searchResponse = client().prepareSearch("test")
|
||||||
.actionGet();
|
.setQuery(hasChildQuery("child", termQuery("c_field", "blue"), ScoreMode.None))
|
||||||
|
.get();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p2"));
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(2L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(2L));
|
||||||
assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("p2"), equalTo("p1")));
|
assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("p2"), equalTo("p1")));
|
||||||
assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("p2"), equalTo("p1")));
|
assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("p2"), equalTo("p1")));
|
||||||
|
|
||||||
// HAS CHILD FILTER
|
// HAS CHILD FILTER
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test")
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1"));
|
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();
|
.get();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p2"));
|
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();
|
.get();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(2L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(2L));
|
||||||
@ -427,7 +433,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
|
|
||||||
SearchResponse searchResponse = client()
|
SearchResponse searchResponse = client()
|
||||||
.prepareSearch("test")
|
.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(
|
.addAggregation(AggregationBuilders.global("global").subAggregation(
|
||||||
AggregationBuilders.filter("filter", boolQuery().should(termQuery("c_field", "red")).should(termQuery("c_field", "yellow"))).subAggregation(
|
AggregationBuilders.filter("filter", boolQuery().should(termQuery("c_field", "red")).should(termQuery("c_field", "yellow"))).subAggregation(
|
||||||
AggregationBuilders.terms("facet1").field("c_field")))).get();
|
AggregationBuilders.terms("facet1").field("c_field")))).get();
|
||||||
@ -462,7 +468,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
refresh();
|
refresh();
|
||||||
|
|
||||||
SearchResponse searchResponse = client().prepareSearch("test")
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1"));
|
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1"));
|
||||||
@ -474,7 +480,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
client().admin().indices().prepareRefresh().get();
|
client().admin().indices().prepareRefresh().get();
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test")
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1"));
|
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1"));
|
||||||
@ -498,11 +504,12 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
refresh();
|
refresh();
|
||||||
|
|
||||||
SearchResponse searchResponse = client().prepareSearch("test").setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
|
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);
|
assertNoFailures(searchResponse);
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test").setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
|
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();
|
.actionGet();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
}
|
}
|
||||||
@ -521,12 +528,12 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
client().admin().indices().prepareFlush("test").get();
|
client().admin().indices().prepareFlush("test").get();
|
||||||
|
|
||||||
SearchResponse searchResponse = client().prepareSearch("test")
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test")
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
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();
|
client().prepareIndex("test", "child", "c1").setSource("c_field", "1").setParent(parentId).get();
|
||||||
refresh();
|
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();
|
.get();
|
||||||
assertHitCount(countResponse, 1L);
|
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();
|
.get();
|
||||||
assertHitCount(countResponse, 1L);
|
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();
|
.get();
|
||||||
assertHitCount(countResponse, 1L);
|
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();
|
.get();
|
||||||
assertHitCount(countResponse, 1L);
|
assertHitCount(countResponse, 1L);
|
||||||
}
|
}
|
||||||
@ -572,20 +581,20 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
|
|
||||||
SearchResponse searchResponse = client().prepareSearch("test")
|
SearchResponse searchResponse = client().prepareSearch("test")
|
||||||
.setExplain(true)
|
.setExplain(true)
|
||||||
.setQuery(hasChildQuery("child", termQuery("c_field", "1")).scoreMode(ScoreMode.Max))
|
.setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max))
|
||||||
.get();
|
.get();
|
||||||
assertHitCount(searchResponse, 1L);
|
assertHitCount(searchResponse, 1L);
|
||||||
assertThat(searchResponse.getHits().getAt(0).explanation().getDescription(), containsString("join value p1"));
|
assertThat(searchResponse.getHits().getAt(0).explanation().getDescription(), containsString("join value p1"));
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test")
|
searchResponse = client().prepareSearch("test")
|
||||||
.setExplain(true)
|
.setExplain(true)
|
||||||
.setQuery(hasParentQuery("parent", termQuery("p_field", "1")).score(true))
|
.setQuery(hasParentQuery("parent", termQuery("p_field", "1"), true))
|
||||||
.get();
|
.get();
|
||||||
assertHitCount(searchResponse, 1L);
|
assertHitCount(searchResponse, 1L);
|
||||||
assertThat(searchResponse.getHits().getAt(0).explanation().getDescription(), containsString("join value p1"));
|
assertThat(searchResponse.getHits().getAt(0).explanation().getDescription(), containsString("join value p1"));
|
||||||
|
|
||||||
ExplainResponse explainResponse = client().prepareExplain("test", "parent", parentId)
|
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();
|
.get();
|
||||||
assertThat(explainResponse.isExists(), equalTo(true));
|
assertThat(explainResponse.isExists(), equalTo(true));
|
||||||
assertThat(explainResponse.getExplanation().getDetails()[0].getDescription(), containsString("join value p1"));
|
assertThat(explainResponse.getExplanation().getDetails()[0].getDescription(), containsString("join value p1"));
|
||||||
@ -662,7 +671,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
"child",
|
"child",
|
||||||
QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0),
|
QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0),
|
||||||
fieldValueFactorFunction("c_field1"))
|
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().totalHits(), equalTo(3L));
|
||||||
assertThat(response.getHits().hits()[0].id(), equalTo("1"));
|
assertThat(response.getHits().hits()[0].id(), equalTo("1"));
|
||||||
@ -679,7 +688,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
"child",
|
"child",
|
||||||
QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0),
|
QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0),
|
||||||
fieldValueFactorFunction("c_field1"))
|
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().totalHits(), equalTo(3L));
|
||||||
assertThat(response.getHits().hits()[0].id(), equalTo("3"));
|
assertThat(response.getHits().hits()[0].id(), equalTo("3"));
|
||||||
@ -696,7 +705,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
"child",
|
"child",
|
||||||
QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0),
|
QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0),
|
||||||
fieldValueFactorFunction("c_field1"))
|
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().totalHits(), equalTo(3L));
|
||||||
assertThat(response.getHits().hits()[0].id(), equalTo("3"));
|
assertThat(response.getHits().hits()[0].id(), equalTo("3"));
|
||||||
@ -713,7 +722,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
"parent",
|
"parent",
|
||||||
QueryBuilders.functionScoreQuery(matchQuery("p_field1", "p_value3"),
|
QueryBuilders.functionScoreQuery(matchQuery("p_field1", "p_value3"),
|
||||||
fieldValueFactorFunction("p_field2"))
|
fieldValueFactorFunction("p_field2"))
|
||||||
.boostMode(CombineFunction.REPLACE)).score(true))
|
.boostMode(CombineFunction.REPLACE), true))
|
||||||
.addSort(SortBuilders.fieldSort("c_field3")).addSort(SortBuilders.scoreSort()).get();
|
.addSort(SortBuilders.fieldSort("c_field3")).addSort(SortBuilders.scoreSort()).get();
|
||||||
|
|
||||||
assertThat(response.getHits().totalHits(), equalTo(7L));
|
assertThat(response.getHits().totalHits(), equalTo(7L));
|
||||||
@ -741,27 +750,27 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
ensureGreen();
|
ensureGreen();
|
||||||
|
|
||||||
SearchResponse response = client().prepareSearch("test")
|
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);
|
assertNoFailures(response);
|
||||||
assertThat(response.getHits().totalHits(), equalTo(0L));
|
assertThat(response.getHits().totalHits(), equalTo(0L));
|
||||||
|
|
||||||
client().prepareIndex("test", "child1").setSource(jsonBuilder().startObject().field("text", "value").endObject()).setRefresh(true)
|
client().prepareIndex("test", "child1").setSource(jsonBuilder().startObject().field("text", "value").endObject()).setRefresh(true)
|
||||||
.get();
|
.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);
|
assertNoFailures(response);
|
||||||
assertThat(response.getHits().totalHits(), equalTo(0L));
|
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();
|
.get();
|
||||||
assertNoFailures(response);
|
assertNoFailures(response);
|
||||||
assertThat(response.getHits().totalHits(), equalTo(0L));
|
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);
|
assertNoFailures(response);
|
||||||
assertThat(response.getHits().totalHits(), equalTo(0L));
|
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();
|
.get();
|
||||||
assertNoFailures(response);
|
assertNoFailures(response);
|
||||||
assertThat(response.getHits().totalHits(), equalTo(0L));
|
assertThat(response.getHits().totalHits(), equalTo(0L));
|
||||||
@ -781,13 +790,14 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
client().admin().indices().prepareFlush("test").get();
|
client().admin().indices().prepareFlush("test").get();
|
||||||
|
|
||||||
SearchResponse searchResponse = client().prepareSearch("test")
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
assertThat(searchResponse.getHits().hits()[0].id(), equalTo("1"));
|
assertThat(searchResponse.getHits().hits()[0].id(), equalTo("1"));
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test")
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
assertThat(searchResponse.getHits().hits()[0].id(), equalTo("2"));
|
assertThat(searchResponse.getHits().hits()[0].id(), equalTo("2"));
|
||||||
@ -806,19 +816,21 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
refresh();
|
refresh();
|
||||||
|
|
||||||
SearchResponse searchResponse = client().prepareSearch("test")
|
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"));
|
assertSearchHit(searchResponse, 1, hasId("1"));
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test")
|
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"));
|
assertSearchHit(searchResponse, 1, hasId("2"));
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test")
|
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"));
|
assertSearchHit(searchResponse, 1, hasId("1"));
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test")
|
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"));
|
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};
|
SearchType[] searchTypes = new SearchType[]{SearchType.QUERY_THEN_FETCH, SearchType.DFS_QUERY_THEN_FETCH};
|
||||||
for (SearchType searchType : searchTypes) {
|
for (SearchType searchType : searchTypes) {
|
||||||
SearchResponse searchResponse = client().prepareSearch("test").setSearchType(searchType)
|
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();
|
.setSize(5).get();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(10L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(10L));
|
||||||
@ -856,7 +869,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
assertThat(searchResponse.getHits().hits()[4].id(), equalTo("p004"));
|
assertThat(searchResponse.getHits().hits()[4].id(), equalTo("p004"));
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test").setSearchType(searchType)
|
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();
|
.setSize(5).get();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(500L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(500L));
|
||||||
@ -886,7 +899,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
refresh();
|
refresh();
|
||||||
|
|
||||||
SearchResponse searchResponse = client().prepareSearch("test")
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1"));
|
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("p1"));
|
||||||
@ -896,7 +909,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
.prepareSearch("test")
|
.prepareSearch("test")
|
||||||
.setQuery(
|
.setQuery(
|
||||||
boolQuery().must(matchQuery("c_field", "x")).must(
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(2L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(2L));
|
||||||
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("c3"));
|
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("c3"));
|
||||||
@ -911,7 +924,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
client().admin().indices().prepareRefresh("test").get();
|
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();
|
.get();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
@ -922,7 +935,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
.prepareSearch("test")
|
.prepareSearch("test")
|
||||||
.setQuery(
|
.setQuery(
|
||||||
boolQuery().must(matchQuery("c_field", "x")).must(
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(2L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(2L));
|
||||||
assertThat(searchResponse.getHits().getAt(0).id(), Matchers.anyOf(equalTo("c3"), equalTo("c4")));
|
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();
|
client().prepareIndex("test", "child", "c5").setSource("c_field", "x").setParent("p2").get();
|
||||||
refresh();
|
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!
|
.setMinScore(3) // Score needs to be 3 or above!
|
||||||
.get();
|
.get();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
@ -1035,7 +1048,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
client().admin().indices().prepareRefresh("test").get();
|
client().admin().indices().prepareRefresh("test").get();
|
||||||
|
|
||||||
SearchResponse searchResponse = client().prepareSearch("test")
|
SearchResponse searchResponse = client().prepareSearch("test")
|
||||||
.setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "blue"))))
|
.setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "blue"), ScoreMode.None)))
|
||||||
.get();
|
.get();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
@ -1044,7 +1057,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
client().admin().indices().prepareRefresh("test").get();
|
client().admin().indices().prepareRefresh("test").get();
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test")
|
searchResponse = client().prepareSearch("test")
|
||||||
.setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "blue"))))
|
.setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "blue"), ScoreMode.None)))
|
||||||
.get();
|
.get();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(2L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(2L));
|
||||||
@ -1053,24 +1066,24 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
private QueryBuilder randomHasChild(String type, String field, String value) {
|
private QueryBuilder randomHasChild(String type, String field, String value) {
|
||||||
if (randomBoolean()) {
|
if (randomBoolean()) {
|
||||||
if (randomBoolean()) {
|
if (randomBoolean()) {
|
||||||
return constantScoreQuery(hasChildQuery(type, termQuery(field, value)));
|
return constantScoreQuery(hasChildQuery(type, termQuery(field, value), ScoreMode.None));
|
||||||
} else {
|
} else {
|
||||||
return boolQuery().must(matchAllQuery()).filter(hasChildQuery(type, termQuery(field, value)));
|
return boolQuery().must(matchAllQuery()).filter(hasChildQuery(type, termQuery(field, value), ScoreMode.None));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return hasChildQuery(type, termQuery(field, value));
|
return hasChildQuery(type, termQuery(field, value), ScoreMode.None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private QueryBuilder randomHasParent(String type, String field, String value) {
|
private QueryBuilder randomHasParent(String type, String field, String value) {
|
||||||
if (randomBoolean()) {
|
if (randomBoolean()) {
|
||||||
if (randomBoolean()) {
|
if (randomBoolean()) {
|
||||||
return constantScoreQuery(hasParentQuery(type, termQuery(field, value)));
|
return constantScoreQuery(hasParentQuery(type, termQuery(field, value), false));
|
||||||
} else {
|
} else {
|
||||||
return boolQuery().must(matchAllQuery()).filter(hasParentQuery(type, termQuery(field, value)));
|
return boolQuery().must(matchAllQuery()).filter(hasParentQuery(type, termQuery(field, value), false));
|
||||||
}
|
}
|
||||||
} else {
|
} 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",
|
"child_type_one",
|
||||||
boolQuery().must(
|
boolQuery().must(
|
||||||
queryStringQuery("name:William*").analyzeWildcard(true)
|
queryStringQuery("name:William*").analyzeWildcard(true)
|
||||||
)
|
),
|
||||||
)
|
ScoreMode.None)
|
||||||
)
|
),
|
||||||
)
|
ScoreMode.None)
|
||||||
)
|
)
|
||||||
).get();
|
).get();
|
||||||
assertHitCount(searchResponse, 1L);
|
assertHitCount(searchResponse, 1L);
|
||||||
@ -1118,10 +1131,10 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
"child_type_two",
|
"child_type_two",
|
||||||
boolQuery().must(
|
boolQuery().must(
|
||||||
queryStringQuery("name:William*").analyzeWildcard(true)
|
queryStringQuery("name:William*").analyzeWildcard(true)
|
||||||
)
|
),
|
||||||
)
|
ScoreMode.None)
|
||||||
)
|
),
|
||||||
)
|
ScoreMode.None)
|
||||||
)
|
)
|
||||||
).get();
|
).get();
|
||||||
assertHitCount(searchResponse, 0L);
|
assertHitCount(searchResponse, 0L);
|
||||||
@ -1203,13 +1216,13 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
|
|
||||||
ScoreMode scoreMode = randomFrom(ScoreMode.values());
|
ScoreMode scoreMode = randomFrom(ScoreMode.values());
|
||||||
SearchResponse searchResponse = client().prepareSearch("test")
|
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();
|
.get();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test")
|
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();
|
.get();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(2L));
|
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();
|
client().prepareIndex("test", "child", "c1").setSource("c_field", "1").setParent(parentId).get();
|
||||||
refresh();
|
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();
|
.get();
|
||||||
assertHitCount(searchResponse, 1L);
|
assertHitCount(searchResponse, 1L);
|
||||||
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1));
|
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1));
|
||||||
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("test"));
|
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();
|
.get();
|
||||||
assertHitCount(searchResponse, 1L);
|
assertHitCount(searchResponse, 1L);
|
||||||
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1));
|
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1));
|
||||||
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("test"));
|
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();
|
.get();
|
||||||
assertHitCount(searchResponse, 1L);
|
assertHitCount(searchResponse, 1L);
|
||||||
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1));
|
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1));
|
||||||
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("test"));
|
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();
|
.get();
|
||||||
assertHitCount(searchResponse, 1L);
|
assertHitCount(searchResponse, 1L);
|
||||||
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1));
|
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1));
|
||||||
@ -1264,7 +1277,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
client().prepareSearch("test")
|
client().prepareSearch("test")
|
||||||
.setQuery(hasChildQuery("child", termQuery("c_field", "1")))
|
.setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.None))
|
||||||
.get();
|
.get();
|
||||||
fail();
|
fail();
|
||||||
} catch (SearchPhaseExecutionException e) {
|
} catch (SearchPhaseExecutionException e) {
|
||||||
@ -1273,7 +1286,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
client().prepareSearch("test")
|
client().prepareSearch("test")
|
||||||
.setQuery(hasChildQuery("child", termQuery("c_field", "1")).scoreMode(ScoreMode.Max))
|
.setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max))
|
||||||
.get();
|
.get();
|
||||||
fail();
|
fail();
|
||||||
} catch (SearchPhaseExecutionException e) {
|
} catch (SearchPhaseExecutionException e) {
|
||||||
@ -1282,7 +1295,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
client().prepareSearch("test")
|
client().prepareSearch("test")
|
||||||
.setPostFilter(hasChildQuery("child", termQuery("c_field", "1")))
|
.setPostFilter(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.None))
|
||||||
.get();
|
.get();
|
||||||
fail();
|
fail();
|
||||||
} catch (SearchPhaseExecutionException e) {
|
} catch (SearchPhaseExecutionException e) {
|
||||||
@ -1291,7 +1304,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
client().prepareSearch("test")
|
client().prepareSearch("test")
|
||||||
.setQuery(hasParentQuery("parent", termQuery("p_field", "1")).score(true))
|
.setQuery(hasParentQuery("parent", termQuery("p_field", "1"), true))
|
||||||
.get();
|
.get();
|
||||||
fail();
|
fail();
|
||||||
} catch (SearchPhaseExecutionException e) {
|
} catch (SearchPhaseExecutionException e) {
|
||||||
@ -1300,7 +1313,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
client().prepareSearch("test")
|
client().prepareSearch("test")
|
||||||
.setPostFilter(hasParentQuery("parent", termQuery("p_field", "1")))
|
.setPostFilter(hasParentQuery("parent", termQuery("p_field", "1"), false))
|
||||||
.get();
|
.get();
|
||||||
fail();
|
fail();
|
||||||
} catch (SearchPhaseExecutionException e) {
|
} catch (SearchPhaseExecutionException e) {
|
||||||
@ -1360,7 +1373,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
SearchResponse searchResponse = client().prepareSearch()
|
SearchResponse searchResponse = client().prepareSearch()
|
||||||
.setQuery(boolQuery().must(matchAllQuery()).filter(boolQuery()
|
.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())))
|
.must(matchAllQuery())))
|
||||||
.get();
|
.get();
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(2L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(2L));
|
||||||
@ -1372,7 +1385,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
|
|
||||||
SearchResponse searchResponse = client().prepareSearch()
|
SearchResponse searchResponse = client().prepareSearch()
|
||||||
.setQuery(boolQuery().must(matchAllQuery()).filter(boolQuery()
|
.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())))
|
.must(matchAllQuery())))
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
@ -1392,10 +1405,10 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
refresh();
|
refresh();
|
||||||
|
|
||||||
QueryBuilder[] queries = new QueryBuilder[]{
|
QueryBuilder[] queries = new QueryBuilder[]{
|
||||||
hasChildQuery("child", matchAllQuery()),
|
hasChildQuery("child", matchAllQuery(), ScoreMode.None),
|
||||||
boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", matchAllQuery())),
|
boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", matchAllQuery(), ScoreMode.None)),
|
||||||
hasParentQuery("parent", matchAllQuery()),
|
hasParentQuery("parent", matchAllQuery(), false),
|
||||||
boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", matchAllQuery()))
|
boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", matchAllQuery(), false))
|
||||||
};
|
};
|
||||||
|
|
||||||
for (QueryBuilder query : queries) {
|
for (QueryBuilder query : queries) {
|
||||||
@ -1436,7 +1449,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
|
|
||||||
SearchResponse resp;
|
SearchResponse resp;
|
||||||
resp = client().prepareSearch("test")
|
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();
|
.get();
|
||||||
assertHitCount(resp, 1L);
|
assertHitCount(resp, 1L);
|
||||||
}
|
}
|
||||||
@ -1473,22 +1486,22 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
indexRandom(true, indexRequests);
|
indexRandom(true, indexRequests);
|
||||||
|
|
||||||
SearchResponse searchResponse = client().prepareSearch("test")
|
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();
|
.get();
|
||||||
assertHitCount(searchResponse, 0L);
|
assertHitCount(searchResponse, 0L);
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test")
|
searchResponse = client().prepareSearch("test")
|
||||||
.setQuery(hasParentQuery("parent", constantScoreQuery(boolQuery().mustNot(termQuery("field1", "a")))))
|
.setQuery(hasParentQuery("parent", constantScoreQuery(boolQuery().mustNot(termQuery("field1", "a"))), false))
|
||||||
.get();
|
.get();
|
||||||
assertHitCount(searchResponse, 0L);
|
assertHitCount(searchResponse, 0L);
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test")
|
searchResponse = client().prepareSearch("test")
|
||||||
.setQuery(constantScoreQuery(hasParentQuery("parent", termQuery("field1", "a"))))
|
.setQuery(constantScoreQuery(hasParentQuery("parent", termQuery("field1", "a"), false)))
|
||||||
.get();
|
.get();
|
||||||
assertHitCount(searchResponse, 2L);
|
assertHitCount(searchResponse, 2L);
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test")
|
searchResponse = client().prepareSearch("test")
|
||||||
.setQuery(hasParentQuery("parent", constantScoreQuery(termQuery("field1", "a"))))
|
.setQuery(hasParentQuery("parent", constantScoreQuery(termQuery("field1", "a")), false))
|
||||||
.get();
|
.get();
|
||||||
assertHitCount(searchResponse, 2L);
|
assertHitCount(searchResponse, 2L);
|
||||||
}
|
}
|
||||||
@ -1538,11 +1551,8 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
new FunctionScoreQueryBuilder.FilterFunctionBuilder(weightFactorFunction(1)),
|
new FunctionScoreQueryBuilder.FilterFunctionBuilder(weightFactorFunction(1)),
|
||||||
new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("foo", "three"), weightFactorFunction(1)),
|
new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("foo", "three"), weightFactorFunction(1)),
|
||||||
new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("foo", "four"), weightFactorFunction(1))
|
new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("foo", "four"), weightFactorFunction(1))
|
||||||
}).boostMode(CombineFunction.REPLACE).scoreMode(FiltersFunctionScoreQuery.ScoreMode.SUM)).scoreMode(scoreMode).minChildren(minChildren);
|
}).boostMode(CombineFunction.REPLACE).scoreMode(FiltersFunctionScoreQuery.ScoreMode.SUM), scoreMode)
|
||||||
|
.minMaxChildren(minChildren, maxChildren != null ? maxChildren : HasChildQueryBuilder.DEFAULT_MAX_CHILDREN);
|
||||||
if (maxChildren != null) {
|
|
||||||
hasChildQuery.maxChildren(maxChildren);
|
|
||||||
}
|
|
||||||
|
|
||||||
return client()
|
return client()
|
||||||
.prepareSearch("test")
|
.prepareSearch("test")
|
||||||
@ -1632,12 +1642,8 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
assertThat(response.getHits().hits()[0].id(), equalTo("3"));
|
assertThat(response.getHits().hits()[0].id(), equalTo("3"));
|
||||||
assertThat(response.getHits().hits()[0].score(), equalTo(1f));
|
assertThat(response.getHits().hits()[0].score(), equalTo(1f));
|
||||||
|
|
||||||
try {
|
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> minMaxQuery(ScoreMode.None, 3, 2));
|
||||||
response = minMaxQuery(ScoreMode.None, 3, 2);
|
assertThat(e.getMessage(), equalTo("[has_child] 'max_children' is less than 'min_children'"));
|
||||||
fail();
|
|
||||||
} catch (SearchPhaseExecutionException e) {
|
|
||||||
assertThat(e.toString(), containsString("[has_child] 'max_children' is less than 'min_children'"));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Score mode = SUM
|
// Score mode = SUM
|
||||||
response = minMaxQuery(ScoreMode.Total, 0, null);
|
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].id(), equalTo("3"));
|
||||||
assertThat(response.getHits().hits()[0].score(), equalTo(3f));
|
assertThat(response.getHits().hits()[0].score(), equalTo(3f));
|
||||||
|
|
||||||
try {
|
e = expectThrows(IllegalArgumentException.class, () -> minMaxQuery(ScoreMode.Total, 3, 2));
|
||||||
response = minMaxQuery(ScoreMode.Total, 3, 2);
|
assertThat(e.getMessage(), equalTo("[has_child] 'max_children' is less than 'min_children'"));
|
||||||
fail();
|
|
||||||
} catch (SearchPhaseExecutionException e) {
|
|
||||||
assertThat(e.toString(), containsString("[has_child] 'max_children' is less than 'min_children'"));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Score mode = MAX
|
// Score mode = MAX
|
||||||
response = minMaxQuery(ScoreMode.Max, 0, null);
|
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].id(), equalTo("3"));
|
||||||
assertThat(response.getHits().hits()[0].score(), equalTo(2f));
|
assertThat(response.getHits().hits()[0].score(), equalTo(2f));
|
||||||
|
|
||||||
try {
|
e = expectThrows(IllegalArgumentException.class, () -> minMaxQuery(ScoreMode.Max, 3, 2));
|
||||||
response = minMaxQuery(ScoreMode.Max, 3, 2);
|
assertThat(e.getMessage(), equalTo("[has_child] 'max_children' is less than 'min_children'"));
|
||||||
fail();
|
|
||||||
} catch (SearchPhaseExecutionException e) {
|
|
||||||
assertThat(e.toString(), containsString("[has_child] 'max_children' is less than 'min_children'"));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Score mode = AVG
|
// Score mode = AVG
|
||||||
response = minMaxQuery(ScoreMode.Avg, 0, null);
|
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].id(), equalTo("3"));
|
||||||
assertThat(response.getHits().hits()[0].score(), equalTo(1.5f));
|
assertThat(response.getHits().hits()[0].score(), equalTo(1.5f));
|
||||||
|
|
||||||
try {
|
e = expectThrows(IllegalArgumentException.class, () -> minMaxQuery(ScoreMode.Avg, 3, 2));
|
||||||
response = minMaxQuery(ScoreMode.Avg, 3, 2);
|
assertThat(e.getMessage(), equalTo("[has_child] 'max_children' is less than 'min_children'"));
|
||||||
fail();
|
|
||||||
} catch (SearchPhaseExecutionException e) {
|
|
||||||
assertThat(e.toString(), containsString("[has_child] 'max_children' is less than 'min_children'"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testParentFieldToNonExistingType() {
|
public void testParentFieldToNonExistingType() {
|
||||||
@ -1888,18 +1882,13 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
client().prepareSearch("test")
|
client().prepareSearch("test")
|
||||||
.setQuery(QueryBuilders.hasChildQuery("child", matchAllQuery()))
|
.setQuery(QueryBuilders.hasChildQuery("child", matchAllQuery(), ScoreMode.None))
|
||||||
.get();
|
.get();
|
||||||
fail();
|
fail();
|
||||||
} catch (SearchPhaseExecutionException e) {
|
} catch (SearchPhaseExecutionException e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static HasChildQueryBuilder hasChildQuery(String type, QueryBuilder queryBuilder) {
|
|
||||||
HasChildQueryBuilder hasChildQueryBuilder = QueryBuilders.hasChildQuery(type, queryBuilder);
|
|
||||||
return hasChildQueryBuilder;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testHasParentInnerQueryType() {
|
public void testHasParentInnerQueryType() {
|
||||||
assertAcked(prepareCreate("test").addMapping("parent-type").addMapping("child-type", "_parent", "type=parent-type"));
|
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();
|
client().prepareIndex("test", "child-type", "child-id").setParent("parent-id").setSource("{}").get();
|
||||||
@ -1907,7 +1896,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
refresh();
|
refresh();
|
||||||
//make sure that when we explicitly set a type, the inner query is executed in the context of the parent type instead
|
//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(
|
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");
|
assertSearchHits(searchResponse, "child-id");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1918,7 +1907,7 @@ public class ChildQuerySearchIT extends ESIntegTestCase {
|
|||||||
refresh();
|
refresh();
|
||||||
//make sure that when we explicitly set a type, the inner query is executed in the context of the child type instead
|
//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(
|
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");
|
assertSearchHits(searchResponse, "parent-id");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
package org.elasticsearch.search.innerhits;
|
package org.elasticsearch.search.innerhits;
|
||||||
|
|
||||||
|
import org.apache.lucene.search.join.ScoreMode;
|
||||||
import org.apache.lucene.util.ArrayUtil;
|
import org.apache.lucene.util.ArrayUtil;
|
||||||
import org.elasticsearch.action.index.IndexRequestBuilder;
|
import org.elasticsearch.action.index.IndexRequestBuilder;
|
||||||
import org.elasticsearch.action.search.SearchRequest;
|
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
|
// Inner hits can be defined in two ways: 1) with the query 2) as separate inner_hit definition
|
||||||
SearchRequest[] searchRequests = new SearchRequest[]{
|
SearchRequest[] searchRequests = new SearchRequest[]{
|
||||||
client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"))
|
client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.Avg).innerHit(
|
||||||
.innerHit(new InnerHitBuilder().setName("comment"))).request(),
|
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))
|
||||||
.innerHits(innerHitsBuilder).request()
|
.innerHits(innerHitsBuilder).request()
|
||||||
};
|
};
|
||||||
for (SearchRequest searchRequest : searchRequests) {
|
for (SearchRequest searchRequest : searchRequests) {
|
||||||
@ -148,12 +149,12 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
// separate inner_hit definition
|
// separate inner_hit definition
|
||||||
searchRequests = new SearchRequest[] {
|
searchRequests = new SearchRequest[] {
|
||||||
client().prepareSearch("articles")
|
client().prepareSearch("articles")
|
||||||
.setQuery(nestedQuery("comments", matchQuery("comments.message", "elephant")))
|
.setQuery(nestedQuery("comments", matchQuery("comments.message", "elephant"), ScoreMode.Avg))
|
||||||
.innerHits(innerHitsBuilder).request(),
|
.innerHits(innerHitsBuilder).request(),
|
||||||
client().prepareSearch("articles")
|
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")
|
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) {
|
for (SearchRequest searchRequest : searchRequests) {
|
||||||
SearchResponse response = client().search(searchRequest).actionGet();
|
SearchResponse response = client().search(searchRequest).actionGet();
|
||||||
@ -187,10 +188,10 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
innerHitsBuilder.addInnerHit("comments", innerHit);
|
innerHitsBuilder.addInnerHit("comments", innerHit);
|
||||||
searchRequests = new SearchRequest[] {
|
searchRequests = new SearchRequest[] {
|
||||||
client().prepareSearch("articles")
|
client().prepareSearch("articles")
|
||||||
.setQuery(nestedQuery("comments", matchQuery("comments.message", "fox")))
|
.setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.Avg))
|
||||||
.innerHits(innerHitsBuilder).request(),
|
.innerHits(innerHitsBuilder).request(),
|
||||||
client().prepareSearch("articles")
|
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"))
|
new InnerHitBuilder().setHighlightBuilder(new HighlightBuilder().field("comments.message"))
|
||||||
.setExplain(true)
|
.setExplain(true)
|
||||||
.addFieldDataField("comments.message")
|
.addFieldDataField("comments.message")
|
||||||
@ -252,14 +253,14 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
} else {
|
} else {
|
||||||
BoolQueryBuilder boolQuery = new BoolQueryBuilder();
|
BoolQueryBuilder boolQuery = new BoolQueryBuilder();
|
||||||
if (randomBoolean()) {
|
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))));
|
.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)));
|
.addSort(new FieldSortBuilder("_doc").order(SortOrder.DESC)).setSize(size)));
|
||||||
} else {
|
} 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)))));
|
.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)))));
|
.setSize(size).addSort(new FieldSortBuilder("_doc").order(SortOrder.DESC)))));
|
||||||
}
|
}
|
||||||
searchResponse = client().prepareSearch("idx")
|
searchResponse = client().prepareSearch("idx")
|
||||||
@ -317,11 +318,11 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
.setQuery(matchQuery("message", "fox")));
|
.setQuery(matchQuery("message", "fox")));
|
||||||
SearchRequest[] searchRequests = new SearchRequest[]{
|
SearchRequest[] searchRequests = new SearchRequest[]{
|
||||||
client().prepareSearch("articles")
|
client().prepareSearch("articles")
|
||||||
.setQuery(hasChildQuery("comment", matchQuery("message", "fox")))
|
.setQuery(hasChildQuery("comment", matchQuery("message", "fox"), ScoreMode.None))
|
||||||
.innerHits(innerHitsBuilder)
|
.innerHits(innerHitsBuilder)
|
||||||
.request(),
|
.request(),
|
||||||
client().prepareSearch("articles")
|
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()
|
.request()
|
||||||
};
|
};
|
||||||
for (SearchRequest searchRequest : searchRequests) {
|
for (SearchRequest searchRequest : searchRequests) {
|
||||||
@ -346,11 +347,11 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
.setQuery(matchQuery("message", "elephant")));
|
.setQuery(matchQuery("message", "elephant")));
|
||||||
searchRequests = new SearchRequest[] {
|
searchRequests = new SearchRequest[] {
|
||||||
client().prepareSearch("articles")
|
client().prepareSearch("articles")
|
||||||
.setQuery(hasChildQuery("comment", matchQuery("message", "elephant")))
|
.setQuery(hasChildQuery("comment", matchQuery("message", "elephant"), ScoreMode.None))
|
||||||
.innerHits(innerHitsBuilder)
|
.innerHits(innerHitsBuilder)
|
||||||
.request(),
|
.request(),
|
||||||
client().prepareSearch("articles")
|
client().prepareSearch("articles")
|
||||||
.setQuery(hasChildQuery("comment", matchQuery("message", "elephant")).innerHit(new InnerHitBuilder()))
|
.setQuery(hasChildQuery("comment", matchQuery("message", "elephant"), ScoreMode.None).innerHit(new InnerHitBuilder()))
|
||||||
.request()
|
.request()
|
||||||
};
|
};
|
||||||
for (SearchRequest searchRequest : searchRequests) {
|
for (SearchRequest searchRequest : searchRequests) {
|
||||||
@ -382,13 +383,13 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
innerHitsBuilder.addInnerHit("comment", innerHit);
|
innerHitsBuilder.addInnerHit("comment", innerHit);
|
||||||
searchRequests = new SearchRequest[] {
|
searchRequests = new SearchRequest[] {
|
||||||
client().prepareSearch("articles")
|
client().prepareSearch("articles")
|
||||||
.setQuery(hasChildQuery("comment", matchQuery("message", "fox")))
|
.setQuery(hasChildQuery("comment", matchQuery("message", "fox"), ScoreMode.None))
|
||||||
.innerHits(innerHitsBuilder)
|
.innerHits(innerHitsBuilder)
|
||||||
.request(),
|
.request(),
|
||||||
|
|
||||||
client().prepareSearch("articles")
|
client().prepareSearch("articles")
|
||||||
.setQuery(
|
.setQuery(
|
||||||
hasChildQuery("comment", matchQuery("message", "fox")).innerHit(
|
hasChildQuery("comment", matchQuery("message", "fox"), ScoreMode.None).innerHit(
|
||||||
new InnerHitBuilder()
|
new InnerHitBuilder()
|
||||||
.addFieldDataField("message")
|
.addFieldDataField("message")
|
||||||
.setHighlightBuilder(new HighlightBuilder().field("message"))
|
.setHighlightBuilder(new HighlightBuilder().field("message"))
|
||||||
@ -455,11 +456,11 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
} else {
|
} else {
|
||||||
BoolQueryBuilder boolQuery = new BoolQueryBuilder();
|
BoolQueryBuilder boolQuery = new BoolQueryBuilder();
|
||||||
if (randomBoolean()) {
|
if (randomBoolean()) {
|
||||||
boolQuery.should(hasChildQuery("child1", matchAllQuery()).innerHit(new InnerHitBuilder().setName("a").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()).innerHit(new InnerHitBuilder().setName("b").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 {
|
} 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("child1", matchAllQuery(), ScoreMode.None).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("child2", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("b").addSort(new FieldSortBuilder("_uid").order(SortOrder.ASC)).setSize(size))));
|
||||||
}
|
}
|
||||||
searchResponse = client().prepareSearch("idx")
|
searchResponse = client().prepareSearch("idx")
|
||||||
.setSize(numDocs)
|
.setSize(numDocs)
|
||||||
@ -523,7 +524,7 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
.setQuery(
|
.setQuery(
|
||||||
boolQuery()
|
boolQuery()
|
||||||
.must(matchQuery("body", "fail2ban"))
|
.must(matchQuery("body", "fail2ban"))
|
||||||
.must(hasParentQuery("question", matchAllQuery()).innerHit(new InnerHitBuilder()))
|
.must(hasParentQuery("question", matchAllQuery(), false).innerHit(new InnerHitBuilder()))
|
||||||
).get();
|
).get();
|
||||||
assertNoFailures(response);
|
assertNoFailures(response);
|
||||||
assertHitCount(response, 2);
|
assertHitCount(response, 2);
|
||||||
@ -567,10 +568,10 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
InnerHitsBuilder innerHitsBuilder = new InnerHitsBuilder();
|
InnerHitsBuilder innerHitsBuilder = new InnerHitsBuilder();
|
||||||
innerHitsBuilder.addInnerHit("comment", new InnerHitBuilder()
|
innerHitsBuilder.addInnerHit("comment", new InnerHitBuilder()
|
||||||
.setParentChildType("comment")
|
.setParentChildType("comment")
|
||||||
.setQuery(hasChildQuery("remark", matchQuery("message", "good")))
|
.setQuery(hasChildQuery("remark", matchQuery("message", "good"), ScoreMode.None))
|
||||||
.setInnerHitsBuilder(innerInnerHitsBuilder));
|
.setInnerHitsBuilder(innerInnerHitsBuilder));
|
||||||
SearchResponse response = client().prepareSearch("articles")
|
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)
|
.innerHits(innerHitsBuilder)
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
@ -596,10 +597,10 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
innerHitsBuilder = new InnerHitsBuilder();
|
innerHitsBuilder = new InnerHitsBuilder();
|
||||||
innerHitsBuilder.addInnerHit("comment", new InnerHitBuilder()
|
innerHitsBuilder.addInnerHit("comment", new InnerHitBuilder()
|
||||||
.setParentChildType("comment")
|
.setParentChildType("comment")
|
||||||
.setQuery(hasChildQuery("remark", matchQuery("message", "bad")))
|
.setQuery(hasChildQuery("remark", matchQuery("message", "bad"), ScoreMode.None))
|
||||||
.setInnerHitsBuilder(innerInnerHitsBuilder));
|
.setInnerHitsBuilder(innerInnerHitsBuilder));
|
||||||
response = client().prepareSearch("articles")
|
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)
|
.innerHits(innerHitsBuilder)
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
@ -668,11 +669,11 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
InnerHitsBuilder innerHitsBuilder = new InnerHitsBuilder();
|
InnerHitsBuilder innerHitsBuilder = new InnerHitsBuilder();
|
||||||
innerHitsBuilder.addInnerHit("comment", new InnerHitBuilder()
|
innerHitsBuilder.addInnerHit("comment", new InnerHitBuilder()
|
||||||
.setNestedPath("comments")
|
.setNestedPath("comments")
|
||||||
.setQuery(nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "good")))
|
.setQuery(nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "good"), ScoreMode.Avg))
|
||||||
.setInnerHitsBuilder(innerInnerHitsBuilder)
|
.setInnerHitsBuilder(innerInnerHitsBuilder)
|
||||||
);
|
);
|
||||||
SearchResponse response = client().prepareSearch("articles")
|
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();
|
.innerHits(innerHitsBuilder).get();
|
||||||
assertNoFailures(response);
|
assertNoFailures(response);
|
||||||
assertHitCount(response, 1);
|
assertHitCount(response, 1);
|
||||||
@ -695,7 +696,7 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
|
|
||||||
// Directly refer to the second level:
|
// Directly refer to the second level:
|
||||||
response = client().prepareSearch("articles")
|
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();
|
.get();
|
||||||
assertNoFailures(response);
|
assertNoFailures(response);
|
||||||
assertHitCount(response, 1);
|
assertHitCount(response, 1);
|
||||||
@ -717,10 +718,10 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
innerHitsBuilder = new InnerHitsBuilder();
|
innerHitsBuilder = new InnerHitsBuilder();
|
||||||
innerHitsBuilder.addInnerHit("comment", new InnerHitBuilder()
|
innerHitsBuilder.addInnerHit("comment", new InnerHitBuilder()
|
||||||
.setNestedPath("comments")
|
.setNestedPath("comments")
|
||||||
.setQuery(nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "bad")))
|
.setQuery(nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "bad"), ScoreMode.Avg))
|
||||||
.setInnerHitsBuilder(innerInnerHitsBuilder));
|
.setInnerHitsBuilder(innerInnerHitsBuilder));
|
||||||
response = client().prepareSearch("articles")
|
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)
|
.innerHits(innerHitsBuilder)
|
||||||
.get();
|
.get();
|
||||||
assertNoFailures(response);
|
assertNoFailures(response);
|
||||||
@ -755,7 +756,7 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
indexRandom(true, requests);
|
indexRandom(true, requests);
|
||||||
|
|
||||||
SearchResponse response = client().prepareSearch("articles")
|
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();
|
.get();
|
||||||
assertNoFailures(response);
|
assertNoFailures(response);
|
||||||
assertHitCount(response, 1);
|
assertHitCount(response, 1);
|
||||||
@ -795,7 +796,7 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
indexRandom(true, requests);
|
indexRandom(true, requests);
|
||||||
|
|
||||||
SearchResponse response = client().prepareSearch("articles")
|
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();
|
.get();
|
||||||
assertNoFailures(response);
|
assertNoFailures(response);
|
||||||
assertHitCount(response, 1);
|
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());
|
assertThat(response.getHits().getAt(0).getInnerHits().get("comments.messages").getAt(0).getNestedIdentity().getChild(), nullValue());
|
||||||
|
|
||||||
response = client().prepareSearch("articles")
|
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();
|
.get();
|
||||||
assertNoFailures(response);
|
assertNoFailures(response);
|
||||||
assertHitCount(response, 1);
|
assertHitCount(response, 1);
|
||||||
@ -826,7 +827,7 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
.endObject()));
|
.endObject()));
|
||||||
indexRandom(true, requests);
|
indexRandom(true, requests);
|
||||||
response = client().prepareSearch("articles")
|
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();
|
.get();
|
||||||
assertNoFailures(response);
|
assertNoFailures(response);
|
||||||
assertHitCount(response, 1);
|
assertHitCount(response, 1);
|
||||||
@ -987,8 +988,8 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
.setQuery(nestedQuery("nested1", boolQuery()
|
.setQuery(nestedQuery("nested1", boolQuery()
|
||||||
.should(termQuery("nested1.n_field1", "n_value1_1").queryName("test1"))
|
.should(termQuery("nested1.n_field1", "n_value1_1").queryName("test1"))
|
||||||
.should(termQuery("nested1.n_field1", "n_value1_3").queryName("test2"))
|
.should(termQuery("nested1.n_field1", "n_value1_3").queryName("test2"))
|
||||||
.should(termQuery("nested1.n_field2", "n_value2_2").queryName("test3"))
|
.should(termQuery("nested1.n_field2", "n_value2_2").queryName("test3")),
|
||||||
).innerHit(new InnerHitBuilder().addSort(new FieldSortBuilder("nested1.n_field1").order(SortOrder.ASC))))
|
ScoreMode.Avg).innerHit(new InnerHitBuilder().addSort(new FieldSortBuilder("nested1.n_field1").order(SortOrder.ASC))))
|
||||||
.setSize(numDocs)
|
.setSize(numDocs)
|
||||||
.addSort("field1", SortOrder.ASC)
|
.addSort("field1", SortOrder.ASC)
|
||||||
.get();
|
.get();
|
||||||
@ -1027,7 +1028,7 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
indexRandom(true, requests);
|
indexRandom(true, requests);
|
||||||
|
|
||||||
SearchResponse response = client().prepareSearch("index")
|
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)
|
.addSort("_uid", SortOrder.ASC)
|
||||||
.get();
|
.get();
|
||||||
assertHitCount(response, 2);
|
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"));
|
assertThat(response.getHits().getAt(1).getInnerHits().get("child").getAt(0).getMatchedQueries()[0], equalTo("_name1"));
|
||||||
|
|
||||||
response = client().prepareSearch("index")
|
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)
|
.addSort("_uid", SortOrder.ASC)
|
||||||
.get();
|
.get();
|
||||||
assertHitCount(response, 1);
|
assertHitCount(response, 1);
|
||||||
@ -1060,7 +1061,7 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
indexRandom(true, requests);
|
indexRandom(true, requests);
|
||||||
|
|
||||||
SearchResponse response = client().prepareSearch("index1")
|
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)
|
.addSort("_uid", SortOrder.ASC)
|
||||||
.get();
|
.get();
|
||||||
assertNoFailures(response);
|
assertNoFailures(response);
|
||||||
@ -1078,7 +1079,7 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
.get();
|
.get();
|
||||||
|
|
||||||
response = client().prepareSearch("index2")
|
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)
|
.addSort("_uid", SortOrder.ASC)
|
||||||
.get();
|
.get();
|
||||||
assertNoFailures(response);
|
assertNoFailures(response);
|
||||||
@ -1097,7 +1098,7 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||||||
InnerHitsBuilder innerHitsBuilder = new InnerHitsBuilder();
|
InnerHitsBuilder innerHitsBuilder = new InnerHitsBuilder();
|
||||||
innerHitsBuilder.addInnerHit("my-inner-hit", new InnerHitBuilder().setParentChildType("child"));
|
innerHitsBuilder.addInnerHit("my-inner-hit", new InnerHitBuilder().setParentChildType("child"));
|
||||||
SearchResponse response = client().prepareSearch("index1")
|
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)
|
.innerHits(innerHitsBuilder)
|
||||||
.get();
|
.get();
|
||||||
assertHitCount(response, 1);
|
assertHitCount(response, 1);
|
||||||
|
@ -100,11 +100,11 @@ public class SimpleNestedIT extends ESIntegTestCase {
|
|||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(0L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(0L));
|
||||||
|
|
||||||
// now, do a nested query
|
// 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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
|
|
||||||
@ -129,19 +129,19 @@ public class SimpleNestedIT extends ESIntegTestCase {
|
|||||||
assertDocumentCount("test", 6);
|
assertDocumentCount("test", 6);
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1",
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
|
|
||||||
// filter
|
// filter
|
||||||
searchResponse = client().prepareSearch("test").setQuery(boolQuery().must(matchAllQuery()).mustNot(nestedQuery("nested1",
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
|
|
||||||
// check with type prefix
|
// check with type prefix
|
||||||
searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1",
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
|
|
||||||
@ -153,11 +153,11 @@ public class SimpleNestedIT extends ESIntegTestCase {
|
|||||||
flush();
|
flush();
|
||||||
assertDocumentCount("test", 3);
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
}
|
}
|
||||||
@ -191,42 +191,42 @@ public class SimpleNestedIT extends ESIntegTestCase {
|
|||||||
|
|
||||||
// do some multi nested queries
|
// do some multi nested queries
|
||||||
SearchResponse searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1",
|
SearchResponse searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1",
|
||||||
termQuery("nested1.field1", "1"))).execute().actionGet();
|
termQuery("nested1.field1", "1"), ScoreMode.Avg)).execute().actionGet();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1.nested2",
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1",
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1",
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1",
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(0L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(0L));
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1",
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(0L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(0L));
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1",
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(1L));
|
||||||
|
|
||||||
searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1",
|
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);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(0L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(0L));
|
||||||
}
|
}
|
||||||
@ -310,7 +310,7 @@ public class SimpleNestedIT extends ESIntegTestCase {
|
|||||||
.execute().actionGet();
|
.execute().actionGet();
|
||||||
|
|
||||||
SearchResponse searchResponse = client().prepareSearch("test")
|
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)
|
.setExplain(true)
|
||||||
.execute().actionGet();
|
.execute().actionGet();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
@ -988,7 +988,7 @@ public class SimpleNestedIT extends ESIntegTestCase {
|
|||||||
.addSort(SortBuilders.fieldSort("users.first")
|
.addSort(SortBuilders.fieldSort("users.first")
|
||||||
.order(SortOrder.ASC)
|
.order(SortOrder.ASC)
|
||||||
.setNestedPath("users")
|
.setNestedPath("users")
|
||||||
.setNestedFilter(nestedQuery("users.workstations", termQuery("users.workstations.stationid", "s5"))))
|
.setNestedFilter(nestedQuery("users.workstations", termQuery("users.workstations.stationid", "s5"), ScoreMode.Avg)))
|
||||||
.get();
|
.get();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertHitCount(searchResponse, 2);
|
assertHitCount(searchResponse, 2);
|
||||||
@ -1044,7 +1044,7 @@ public class SimpleNestedIT extends ESIntegTestCase {
|
|||||||
|
|
||||||
// only when querying with nested the fixed bitsets are loaded
|
// only when querying with nested the fixed bitsets are loaded
|
||||||
SearchResponse searchResponse = client().prepareSearch("test")
|
SearchResponse searchResponse = client().prepareSearch("test")
|
||||||
.setQuery(nestedQuery("array1", termQuery("array1.field1", "value1")))
|
.setQuery(nestedQuery("array1", termQuery("array1.field1", "value1"), ScoreMode.Avg))
|
||||||
.get();
|
.get();
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
assertThat(searchResponse.getHits().totalHits(), equalTo(5L));
|
assertThat(searchResponse.getHits().totalHits(), equalTo(5L));
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
package org.elasticsearch.search.query;
|
package org.elasticsearch.search.query;
|
||||||
|
|
||||||
|
import org.apache.lucene.search.join.ScoreMode;
|
||||||
import org.apache.lucene.util.English;
|
import org.apache.lucene.util.English;
|
||||||
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
|
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
|
||||||
import org.elasticsearch.action.index.IndexRequestBuilder;
|
import org.elasticsearch.action.index.IndexRequestBuilder;
|
||||||
@ -1773,7 +1774,7 @@ public class SearchQueryIT extends ESIntegTestCase {
|
|||||||
//has_child fails if executed on "simple" index
|
//has_child fails if executed on "simple" index
|
||||||
try {
|
try {
|
||||||
client().prepareSearch("simple")
|
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");
|
fail("Should have failed as has_child query can only be executed against parent-child types");
|
||||||
} catch (SearchPhaseExecutionException e) {
|
} catch (SearchPhaseExecutionException e) {
|
||||||
assertThat(e.shardFailures().length, greaterThan(0));
|
assertThat(e.shardFailures().length, greaterThan(0));
|
||||||
@ -1784,7 +1785,7 @@ public class SearchQueryIT extends ESIntegTestCase {
|
|||||||
|
|
||||||
//has_child doesn't get parsed for "simple" index
|
//has_child doesn't get parsed for "simple" index
|
||||||
SearchResponse searchResponse = client().prepareSearch("related", "simple")
|
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();
|
.noMatchQuery(matchQuery("text", "value1"))).get();
|
||||||
assertHitCount(searchResponse, 2L);
|
assertHitCount(searchResponse, 2L);
|
||||||
assertSearchHits(searchResponse, "1", "2");
|
assertSearchHits(searchResponse, "1", "2");
|
||||||
|
@ -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
|
//we check that the _parent field was set on the child document by using the has parent query
|
||||||
SearchResponse searchResponse = client().prepareSearch("test")
|
SearchResponse searchResponse = client().prepareSearch("test")
|
||||||
.setQuery(QueryBuilders.hasParentQuery("parent", QueryBuilders.matchAllQuery()))
|
.setQuery(QueryBuilders.hasParentQuery("parent", QueryBuilders.matchAllQuery(), false))
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
assertNoFailures(searchResponse);
|
assertNoFailures(searchResponse);
|
||||||
@ -468,7 +468,7 @@ public class BulkTests extends ESIntegTestCase {
|
|||||||
client().admin().indices().prepareRefresh("test").get();
|
client().admin().indices().prepareRefresh("test").get();
|
||||||
|
|
||||||
SearchResponse searchResponse = client().prepareSearch("test")
|
SearchResponse searchResponse = client().prepareSearch("test")
|
||||||
.setQuery(QueryBuilders.hasParentQuery("parent", QueryBuilders.matchAllQuery()))
|
.setQuery(QueryBuilders.hasParentQuery("parent", QueryBuilders.matchAllQuery(), false))
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
assertSearchHits(searchResponse, "child1");
|
assertSearchHits(searchResponse, "child1");
|
||||||
|
@ -102,8 +102,8 @@ public class ReindexParentChildTests extends ReindexTestCase {
|
|||||||
.setSource("foo", "bar").setRouting("united states"));
|
.setSource("foo", "bar").setRouting("united states"));
|
||||||
|
|
||||||
findsCountry = idsQuery("country").addIds("united states");
|
findsCountry = idsQuery("country").addIds("united states");
|
||||||
findsCity = hasParentQuery("country", findsCountry);
|
findsCity = hasParentQuery("country", findsCountry, false);
|
||||||
findsNeighborhood = hasParentQuery("city", findsCity);
|
findsNeighborhood = hasParentQuery("city", findsCity, false);
|
||||||
|
|
||||||
// Make sure we built the parent/child relationship
|
// Make sure we built the parent/child relationship
|
||||||
assertSearchHits(client().prepareSearch(indexName).setQuery(findsCity).get(), "pittsburgh");
|
assertSearchHits(client().prepareSearch(indexName).setQuery(findsCity).get(), "pittsburgh");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user