Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.elasticsearch.search.aggregations.AggregatorBuilder;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.innerhits.InnerHitsBuilder;
import org.elasticsearch.index.query.support.InnerHitsBuilder;
import org.elasticsearch.search.highlight.HighlightBuilder;
import org.elasticsearch.search.rescore.RescoreBuilder;
import org.elasticsearch.search.sort.SortBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,13 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.fielddata.IndexParentChildFieldData;
import org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.internal.ParentFieldMapper;
import org.elasticsearch.index.query.support.QueryInnerHits;
import org.elasticsearch.search.fetch.innerhits.InnerHitsContext;
import org.elasticsearch.search.fetch.innerhits.InnerHitsSubSearchContext;
import org.elasticsearch.index.query.support.InnerHitBuilder;

import java.io.IOException;
import java.util.Locale;
import java.util.Objects;

/**
Expand Down Expand Up @@ -77,16 +73,20 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil

private int maxChildren = DEFAULT_MAX_CHILDREN;

private QueryInnerHits queryInnerHits;
private InnerHitBuilder innerHitBuilder;

static final HasChildQueryBuilder PROTOTYPE = new HasChildQueryBuilder("", EmptyQueryBuilder.PROTOTYPE);

public HasChildQueryBuilder(String type, QueryBuilder query, int maxChildren, int minChildren, ScoreMode scoreMode, QueryInnerHits queryInnerHits) {
public HasChildQueryBuilder(String type, QueryBuilder query, int maxChildren, int minChildren, ScoreMode scoreMode, InnerHitBuilder innerHitBuilder) {
this(type, query);
scoreMode(scoreMode);
this.maxChildren = maxChildren;
this.minChildren = minChildren;
this.queryInnerHits = queryInnerHits;
this.innerHitBuilder = innerHitBuilder;
if (this.innerHitBuilder != null) {
this.innerHitBuilder.setParentChildType(type);
this.innerHitBuilder.setQuery(query);
}
}

public HasChildQueryBuilder(String type, QueryBuilder query) {
Expand Down Expand Up @@ -136,16 +136,18 @@ public HasChildQueryBuilder maxChildren(int maxChildren) {
/**
* Sets the query name for the filter that can be used when searching for matched_filters per hit.
*/
public HasChildQueryBuilder innerHit(QueryInnerHits queryInnerHits) {
this.queryInnerHits = queryInnerHits;
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.
*/
public QueryInnerHits innerHit() {
return queryInnerHits;
public InnerHitBuilder innerHit() {
return innerHitBuilder;
}

/**
Expand Down Expand Up @@ -193,8 +195,8 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.field(HasChildQueryParser.MIN_CHILDREN_FIELD.getPreferredName(), minChildren);
builder.field(HasChildQueryParser.MAX_CHILDREN_FIELD.getPreferredName(), maxChildren);
printBoostAndQueryName(builder);
if (queryInnerHits != null) {
queryInnerHits.toXContent(builder, params);
if (innerHitBuilder != null) {
builder.field(HasChildQueryParser.INNER_HITS_FIELD.getPreferredName(), innerHitBuilder, params);
}
builder.endObject();
}
Expand Down Expand Up @@ -226,20 +228,8 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
if (parentFieldMapper.active() == false) {
throw new QueryShardException(context, "[" + NAME + "] _parent field has no parent type configured");
}
if (queryInnerHits != null) {
try (XContentParser parser = queryInnerHits.getXcontentParser()) {
XContentParser.Token token = parser.nextToken();
if (token != XContentParser.Token.START_OBJECT) {
throw new IllegalStateException("start object expected but was: [" + token + "]");
}
InnerHitsSubSearchContext innerHits = context.getInnerHitsContext(parser);
if (innerHits != null) {
ParsedQuery parsedQuery = new ParsedQuery(innerQuery, context.copyNamedQueries());
InnerHitsContext.ParentChildInnerHits parentChildInnerHits = new InnerHitsContext.ParentChildInnerHits(innerHits.getSubSearchContext(), parsedQuery, null, context.getMapperService(), childDocMapper);
String name = innerHits.getName() != null ? innerHits.getName() : type;
context.addInnerHits(name, parentChildInnerHits);
}
}
if (innerHitBuilder != null) {
context.addInnerHit(innerHitBuilder);
}

String parentType = parentFieldMapper.type();
Expand Down Expand Up @@ -363,12 +353,12 @@ protected boolean doEquals(HasChildQueryBuilder that) {
&& Objects.equals(scoreMode, that.scoreMode)
&& Objects.equals(minChildren, that.minChildren)
&& Objects.equals(maxChildren, that.maxChildren)
&& Objects.equals(queryInnerHits, that.queryInnerHits);
&& Objects.equals(innerHitBuilder, that.innerHitBuilder);
}

@Override
protected int doHashCode() {
return Objects.hash(query, type, scoreMode, minChildren, maxChildren, queryInnerHits);
return Objects.hash(query, type, scoreMode, minChildren, maxChildren, innerHitBuilder);
}

protected HasChildQueryBuilder(StreamInput in) throws IOException {
Expand All @@ -378,9 +368,7 @@ protected HasChildQueryBuilder(StreamInput in) throws IOException {
final int ordinal = in.readVInt();
scoreMode = ScoreMode.values()[ordinal];
query = in.readQuery();
if (in.readBoolean()) {
queryInnerHits = new QueryInnerHits(in);
}
innerHitBuilder = InnerHitBuilder.optionalReadFromStream(in);
}

@Override
Expand All @@ -395,9 +383,9 @@ protected void doWriteTo(StreamOutput out) throws IOException {
out.writeInt(maxChildren());
out.writeVInt(scoreMode.ordinal());
out.writeQuery(query);
if (queryInnerHits != null) {
if (innerHitBuilder != null) {
out.writeBoolean(true);
queryInnerHits.writeTo(out);
innerHitBuilder.writeTo(out);
} else {
out.writeBoolean(false);
}
Expand All @@ -408,10 +396,10 @@ protected QueryBuilder<?> doRewrite(QueryRewriteContext queryRewriteContext) thr
QueryBuilder rewrite = query.rewrite(queryRewriteContext);
if (rewrite != query) {
HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder(type, rewrite);
hasChildQueryBuilder.minChildren = minChildren;
hasChildQueryBuilder.maxChildren = maxChildren;
hasChildQueryBuilder.scoreMode = scoreMode;
hasChildQueryBuilder.queryInnerHits = queryInnerHits;
hasChildQueryBuilder.minChildren(minChildren);
hasChildQueryBuilder.maxChildren(maxChildren);
hasChildQueryBuilder.scoreMode(scoreMode);
hasChildQueryBuilder.innerHit(innerHitBuilder);
return hasChildQueryBuilder;
}
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.support.QueryInnerHits;
import org.elasticsearch.index.query.support.InnerHitBuilder;

import java.io.IOException;
import java.util.Locale;
Expand Down Expand Up @@ -55,7 +55,7 @@ public HasChildQueryBuilder fromXContent(QueryParseContext parseContext) throws
int minChildren = HasChildQueryBuilder.DEFAULT_MIN_CHILDREN;
int maxChildren = HasChildQueryBuilder.DEFAULT_MAX_CHILDREN;
String queryName = null;
QueryInnerHits queryInnerHits = null;
InnerHitBuilder innerHitBuilder = null;
String currentFieldName = null;
XContentParser.Token token;
QueryBuilder iqb = null;
Expand All @@ -68,7 +68,7 @@ public HasChildQueryBuilder fromXContent(QueryParseContext parseContext) throws
if (parseContext.parseFieldMatcher().match(currentFieldName, QUERY_FIELD)) {
iqb = parseContext.parseInnerQueryBuilder();
} else if (parseContext.parseFieldMatcher().match(currentFieldName, INNER_HITS_FIELD)) {
queryInnerHits = new QueryInnerHits(parser);
innerHitBuilder = InnerHitBuilder.fromXContent(parser, parseContext);
} else {
throw new ParsingException(parser.getTokenLocation(), "[has_child] query does not support [" + currentFieldName + "]");
}
Expand All @@ -90,7 +90,7 @@ public HasChildQueryBuilder fromXContent(QueryParseContext parseContext) throws
}
}
}
HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder(childType, iqb, maxChildren, minChildren, scoreMode, queryInnerHits);
HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder(childType, iqb, maxChildren, minChildren, scoreMode, innerHitBuilder);
hasChildQueryBuilder.queryName(queryName);
hasChildQueryBuilder.boost(boost);
return hasChildQueryBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.internal.ParentFieldMapper;
import org.elasticsearch.index.query.support.QueryInnerHits;
import org.elasticsearch.search.fetch.innerhits.InnerHitsContext;
import org.elasticsearch.search.fetch.innerhits.InnerHitsSubSearchContext;
import org.elasticsearch.index.query.support.InnerHitBuilder;

import java.io.IOException;
import java.util.HashSet;
Expand All @@ -49,7 +46,7 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
private final QueryBuilder query;
private final String type;
private boolean score = DEFAULT_SCORE;
private QueryInnerHits innerHit;
private InnerHitBuilder innerHit;

/**
* @param type The parent type
Expand All @@ -66,10 +63,14 @@ public HasParentQueryBuilder(String type, QueryBuilder query) {
this.query = query;
}

public HasParentQueryBuilder(String type, QueryBuilder query, boolean score, QueryInnerHits innerHits) {
public HasParentQueryBuilder(String type, QueryBuilder query, boolean score, InnerHitBuilder innerHit) {
this(type, query);
this.score = score;
this.innerHit = innerHits;
this.innerHit = innerHit;
if (this.innerHit != null) {
this.innerHit.setParentChildType(type);
this.innerHit.setQuery(query);
}
}

/**
Expand All @@ -83,8 +84,10 @@ public HasParentQueryBuilder score(boolean score) {
/**
* Sets inner hit definition in the scope of this query and reusing the defined type and query.
*/
public HasParentQueryBuilder innerHit(QueryInnerHits innerHit) {
this.innerHit = innerHit;
public HasParentQueryBuilder innerHit(InnerHitBuilder innerHit) {
this.innerHit = Objects.requireNonNull(innerHit);
this.innerHit.setParentChildType(type);
this.innerHit.setQuery(query);
return this;
}

Expand Down Expand Up @@ -112,7 +115,7 @@ public String type() {
/**
* Returns inner hit definition in the scope of this query and reusing the defined type and query.
*/
public QueryInnerHits innerHit() {
public InnerHitBuilder innerHit() {
return innerHit;
}

Expand All @@ -137,19 +140,7 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
}

if (innerHit != null) {
try (XContentParser parser = innerHit.getXcontentParser()) {
XContentParser.Token token = parser.nextToken();
if (token != XContentParser.Token.START_OBJECT) {
throw new IllegalStateException("start object expected but was: [" + token + "]");
}
InnerHitsSubSearchContext innerHits = context.getInnerHitsContext(parser);
if (innerHits != null) {
ParsedQuery parsedQuery = new ParsedQuery(innerQuery, context.copyNamedQueries());
InnerHitsContext.ParentChildInnerHits parentChildInnerHits = new InnerHitsContext.ParentChildInnerHits(innerHits.getSubSearchContext(), parsedQuery, null, context.getMapperService(), parentDocMapper);
String name = innerHits.getName() != null ? innerHits.getName() : type;
context.addInnerHits(name, parentChildInnerHits);
}
}
context.addInnerHit(innerHit);
}

Set<String> childTypes = new HashSet<>();
Expand Down Expand Up @@ -200,7 +191,7 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.field(HasParentQueryParser.SCORE_FIELD.getPreferredName(), score);
printBoostAndQueryName(builder);
if (innerHit != null) {
innerHit.toXContent(builder, params);
builder.field(HasParentQueryParser.INNER_HITS_FIELD.getPreferredName(), innerHit, params);
}
builder.endObject();
}
Expand All @@ -214,9 +205,7 @@ protected HasParentQueryBuilder(StreamInput in) throws IOException {
type = in.readString();
score = in.readBoolean();
query = in.readQuery();
if (in.readBoolean()) {
innerHit = new QueryInnerHits(in);
}
innerHit = InnerHitBuilder.optionalReadFromStream(in);
}

@Override
Expand Down Expand Up @@ -255,8 +244,8 @@ protected QueryBuilder<?> doRewrite(QueryRewriteContext queryShardContext) throw
QueryBuilder rewrite = query.rewrite(queryShardContext);
if (rewrite != query) {
HasParentQueryBuilder hasParentQueryBuilder = new HasParentQueryBuilder(type, rewrite);
hasParentQueryBuilder.score = score;
hasParentQueryBuilder.innerHit = innerHit;
hasParentQueryBuilder.score(score);
hasParentQueryBuilder.innerHit(innerHit);
return hasParentQueryBuilder;
}
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.support.QueryInnerHits;
import org.elasticsearch.index.query.support.InnerHitBuilder;

import java.io.IOException;

Expand All @@ -35,6 +35,7 @@ public class HasParentQueryParser implements QueryParser<HasParentQueryBuilder>
public static final ParseField SCORE_MODE_FIELD = new ParseField("score_mode").withAllDeprecated("score");
public static final ParseField TYPE_FIELD = new ParseField("parent_type", "type");
public static final ParseField SCORE_FIELD = new ParseField("score");
public static final ParseField INNER_HITS_FIELD = new ParseField("inner_hits");

@Override
public String[] names() {
Expand All @@ -48,7 +49,7 @@ public HasParentQueryBuilder fromXContent(QueryParseContext parseContext) throws
String parentType = null;
boolean score = HasParentQueryBuilder.DEFAULT_SCORE;
String queryName = null;
QueryInnerHits innerHits = null;
InnerHitBuilder innerHits = null;

String currentFieldName = null;
XContentParser.Token token;
Expand All @@ -59,8 +60,8 @@ public HasParentQueryBuilder fromXContent(QueryParseContext parseContext) throws
} else if (token == XContentParser.Token.START_OBJECT) {
if (parseContext.parseFieldMatcher().match(currentFieldName, QUERY_FIELD)) {
iqb = parseContext.parseInnerQueryBuilder();
} else if ("inner_hits".equals(currentFieldName)) {
innerHits = new QueryInnerHits(parser);
} else if (parseContext.parseFieldMatcher().match(currentFieldName, INNER_HITS_FIELD)) {
innerHits = InnerHitBuilder.fromXContent(parser, parseContext);
} else {
throw new ParsingException(parser.getTokenLocation(), "[has_parent] query does not support [" + currentFieldName + "]");
}
Expand Down
Loading