Add parsing method for Top Hits aggregation (#24717)

Related to #23331
This commit is contained in:
Tanguy Leroux 2017-05-18 09:37:46 +02:00 committed by GitHub
parent 25fceb8c0f
commit 055875392e
4 changed files with 108 additions and 2 deletions

View File

@ -0,0 +1,63 @@
/*
* 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.ParseField;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.ParsedAggregation;
import java.io.IOException;
public class ParsedTopHits extends ParsedAggregation implements TopHits {
private SearchHits searchHits;
@Override
public String getType() {
return TopHitsAggregationBuilder.NAME;
}
@Override
public SearchHits getHits() {
return searchHits;
}
@Override
protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
return searchHits.toXContent(builder, params);
}
private static ObjectParser<ParsedTopHits, Void> PARSER =
new ObjectParser<>(ParsedTopHits.class.getSimpleName(), true, ParsedTopHits::new);
static {
declareAggregationFields(PARSER);
PARSER.declareObject((topHit, searchHits) -> topHit.searchHits = searchHits, (parser, context) -> SearchHits.fromXContent(parser),
new ParseField(SearchHits.Fields.HITS));
}
public static ParsedTopHits fromXContent(XContentParser parser, String name) throws IOException {
ParsedTopHits aggregation = PARSER.parse(parser, null);
aggregation.setName(name);
return aggregation;
}
}

View File

@ -61,6 +61,7 @@ import org.elasticsearch.search.aggregations.metrics.percentiles.tdigest.Interna
import org.elasticsearch.search.aggregations.metrics.percentiles.tdigest.InternalTDigestPercentilesTests;
import org.elasticsearch.search.aggregations.metrics.scripted.InternalScriptedMetricTests;
import org.elasticsearch.search.aggregations.metrics.sum.InternalSumTests;
import org.elasticsearch.search.aggregations.metrics.tophits.InternalTopHitsTests;
import org.elasticsearch.search.aggregations.metrics.valuecount.InternalValueCountTests;
import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValueTests;
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValueTests;
@ -134,6 +135,7 @@ public class AggregationsTests extends ESTestCase {
aggsTests.add(new SignificantStringTermsTests());
aggsTests.add(new InternalScriptedMetricTests());
aggsTests.add(new InternalBinaryRangeTests());
aggsTests.add(new InternalTopHitsTests());
return Collections.unmodifiableList(aggsTests);
}

View File

@ -33,9 +33,11 @@ import org.elasticsearch.common.text.Text;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHitField;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.ParsedAggregation;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
import org.elasticsearch.test.InternalAggregationTestCase;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
@ -54,11 +56,18 @@ public class InternalTopHitsTests extends InternalAggregationTestCase<InternalTo
* Should the test instances look like they are sorted by some fields (true) or sorted by score (false). Set here because these need
* to be the same across the entirety of {@link #testReduceRandom()}.
*/
private final boolean testInstancesLookSortedByField = randomBoolean();
private boolean testInstancesLookSortedByField;
/**
* Fields shared by all instances created by {@link #createTestInstance(String, List, Map)}.
*/
private final SortField[] testInstancesSortFields = testInstancesLookSortedByField ? randomSortFields() : new SortField[0];
private SortField[] testInstancesSortFields;
@Override
public void setUp() throws Exception {
super.setUp();
testInstancesLookSortedByField = randomBoolean();
testInstancesSortFields = testInstancesLookSortedByField ? randomSortFields() : new SortField[0];
}
@Override
protected InternalTopHits createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
@ -103,6 +112,35 @@ public class InternalTopHitsTests extends InternalAggregationTestCase<InternalTo
return new InternalTopHits(name, from, requestedSize, topDocs, searchHits, pipelineAggregators, metaData);
}
@Override
protected void assertFromXContent(InternalTopHits aggregation, ParsedAggregation parsedAggregation) throws IOException {
final SearchHits expectedSearchHits = aggregation.getHits();
assertTrue(parsedAggregation instanceof ParsedTopHits);
ParsedTopHits parsed = (ParsedTopHits) parsedAggregation;
final SearchHits actualSearchHits = parsed.getHits();
assertEquals(expectedSearchHits.getTotalHits(), actualSearchHits.getTotalHits());
assertEquals(expectedSearchHits.getMaxScore(), actualSearchHits.getMaxScore(), 0.0f);
List<SearchHit> expectedHits = Arrays.asList(expectedSearchHits.getHits());
List<SearchHit> actualHits = Arrays.asList(actualSearchHits.getHits());
assertEquals(expectedHits.size(), actualHits.size());
for (int i = 0; i < expectedHits.size(); i++) {
SearchHit expected = expectedHits.get(i);
SearchHit actual = actualHits.get(i);
assertEquals(expected.getIndex(), actual.getIndex());
assertEquals(expected.getType(), actual.getType());
assertEquals(expected.getId(), actual.getId());
assertEquals(expected.getVersion(), actual.getVersion());
assertEquals(expected.getScore(), actual.getScore(), 0.0f);
assertEquals(expected.getFields(), actual.getFields());
assertEquals(expected.getSourceAsMap(), actual.getSourceAsMap());
}
}
private Object randomOfType(SortField.Type type) {
switch (type) {
case CUSTOM:

View File

@ -106,6 +106,8 @@ import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStat
import org.elasticsearch.search.aggregations.metrics.stats.extended.ParsedExtendedStats;
import org.elasticsearch.search.aggregations.metrics.sum.ParsedSum;
import org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.tophits.ParsedTopHits;
import org.elasticsearch.search.aggregations.metrics.tophits.TopHitsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.valuecount.ParsedValueCount;
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCountAggregationBuilder;
import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue;
@ -187,6 +189,7 @@ public abstract class InternalAggregationTestCase<T extends InternalAggregation>
map.put(SignificantStringTerms.NAME, (p, c) -> ParsedSignificantStringTerms.fromXContent(p, (String) c));
map.put(ScriptedMetricAggregationBuilder.NAME, (p, c) -> ParsedScriptedMetric.fromXContent(p, (String) c));
map.put(IpRangeAggregationBuilder.NAME, (p, c) -> ParsedBinaryRange.fromXContent(p, (String) c));
map.put(TopHitsAggregationBuilder.NAME, (p, c) -> ParsedTopHits.fromXContent(p, (String) c));
namedXContents = map.entrySet().stream()
.map(entry -> new NamedXContentRegistry.Entry(Aggregation.class, new ParseField(entry.getKey()), entry.getValue()))