Cut top_hits aggregation to registerAggregation
and remove its PROTOTYPE. Relates to #17085
This commit is contained in:
parent
c94302d246
commit
d223090d7d
|
@ -182,7 +182,7 @@ import org.elasticsearch.search.aggregations.metrics.stats.extended.InternalExte
|
||||||
import org.elasticsearch.search.aggregations.metrics.sum.InternalSum;
|
import org.elasticsearch.search.aggregations.metrics.sum.InternalSum;
|
||||||
import org.elasticsearch.search.aggregations.metrics.sum.SumParser;
|
import org.elasticsearch.search.aggregations.metrics.sum.SumParser;
|
||||||
import org.elasticsearch.search.aggregations.metrics.tophits.InternalTopHits;
|
import org.elasticsearch.search.aggregations.metrics.tophits.InternalTopHits;
|
||||||
import org.elasticsearch.search.aggregations.metrics.tophits.TopHitsParser;
|
import org.elasticsearch.search.aggregations.metrics.tophits.TopHitsAggregatorBuilder;
|
||||||
import org.elasticsearch.search.aggregations.metrics.valuecount.InternalValueCount;
|
import org.elasticsearch.search.aggregations.metrics.valuecount.InternalValueCount;
|
||||||
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCountParser;
|
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCountParser;
|
||||||
import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue;
|
import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue;
|
||||||
|
@ -482,7 +482,8 @@ public class SearchModule extends AbstractModule {
|
||||||
registerAggregation(NestedAggregatorBuilder::new, NestedAggregatorBuilder::parse, NestedAggregatorBuilder.AGGREGATION_FIELD_NAME);
|
registerAggregation(NestedAggregatorBuilder::new, NestedAggregatorBuilder::parse, NestedAggregatorBuilder.AGGREGATION_FIELD_NAME);
|
||||||
registerAggregation(ReverseNestedAggregatorBuilder::new, ReverseNestedAggregatorBuilder::parse,
|
registerAggregation(ReverseNestedAggregatorBuilder::new, ReverseNestedAggregatorBuilder::parse,
|
||||||
ReverseNestedAggregatorBuilder.AGGREGATION_NAME_FIELD);
|
ReverseNestedAggregatorBuilder.AGGREGATION_NAME_FIELD);
|
||||||
registerAggregatorParser(new TopHitsParser());
|
registerAggregation(TopHitsAggregatorBuilder::new, TopHitsAggregatorBuilder::parse,
|
||||||
|
TopHitsAggregatorBuilder.AGGREGATION_NAME_FIELD);
|
||||||
registerAggregation(GeoBoundsAggregatorBuilder::new, new GeoBoundsParser(), GeoBoundsAggregatorBuilder.AGGREGATION_NAME_FIED);
|
registerAggregation(GeoBoundsAggregatorBuilder::new, new GeoBoundsParser(), GeoBoundsAggregatorBuilder.AGGREGATION_NAME_FIED);
|
||||||
registerAggregation(GeoCentroidAggregatorBuilder::new, new GeoCentroidParser(),
|
registerAggregation(GeoCentroidAggregatorBuilder::new, new GeoCentroidParser(),
|
||||||
GeoCentroidAggregatorBuilder.AGGREGATION_NAME_FIELD);
|
GeoCentroidAggregatorBuilder.AGGREGATION_NAME_FIELD);
|
||||||
|
|
|
@ -20,10 +20,14 @@
|
||||||
package org.elasticsearch.search.aggregations.metrics.tophits;
|
package org.elasticsearch.search.aggregations.metrics.tophits;
|
||||||
|
|
||||||
import org.elasticsearch.common.Nullable;
|
import org.elasticsearch.common.Nullable;
|
||||||
|
import org.elasticsearch.common.ParseField;
|
||||||
|
import org.elasticsearch.common.ParsingException;
|
||||||
import org.elasticsearch.common.Strings;
|
import org.elasticsearch.common.Strings;
|
||||||
import org.elasticsearch.common.io.stream.StreamInput;
|
import org.elasticsearch.common.io.stream.StreamInput;
|
||||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
|
import org.elasticsearch.common.xcontent.XContentParser;
|
||||||
|
import org.elasticsearch.index.query.QueryParseContext;
|
||||||
import org.elasticsearch.script.Script;
|
import org.elasticsearch.script.Script;
|
||||||
import org.elasticsearch.search.aggregations.AggregationInitializationException;
|
import org.elasticsearch.search.aggregations.AggregationInitializationException;
|
||||||
import org.elasticsearch.search.aggregations.AggregatorBuilder;
|
import org.elasticsearch.search.aggregations.AggregatorBuilder;
|
||||||
|
@ -48,8 +52,8 @@ import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class TopHitsAggregatorBuilder extends AggregatorBuilder<TopHitsAggregatorBuilder> {
|
public class TopHitsAggregatorBuilder extends AggregatorBuilder<TopHitsAggregatorBuilder> {
|
||||||
|
public static final String NAME = InternalTopHits.TYPE.name();
|
||||||
static final TopHitsAggregatorBuilder PROTOTYPE = new TopHitsAggregatorBuilder("");
|
public static final ParseField AGGREGATION_NAME_FIELD = new ParseField(NAME);
|
||||||
|
|
||||||
private int from = 0;
|
private int from = 0;
|
||||||
private int size = 3;
|
private int size = 3;
|
||||||
|
@ -67,6 +71,96 @@ public class TopHitsAggregatorBuilder extends AggregatorBuilder<TopHitsAggregato
|
||||||
super(name, InternalTopHits.TYPE);
|
super(name, InternalTopHits.TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read from a stream.
|
||||||
|
*/
|
||||||
|
public TopHitsAggregatorBuilder(StreamInput in) throws IOException {
|
||||||
|
super(in, InternalTopHits.TYPE);
|
||||||
|
explain = in.readBoolean();
|
||||||
|
fetchSourceContext = in.readOptionalStreamable(FetchSourceContext::new);
|
||||||
|
if (in.readBoolean()) {
|
||||||
|
int size = in.readVInt();
|
||||||
|
fieldDataFields = new ArrayList<>(size);
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
fieldDataFields.add(in.readString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (in.readBoolean()) {
|
||||||
|
int size = in.readVInt();
|
||||||
|
fieldNames = new ArrayList<>(size);
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
fieldNames.add(in.readString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
from = in.readVInt();
|
||||||
|
highlightBuilder = in.readOptionalWriteable(HighlightBuilder::new);
|
||||||
|
if (in.readBoolean()) {
|
||||||
|
int size = in.readVInt();
|
||||||
|
scriptFields = new HashSet<>(size);
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
scriptFields.add(new ScriptField(in));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
size = in.readVInt();
|
||||||
|
if (in.readBoolean()) {
|
||||||
|
int size = in.readVInt();
|
||||||
|
sorts = new ArrayList<>();
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
sorts.add(in.readSortBuilder());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
trackScores = in.readBoolean();
|
||||||
|
version = in.readBoolean();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doWriteTo(StreamOutput out) throws IOException {
|
||||||
|
out.writeBoolean(explain);
|
||||||
|
out.writeOptionalStreamable(fetchSourceContext);
|
||||||
|
boolean hasFieldDataFields = fieldDataFields != null;
|
||||||
|
out.writeBoolean(hasFieldDataFields);
|
||||||
|
if (hasFieldDataFields) {
|
||||||
|
out.writeVInt(fieldDataFields.size());
|
||||||
|
for (String fieldName : fieldDataFields) {
|
||||||
|
out.writeString(fieldName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
boolean hasFieldNames = fieldNames != null;
|
||||||
|
out.writeBoolean(hasFieldNames);
|
||||||
|
if (hasFieldNames) {
|
||||||
|
out.writeVInt(fieldNames.size());
|
||||||
|
for (String fieldName : fieldNames) {
|
||||||
|
out.writeString(fieldName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out.writeVInt(from);
|
||||||
|
out.writeOptionalWriteable(highlightBuilder);
|
||||||
|
boolean hasScriptFields = scriptFields != null;
|
||||||
|
out.writeBoolean(hasScriptFields);
|
||||||
|
if (hasScriptFields) {
|
||||||
|
out.writeVInt(scriptFields.size());
|
||||||
|
for (ScriptField scriptField : scriptFields) {
|
||||||
|
scriptField.writeTo(out);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out.writeVInt(size);
|
||||||
|
boolean hasSorts = sorts != null;
|
||||||
|
out.writeBoolean(hasSorts);
|
||||||
|
if (hasSorts) {
|
||||||
|
out.writeVInt(sorts.size());
|
||||||
|
for (SortBuilder<?> sort : sorts) {
|
||||||
|
out.writeSortBuilder(sort);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out.writeBoolean(trackScores);
|
||||||
|
out.writeBoolean(version);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean usesNewStyleSerialization() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* From index to start the search from. Defaults to <tt>0</tt>.
|
* From index to start the search from. Defaults to <tt>0</tt>.
|
||||||
*/
|
*/
|
||||||
|
@ -518,92 +612,130 @@ public class TopHitsAggregatorBuilder extends AggregatorBuilder<TopHitsAggregato
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public static TopHitsAggregatorBuilder parse(String aggregationName, QueryParseContext context) throws IOException {
|
||||||
protected TopHitsAggregatorBuilder doReadFrom(String name, StreamInput in) throws IOException {
|
TopHitsAggregatorBuilder factory = new TopHitsAggregatorBuilder(aggregationName);
|
||||||
TopHitsAggregatorBuilder factory = new TopHitsAggregatorBuilder(name);
|
XContentParser.Token token;
|
||||||
factory.explain = in.readBoolean();
|
String currentFieldName = null;
|
||||||
factory.fetchSourceContext = in.readOptionalStreamable(FetchSourceContext::new);
|
XContentParser parser = context.parser();
|
||||||
if (in.readBoolean()) {
|
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||||
int size = in.readVInt();
|
if (token == XContentParser.Token.FIELD_NAME) {
|
||||||
List<String> fieldDataFields = new ArrayList<>(size);
|
currentFieldName = parser.currentName();
|
||||||
for (int i = 0; i < size; i++) {
|
} else if (token.isValue()) {
|
||||||
fieldDataFields.add(in.readString());
|
if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.FROM_FIELD)) {
|
||||||
|
factory.from(parser.intValue());
|
||||||
|
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.SIZE_FIELD)) {
|
||||||
|
factory.size(parser.intValue());
|
||||||
|
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.VERSION_FIELD)) {
|
||||||
|
factory.version(parser.booleanValue());
|
||||||
|
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.EXPLAIN_FIELD)) {
|
||||||
|
factory.explain(parser.booleanValue());
|
||||||
|
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.TRACK_SCORES_FIELD)) {
|
||||||
|
factory.trackScores(parser.booleanValue());
|
||||||
|
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder._SOURCE_FIELD)) {
|
||||||
|
factory.fetchSource(FetchSourceContext.parse(context));
|
||||||
|
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.FIELDS_FIELD)) {
|
||||||
|
List<String> fieldNames = new ArrayList<>();
|
||||||
|
fieldNames.add(parser.text());
|
||||||
|
factory.fields(fieldNames);
|
||||||
|
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.SORT_FIELD)) {
|
||||||
|
factory.sort(parser.text());
|
||||||
|
} else {
|
||||||
|
throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].",
|
||||||
|
parser.getTokenLocation());
|
||||||
}
|
}
|
||||||
factory.fieldDataFields = fieldDataFields;
|
} else if (token == XContentParser.Token.START_OBJECT) {
|
||||||
|
if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder._SOURCE_FIELD)) {
|
||||||
|
factory.fetchSource(FetchSourceContext.parse(context));
|
||||||
|
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.SCRIPT_FIELDS_FIELD)) {
|
||||||
|
List<ScriptField> scriptFields = new ArrayList<>();
|
||||||
|
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||||
|
String scriptFieldName = parser.currentName();
|
||||||
|
token = parser.nextToken();
|
||||||
|
if (token == XContentParser.Token.START_OBJECT) {
|
||||||
|
Script script = null;
|
||||||
|
boolean ignoreFailure = false;
|
||||||
|
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||||
|
if (token == XContentParser.Token.FIELD_NAME) {
|
||||||
|
currentFieldName = parser.currentName();
|
||||||
|
} else if (token.isValue()) {
|
||||||
|
if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.SCRIPT_FIELD)) {
|
||||||
|
script = Script.parse(parser, context.getParseFieldMatcher());
|
||||||
|
} else if (context.getParseFieldMatcher().match(currentFieldName,
|
||||||
|
SearchSourceBuilder.IGNORE_FAILURE_FIELD)) {
|
||||||
|
ignoreFailure = parser.booleanValue();
|
||||||
|
} else {
|
||||||
|
throw new ParsingException(parser.getTokenLocation(),
|
||||||
|
"Unknown key for a " + token + " in [" + currentFieldName + "].",
|
||||||
|
parser.getTokenLocation());
|
||||||
}
|
}
|
||||||
if (in.readBoolean()) {
|
} else if (token == XContentParser.Token.START_OBJECT) {
|
||||||
int size = in.readVInt();
|
if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.SCRIPT_FIELD)) {
|
||||||
List<String> fieldNames = new ArrayList<>(size);
|
script = Script.parse(parser, context.getParseFieldMatcher());
|
||||||
for (int i = 0; i < size; i++) {
|
} else {
|
||||||
fieldNames.add(in.readString());
|
throw new ParsingException(parser.getTokenLocation(),
|
||||||
|
"Unknown key for a " + token + " in [" + currentFieldName + "].",
|
||||||
|
parser.getTokenLocation());
|
||||||
}
|
}
|
||||||
factory.fieldNames = fieldNames;
|
} else {
|
||||||
|
throw new ParsingException(parser.getTokenLocation(),
|
||||||
|
"Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
|
||||||
}
|
}
|
||||||
factory.from = in.readVInt();
|
|
||||||
factory.highlightBuilder = in.readOptionalWriteable(HighlightBuilder::new);
|
|
||||||
if (in.readBoolean()) {
|
|
||||||
int size = in.readVInt();
|
|
||||||
Set<ScriptField> scriptFields = new HashSet<>(size);
|
|
||||||
for (int i = 0; i < size; i++) {
|
|
||||||
scriptFields.add(new ScriptField(in));
|
|
||||||
}
|
}
|
||||||
factory.scriptFields = scriptFields;
|
scriptFields.add(new ScriptField(scriptFieldName, script, ignoreFailure));
|
||||||
|
} else {
|
||||||
|
throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT
|
||||||
|
+ "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation());
|
||||||
}
|
}
|
||||||
factory.size = in.readVInt();
|
|
||||||
if (in.readBoolean()) {
|
|
||||||
int size = in.readVInt();
|
|
||||||
List<SortBuilder<?>> sorts = new ArrayList<>();
|
|
||||||
for (int i = 0; i < size; i++) {
|
|
||||||
sorts.add(in.readSortBuilder());
|
|
||||||
}
|
}
|
||||||
factory.sorts = sorts;
|
factory.scriptFields(scriptFields);
|
||||||
}
|
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.HIGHLIGHT_FIELD)) {
|
||||||
factory.trackScores = in.readBoolean();
|
factory.highlighter(HighlightBuilder.fromXContent(context));
|
||||||
factory.version = in.readBoolean();
|
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.SORT_FIELD)) {
|
||||||
return factory;
|
List<SortBuilder<?>> sorts = SortBuilder.fromXContent(context);
|
||||||
|
factory.sorts(sorts);
|
||||||
|
} else {
|
||||||
|
throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].",
|
||||||
|
parser.getTokenLocation());
|
||||||
}
|
}
|
||||||
|
} else if (token == XContentParser.Token.START_ARRAY) {
|
||||||
|
|
||||||
@Override
|
if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.FIELDS_FIELD)) {
|
||||||
protected void doWriteTo(StreamOutput out) throws IOException {
|
List<String> fieldNames = new ArrayList<>();
|
||||||
out.writeBoolean(explain);
|
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||||
out.writeOptionalStreamable(fetchSourceContext);
|
if (token == XContentParser.Token.VALUE_STRING) {
|
||||||
boolean hasFieldDataFields = fieldDataFields != null;
|
fieldNames.add(parser.text());
|
||||||
out.writeBoolean(hasFieldDataFields);
|
} else {
|
||||||
if (hasFieldDataFields) {
|
throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_STRING
|
||||||
out.writeVInt(fieldDataFields.size());
|
+ "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation());
|
||||||
for (String fieldName : fieldDataFields) {
|
|
||||||
out.writeString(fieldName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
boolean hasFieldNames = fieldNames != null;
|
factory.fields(fieldNames);
|
||||||
out.writeBoolean(hasFieldNames);
|
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.FIELDDATA_FIELDS_FIELD)) {
|
||||||
if (hasFieldNames) {
|
List<String> fieldDataFields = new ArrayList<>();
|
||||||
out.writeVInt(fieldNames.size());
|
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||||
for (String fieldName : fieldNames) {
|
if (token == XContentParser.Token.VALUE_STRING) {
|
||||||
out.writeString(fieldName);
|
fieldDataFields.add(parser.text());
|
||||||
|
} else {
|
||||||
|
throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_STRING
|
||||||
|
+ "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out.writeVInt(from);
|
factory.fieldDataFields(fieldDataFields);
|
||||||
out.writeOptionalWriteable(highlightBuilder);
|
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.SORT_FIELD)) {
|
||||||
boolean hasScriptFields = scriptFields != null;
|
List<SortBuilder<?>> sorts = SortBuilder.fromXContent(context);
|
||||||
out.writeBoolean(hasScriptFields);
|
factory.sorts(sorts);
|
||||||
if (hasScriptFields) {
|
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder._SOURCE_FIELD)) {
|
||||||
out.writeVInt(scriptFields.size());
|
factory.fetchSource(FetchSourceContext.parse(context));
|
||||||
for (ScriptField scriptField : scriptFields) {
|
} else {
|
||||||
scriptField.writeTo(out);
|
throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].",
|
||||||
|
parser.getTokenLocation());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].",
|
||||||
|
parser.getTokenLocation());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out.writeVInt(size);
|
return factory;
|
||||||
boolean hasSorts = sorts != null;
|
|
||||||
out.writeBoolean(hasSorts);
|
|
||||||
if (hasSorts) {
|
|
||||||
out.writeVInt(sorts.size());
|
|
||||||
for (SortBuilder<?> sort : sorts) {
|
|
||||||
out.writeSortBuilder(sort);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out.writeBoolean(trackScores);
|
|
||||||
out.writeBoolean(version);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -627,4 +759,9 @@ public class TopHitsAggregatorBuilder extends AggregatorBuilder<TopHitsAggregato
|
||||||
&& Objects.equals(trackScores, other.trackScores)
|
&& Objects.equals(trackScores, other.trackScores)
|
||||||
&& Objects.equals(version, other.version);
|
&& Objects.equals(version, other.version);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getWriteableName() {
|
||||||
|
return NAME;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,178 +0,0 @@
|
||||||
/*
|
|
||||||
* Licensed to Elasticsearch under one or more contributor
|
|
||||||
* license agreements. See the NOTICE file distributed with
|
|
||||||
* this work for additional information regarding copyright
|
|
||||||
* ownership. Elasticsearch licenses this file to you under
|
|
||||||
* the Apache License, Version 2.0 (the "License"); you may
|
|
||||||
* not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing,
|
|
||||||
* software distributed under the License is distributed on an
|
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
||||||
* KIND, either express or implied. See the License for the
|
|
||||||
* specific language governing permissions and limitations
|
|
||||||
* under the License.
|
|
||||||
*/
|
|
||||||
package org.elasticsearch.search.aggregations.metrics.tophits;
|
|
||||||
|
|
||||||
import org.elasticsearch.common.ParsingException;
|
|
||||||
import org.elasticsearch.common.xcontent.XContentParser;
|
|
||||||
import org.elasticsearch.index.query.QueryParseContext;
|
|
||||||
import org.elasticsearch.script.Script;
|
|
||||||
import org.elasticsearch.search.aggregations.Aggregator;
|
|
||||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
||||||
import org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField;
|
|
||||||
import org.elasticsearch.search.fetch.source.FetchSourceContext;
|
|
||||||
import org.elasticsearch.search.highlight.HighlightBuilder;
|
|
||||||
import org.elasticsearch.search.sort.SortBuilder;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class TopHitsParser implements Aggregator.Parser {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String type() {
|
|
||||||
return InternalTopHits.TYPE.name();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TopHitsAggregatorBuilder parse(String aggregationName, QueryParseContext context) throws IOException {
|
|
||||||
TopHitsAggregatorBuilder factory = new TopHitsAggregatorBuilder(aggregationName);
|
|
||||||
XContentParser.Token token;
|
|
||||||
String currentFieldName = null;
|
|
||||||
XContentParser parser = context.parser();
|
|
||||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
|
||||||
if (token == XContentParser.Token.FIELD_NAME) {
|
|
||||||
currentFieldName = parser.currentName();
|
|
||||||
} else if (token.isValue()) {
|
|
||||||
if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.FROM_FIELD)) {
|
|
||||||
factory.from(parser.intValue());
|
|
||||||
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.SIZE_FIELD)) {
|
|
||||||
factory.size(parser.intValue());
|
|
||||||
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.VERSION_FIELD)) {
|
|
||||||
factory.version(parser.booleanValue());
|
|
||||||
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.EXPLAIN_FIELD)) {
|
|
||||||
factory.explain(parser.booleanValue());
|
|
||||||
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.TRACK_SCORES_FIELD)) {
|
|
||||||
factory.trackScores(parser.booleanValue());
|
|
||||||
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder._SOURCE_FIELD)) {
|
|
||||||
factory.fetchSource(FetchSourceContext.parse(context));
|
|
||||||
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.FIELDS_FIELD)) {
|
|
||||||
List<String> fieldNames = new ArrayList<>();
|
|
||||||
fieldNames.add(parser.text());
|
|
||||||
factory.fields(fieldNames);
|
|
||||||
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.SORT_FIELD)) {
|
|
||||||
factory.sort(parser.text());
|
|
||||||
} else {
|
|
||||||
throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].",
|
|
||||||
parser.getTokenLocation());
|
|
||||||
}
|
|
||||||
} else if (token == XContentParser.Token.START_OBJECT) {
|
|
||||||
if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder._SOURCE_FIELD)) {
|
|
||||||
factory.fetchSource(FetchSourceContext.parse(context));
|
|
||||||
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.SCRIPT_FIELDS_FIELD)) {
|
|
||||||
List<ScriptField> scriptFields = new ArrayList<>();
|
|
||||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
|
||||||
String scriptFieldName = parser.currentName();
|
|
||||||
token = parser.nextToken();
|
|
||||||
if (token == XContentParser.Token.START_OBJECT) {
|
|
||||||
Script script = null;
|
|
||||||
boolean ignoreFailure = false;
|
|
||||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
|
||||||
if (token == XContentParser.Token.FIELD_NAME) {
|
|
||||||
currentFieldName = parser.currentName();
|
|
||||||
} else if (token.isValue()) {
|
|
||||||
if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.SCRIPT_FIELD)) {
|
|
||||||
script = Script.parse(parser, context.getParseFieldMatcher());
|
|
||||||
} else if (context.getParseFieldMatcher().match(currentFieldName,
|
|
||||||
SearchSourceBuilder.IGNORE_FAILURE_FIELD)) {
|
|
||||||
ignoreFailure = parser.booleanValue();
|
|
||||||
} else {
|
|
||||||
throw new ParsingException(parser.getTokenLocation(),
|
|
||||||
"Unknown key for a " + token + " in [" + currentFieldName + "].",
|
|
||||||
parser.getTokenLocation());
|
|
||||||
}
|
|
||||||
} else if (token == XContentParser.Token.START_OBJECT) {
|
|
||||||
if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.SCRIPT_FIELD)) {
|
|
||||||
script = Script.parse(parser, context.getParseFieldMatcher());
|
|
||||||
} else {
|
|
||||||
throw new ParsingException(parser.getTokenLocation(),
|
|
||||||
"Unknown key for a " + token + " in [" + currentFieldName + "].",
|
|
||||||
parser.getTokenLocation());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new ParsingException(parser.getTokenLocation(),
|
|
||||||
"Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
scriptFields.add(new ScriptField(scriptFieldName, script, ignoreFailure));
|
|
||||||
} else {
|
|
||||||
throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT
|
|
||||||
+ "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
factory.scriptFields(scriptFields);
|
|
||||||
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.HIGHLIGHT_FIELD)) {
|
|
||||||
factory.highlighter(HighlightBuilder.fromXContent(context));
|
|
||||||
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.SORT_FIELD)) {
|
|
||||||
List<SortBuilder<?>> sorts = SortBuilder.fromXContent(context);
|
|
||||||
factory.sorts(sorts);
|
|
||||||
} else {
|
|
||||||
throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].",
|
|
||||||
parser.getTokenLocation());
|
|
||||||
}
|
|
||||||
} else if (token == XContentParser.Token.START_ARRAY) {
|
|
||||||
|
|
||||||
if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.FIELDS_FIELD)) {
|
|
||||||
List<String> fieldNames = new ArrayList<>();
|
|
||||||
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
|
||||||
if (token == XContentParser.Token.VALUE_STRING) {
|
|
||||||
fieldNames.add(parser.text());
|
|
||||||
} else {
|
|
||||||
throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_STRING
|
|
||||||
+ "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
factory.fields(fieldNames);
|
|
||||||
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.FIELDDATA_FIELDS_FIELD)) {
|
|
||||||
List<String> fieldDataFields = new ArrayList<>();
|
|
||||||
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
|
||||||
if (token == XContentParser.Token.VALUE_STRING) {
|
|
||||||
fieldDataFields.add(parser.text());
|
|
||||||
} else {
|
|
||||||
throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_STRING
|
|
||||||
+ "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
factory.fieldDataFields(fieldDataFields);
|
|
||||||
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder.SORT_FIELD)) {
|
|
||||||
List<SortBuilder<?>> sorts = SortBuilder.fromXContent(context);
|
|
||||||
factory.sorts(sorts);
|
|
||||||
} else if (context.getParseFieldMatcher().match(currentFieldName, SearchSourceBuilder._SOURCE_FIELD)) {
|
|
||||||
factory.fetchSource(FetchSourceContext.parse(context));
|
|
||||||
} else {
|
|
||||||
throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].",
|
|
||||||
parser.getTokenLocation());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].",
|
|
||||||
parser.getTokenLocation());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return factory;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TopHitsAggregatorBuilder getFactoryPrototypes() {
|
|
||||||
return TopHitsAggregatorBuilder.PROTOTYPE;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue