Removes TopHitsBuilder in place of TopHitsAggregator.Factory

This commit is contained in:
Colin Goodheart-Smithe 2016-01-11 15:47:22 +00:00
parent 8c4f0ea705
commit 0e866dd4f4
4 changed files with 31 additions and 235 deletions

View File

@ -74,7 +74,7 @@ import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStat
import org.elasticsearch.search.aggregations.metrics.sum.Sum;
import org.elasticsearch.search.aggregations.metrics.sum.SumAggregator;
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
import org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder;
import org.elasticsearch.search.aggregations.metrics.tophits.TopHitsAggregator;
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount;
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCountAggregator;
@ -278,8 +278,8 @@ public class AggregationBuilders {
/**
* Create a new {@link TopHits} aggregation with the given name.
*/
public static TopHitsBuilder topHits(String name) {
return new TopHitsBuilder(name);
public static TopHitsAggregator.Factory topHits(String name) {
return new TopHitsAggregator.Factory(name);
}
/**

View File

@ -1,204 +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.Nullable;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.script.Script;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import java.io.IOException;
/**
* Builder for the {@link TopHits} aggregation.
*/
public class TopHitsBuilder extends AbstractAggregationBuilder {
private SearchSourceBuilder sourceBuilder;
/**
* Sole constructor.
*/
public TopHitsBuilder(String name) {
super(name, InternalTopHits.TYPE.name());
}
/**
* The index to start to return hits from. Defaults to <tt>0</tt>.
*/
public TopHitsBuilder setFrom(int from) {
sourceBuilder().from(from);
return this;
}
/**
* The number of search hits to return. Defaults to <tt>10</tt>.
*/
public TopHitsBuilder setSize(int size) {
sourceBuilder().size(size);
return this;
}
/**
* Applies when sorting, and controls if scores will be tracked as well. Defaults to
* <tt>false</tt>.
*/
public TopHitsBuilder setTrackScores(boolean trackScores) {
sourceBuilder().trackScores(trackScores);
return this;
}
/**
* Should each {@link org.elasticsearch.search.SearchHit} be returned with an
* explanation of the hit (ranking).
*/
public TopHitsBuilder setExplain(boolean explain) {
sourceBuilder().explain(explain);
return this;
}
/**
* Should each {@link org.elasticsearch.search.SearchHit} be returned with its
* version.
*/
public TopHitsBuilder setVersion(boolean version) {
sourceBuilder().version(version);
return this;
}
/**
* Adds a field to loaded and returned.
*/
public TopHitsBuilder addField(String field) {
sourceBuilder().field(field);
return this;
}
/**
* Sets no fields to be loaded, resulting in only id and type to be returned per field.
*/
public TopHitsBuilder setNoFields() {
sourceBuilder().noFields();
return this;
}
/**
* Indicates whether the response should contain the stored _source for every hit
*/
public TopHitsBuilder setFetchSource(boolean fetch) {
sourceBuilder().fetchSource(fetch);
return this;
}
/**
* Indicate that _source should be returned with every hit, with an "include" and/or "exclude" set which can include simple wildcard
* elements.
*
* @param include An optional include (optionally wildcarded) pattern to filter the returned _source
* @param exclude An optional exclude (optionally wildcarded) pattern to filter the returned _source
*/
public TopHitsBuilder setFetchSource(@Nullable String include, @Nullable String exclude) {
sourceBuilder().fetchSource(include, exclude);
return this;
}
/**
* Indicate that _source should be returned with every hit, with an "include" and/or "exclude" set which can include simple wildcard
* elements.
*
* @param includes An optional list of include (optionally wildcarded) pattern to filter the returned _source
* @param excludes An optional list of exclude (optionally wildcarded) pattern to filter the returned _source
*/
public TopHitsBuilder setFetchSource(@Nullable String[] includes, @Nullable String[] excludes) {
sourceBuilder().fetchSource(includes, excludes);
return this;
}
/**
* Adds a field data based field to load and return. The field does not have to be stored,
* but its recommended to use non analyzed or numeric fields.
*
* @param name The field to get from the field data cache
*/
public TopHitsBuilder addFieldDataField(String name) {
sourceBuilder().fieldDataField(name);
return this;
}
/**
* Adds a script based field to load and return. The field does not have to be stored,
* but its recommended to use non analyzed or numeric fields.
*
* @param name The name that will represent this value in the return hit
* @param script The script to use
*/
public TopHitsBuilder addScriptField(String name, Script script) {
sourceBuilder().scriptField(name, script);
return this;
}
/**
* Adds a sort against the given field name and the sort ordering.
*
* @param field The name of the field
* @param order The sort ordering
*/
public TopHitsBuilder addSort(String field, SortOrder order) {
sourceBuilder().sort(field, order);
return this;
}
/**
* Adds a generic sort builder.
*
* @see org.elasticsearch.search.sort.SortBuilders
*/
public TopHitsBuilder addSort(SortBuilder sort) {
sourceBuilder().sort(sort);
return this;
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(getName()).field(type);
sourceBuilder().toXContent(builder, params);
return builder.endObject();
}
private SearchSourceBuilder sourceBuilder() {
if (sourceBuilder == null) {
sourceBuilder = new SearchSourceBuilder();
}
return sourceBuilder;
}
public HighlightBuilder highlighter() {
return sourceBuilder().highlighter();
}
public TopHitsBuilder highlighter(HighlightBuilder highlightBuilder) {
sourceBuilder().highlighter(highlightBuilder);
return this;
}
}

View File

@ -175,7 +175,7 @@ public class ChildrenIT extends ESIntegTestCase {
.setQuery(matchQuery("randomized", false))
.addAggregation(
terms("category").field("category").size(0).subAggregation(
children("to_comment").childType("comment").subAggregation(topHits("top_comments").addSort("_uid", SortOrder.ASC))
children("to_comment").childType("comment").subAggregation(topHits("top_comments").sort("_uid", SortOrder.ASC))
)
).get();
assertSearchResponse(searchResponse);

View File

@ -256,7 +256,7 @@ public class TopHitsIT extends ESIntegTestCase {
.executionHint(randomExecutionHint())
.field(TERMS_AGGS_FIELD)
.subAggregation(
topHits("hits").addSort(SortBuilders.fieldSort(SORT_FIELD).order(SortOrder.DESC))
topHits("hits").sort(SortBuilders.fieldSort(SORT_FIELD).order(SortOrder.DESC))
)
)
.get();
@ -352,7 +352,7 @@ public class TopHitsIT extends ESIntegTestCase {
.executionHint(randomExecutionHint())
.collectMode(SubAggCollectionMode.BREADTH_FIRST)
.field(TERMS_AGGS_FIELD)
.subAggregation(topHits("hits").setSize(3))
.subAggregation(topHits("hits").size(3))
).get();
assertSearchResponse(response);
@ -403,9 +403,9 @@ public class TopHitsIT extends ESIntegTestCase {
.executionHint(randomExecutionHint())
.field(TERMS_AGGS_FIELD)
.subAggregation(
topHits("hits").addSort(SortBuilders.fieldSort(SORT_FIELD).order(SortOrder.DESC))
.setFrom(from)
.setSize(size)
topHits("hits").sort(SortBuilders.fieldSort(SORT_FIELD).order(SortOrder.DESC))
.from(from)
.size(size)
)
)
.get();
@ -447,7 +447,7 @@ public class TopHitsIT extends ESIntegTestCase {
.field(TERMS_AGGS_FIELD)
.order(Terms.Order.aggregation("max_sort", false))
.subAggregation(
topHits("hits").addSort(SortBuilders.fieldSort(SORT_FIELD).order(SortOrder.DESC)).setTrackScores(true)
topHits("hits").sort(SortBuilders.fieldSort(SORT_FIELD).order(SortOrder.DESC)).trackScores(true)
)
.subAggregation(
max("max_sort").field(SORT_FIELD)
@ -487,7 +487,7 @@ public class TopHitsIT extends ESIntegTestCase {
.setQuery(matchQuery("text", "term rare"))
.addAggregation(
terms("terms").executionHint(randomExecutionHint()).field("group")
.order(Terms.Order.aggregation("max_score", false)).subAggregation(topHits("hits").setSize(1))
.order(Terms.Order.aggregation("max_score", false)).subAggregation(topHits("hits").size(1))
.subAggregation(max("max_score").field("value"))).get();
assertSearchResponse(response);
@ -529,14 +529,14 @@ public class TopHitsIT extends ESIntegTestCase {
.executionHint(randomExecutionHint())
.field(TERMS_AGGS_FIELD)
.subAggregation(
topHits("hits").setSize(1)
topHits("hits").size(1)
.highlighter(new HighlightBuilder().field("text"))
.setExplain(true)
.addField("text")
.addFieldDataField("field1")
.addScriptField("script", new Script("5", ScriptService.ScriptType.INLINE, MockScriptEngine.NAME, Collections.emptyMap()))
.setFetchSource("text", null)
.setVersion(true)
.explain(true)
.field("text")
.fieldDataField("field1")
.scriptField("script", new Script("5", ScriptService.ScriptType.INLINE, MockScriptEngine.NAME, Collections.emptyMap()))
.fetchSource("text", null)
.version(true)
)
)
.get();
@ -586,7 +586,7 @@ public class TopHitsIT extends ESIntegTestCase {
.executionHint(randomExecutionHint())
.field(TERMS_AGGS_FIELD)
.subAggregation(
topHits("hits").addSort(SortBuilders.fieldSort("xyz").order(SortOrder.DESC))
topHits("hits").sort(SortBuilders.fieldSort("xyz").order(SortOrder.DESC))
)
).get();
fail();
@ -650,9 +650,9 @@ public class TopHitsIT extends ESIntegTestCase {
.field("group")
.subAggregation(
topHits("hits")
.setTrackScores(trackScore)
.setSize(1)
.addSort("_id", SortOrder.DESC)
.trackScores(trackScore)
.size(1)
.sort("_id", SortOrder.DESC)
)
)
.get();
@ -696,7 +696,7 @@ public class TopHitsIT extends ESIntegTestCase {
terms("users")
.field("comments.user")
.subAggregation(
topHits("top-comments").addSort("comments.date", SortOrder.ASC)
topHits("top-comments").sort("comments.date", SortOrder.ASC)
)
)
)
@ -746,10 +746,10 @@ public class TopHitsIT extends ESIntegTestCase {
.subAggregation(
nested("to-reviewers").path("comments.reviewers").subAggregation(
// Also need to sort on _doc because there are two reviewers with the same name
topHits("top-reviewers").addSort("comments.reviewers.name", SortOrder.ASC).addSort("_doc", SortOrder.DESC).setSize(7)
topHits("top-reviewers").sort("comments.reviewers.name", SortOrder.ASC).sort("_doc", SortOrder.DESC).size(7)
)
)
.subAggregation(topHits("top-comments").addSort("comments.date", SortOrder.DESC).setSize(4))
.subAggregation(topHits("top-comments").sort("comments.date", SortOrder.DESC).size(4))
).get();
assertNoFailures(searchResponse);
@ -849,10 +849,10 @@ public class TopHitsIT extends ESIntegTestCase {
.setQuery(nestedQuery("comments", matchQuery("comments.message", "comment").queryName("test")))
.addAggregation(
nested("to-comments").path("comments").subAggregation(
topHits("top-comments").setSize(1).highlighter(new HighlightBuilder().field(hlField)).setExplain(true)
.addFieldDataField("comments.user")
.addScriptField("script", new Script("5", ScriptService.ScriptType.INLINE, MockScriptEngine.NAME, Collections.emptyMap())).setFetchSource("message", null)
.setVersion(true).addSort("comments.date", SortOrder.ASC))).get();
topHits("top-comments").size(1).highlighter(new HighlightBuilder().field(hlField)).explain(true)
.fieldDataField("comments.user")
.scriptField("script", new Script("5", ScriptService.ScriptType.INLINE, MockScriptEngine.NAME, Collections.emptyMap())).fetchSource("message", null)
.version(true).sort("comments.date", SortOrder.ASC))).get();
assertHitCount(searchResponse, 2);
Nested nested = searchResponse.getAggregations().get("to-comments");
assertThat(nested.getDocCount(), equalTo(4l));
@ -901,7 +901,7 @@ public class TopHitsIT extends ESIntegTestCase {
.path("comments")
.subAggregation(topHits("comments")
.highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("comments.message").highlightQuery(matchQuery("comments.message", "text"))))
.addSort("comments.id", SortOrder.ASC))
.sort("comments.id", SortOrder.ASC))
)
)
.get();
@ -938,7 +938,7 @@ public class TopHitsIT extends ESIntegTestCase {
.executionHint(randomExecutionHint())
.field(TERMS_AGGS_FIELD)
.subAggregation(
topHits("hits").setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1).addSort(SortBuilders.fieldSort(SORT_FIELD).order(SortOrder.DESC))
topHits("hits").size(ArrayUtil.MAX_ARRAY_LENGTH - 1).sort(SortBuilders.fieldSort(SORT_FIELD).order(SortOrder.DESC))
)
)
.get();