use of org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder in project fess by codelibs.
the class EsAbstractConditionAggregation method regTopHitsA.
protected TopHitsAggregationBuilder regTopHitsA(String name) {
TopHitsAggregationBuilder builder = AggregationBuilders.topHits(name);
regA(builder);
return builder;
}
use of org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder in project OpenSearch by opensearch-project.
the class CompositeAggregatorTests method testWithKeywordAndTopHits.
public void testWithKeywordAndTopHits() throws Exception {
final List<Map<String, List<Object>>> dataset = new ArrayList<>();
dataset.addAll(Arrays.asList(createDocument("keyword", "a"), createDocument("keyword", "c"), createDocument("keyword", "a"), createDocument("keyword", "d"), createDocument("keyword", "c")));
testSearchCase(Arrays.asList(new MatchAllDocsQuery(), new DocValuesFieldExistsQuery("keyword")), dataset, () -> {
TermsValuesSourceBuilder terms = new TermsValuesSourceBuilder("keyword").field("keyword");
return new CompositeAggregationBuilder("name", Collections.singletonList(terms)).subAggregation(new TopHitsAggregationBuilder("top_hits").storedField("_none_"));
}, (result) -> {
assertEquals(3, result.getBuckets().size());
assertEquals("{keyword=a}", result.getBuckets().get(0).getKeyAsString());
assertEquals(2L, result.getBuckets().get(0).getDocCount());
TopHits topHits = result.getBuckets().get(0).getAggregations().get("top_hits");
assertNotNull(topHits);
assertEquals(topHits.getHits().getHits().length, 2);
assertEquals(topHits.getHits().getTotalHits().value, 2L);
assertEquals("{keyword=c}", result.getBuckets().get(1).getKeyAsString());
assertEquals(2L, result.getBuckets().get(1).getDocCount());
topHits = result.getBuckets().get(1).getAggregations().get("top_hits");
assertNotNull(topHits);
assertEquals(topHits.getHits().getHits().length, 2);
assertEquals(topHits.getHits().getTotalHits().value, 2L);
assertEquals("{keyword=d}", result.getBuckets().get(2).getKeyAsString());
assertEquals(1L, result.getBuckets().get(2).getDocCount());
topHits = result.getBuckets().get(2).getAggregations().get("top_hits");
assertNotNull(topHits);
assertEquals(topHits.getHits().getHits().length, 1);
assertEquals(topHits.getHits().getTotalHits().value, 1L);
});
testSearchCase(Arrays.asList(new MatchAllDocsQuery(), new DocValuesFieldExistsQuery("keyword")), dataset, () -> {
TermsValuesSourceBuilder terms = new TermsValuesSourceBuilder("keyword").field("keyword");
return new CompositeAggregationBuilder("name", Collections.singletonList(terms)).aggregateAfter(Collections.singletonMap("keyword", "a")).subAggregation(new TopHitsAggregationBuilder("top_hits").storedField("_none_"));
}, (result) -> {
assertEquals(2, result.getBuckets().size());
assertEquals("{keyword=c}", result.getBuckets().get(0).getKeyAsString());
assertEquals(2L, result.getBuckets().get(0).getDocCount());
TopHits topHits = result.getBuckets().get(0).getAggregations().get("top_hits");
assertNotNull(topHits);
assertEquals(topHits.getHits().getHits().length, 2);
assertEquals(topHits.getHits().getTotalHits().value, 2L);
assertEquals("{keyword=d}", result.getBuckets().get(1).getKeyAsString());
assertEquals(1L, result.getBuckets().get(1).getDocCount());
topHits = result.getBuckets().get(1).getAggregations().get("top_hits");
assertNotNull(topHits);
assertEquals(topHits.getHits().getHits().length, 1);
assertEquals(topHits.getHits().getTotalHits().value, 1L);
});
}
use of org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder in project OpenSearch by opensearch-project.
the class TermsAggregatorTests method testWithNestedAggregations.
public void testWithNestedAggregations() throws IOException {
try (Directory directory = newDirectory()) {
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
for (int i = 0; i < 10; i++) {
int[] nestedValues = new int[i];
for (int j = 0; j < i; j++) {
nestedValues[j] = j;
}
indexWriter.addDocuments(generateDocsWithNested(Integer.toString(i), i, nestedValues));
}
indexWriter.commit();
for (Aggregator.SubAggCollectionMode mode : Aggregator.SubAggCollectionMode.values()) {
for (boolean withScore : new boolean[] { true, false }) {
NestedAggregationBuilder nested = new NestedAggregationBuilder("nested", "nested_object").subAggregation(new TermsAggregationBuilder("terms").userValueTypeHint(ValueType.LONG).field("nested_value").collectMode(mode).order(BucketOrder.key(true)).subAggregation(new TopHitsAggregationBuilder("top_hits").sort(withScore ? new ScoreSortBuilder() : new FieldSortBuilder("_doc")).storedField("_none_")));
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("nested_value", NumberFieldMapper.NumberType.LONG);
try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
{
InternalNested result = searchAndReduce(newSearcher(indexReader, false, true), // match root document only
new DocValuesFieldExistsQuery(PRIMARY_TERM_NAME), nested, fieldType);
InternalMultiBucketAggregation<?, ?> terms = result.getAggregations().get("terms");
assertNestedTopHitsScore(terms, withScore);
}
{
FilterAggregationBuilder filter = new FilterAggregationBuilder("filter", new MatchAllQueryBuilder()).subAggregation(nested);
InternalFilter result = searchAndReduce(newSearcher(indexReader, false, true), // match root document only
new DocValuesFieldExistsQuery(PRIMARY_TERM_NAME), filter, fieldType);
InternalNested nestedResult = result.getAggregations().get("nested");
InternalMultiBucketAggregation<?, ?> terms = nestedResult.getAggregations().get("terms");
assertNestedTopHitsScore(terms, withScore);
}
}
}
}
}
}
}
use of org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder in project fess by codelibs.
the class EsAbstractConditionAggregation method regTopHitsA.
protected TopHitsAggregationBuilder regTopHitsA(String name) {
TopHitsAggregationBuilder builder = AggregationBuilders.topHits(name);
regA(builder);
return builder;
}
use of org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder in project fess by codelibs.
the class EsAbstractConditionAggregation method regTopHitsA.
protected TopHitsAggregationBuilder regTopHitsA(String name) {
TopHitsAggregationBuilder builder = AggregationBuilders.topHits(name);
regA(builder);
return builder;
}
Aggregations