use of org.opensearch.search.aggregations.metrics.InternalTopHits in project OpenSearch by opensearch-project.
the class TermsAggregatorTests method assertNestedTopHitsScore.
private void assertNestedTopHitsScore(InternalMultiBucketAggregation<?, ?> terms, boolean withScore) {
assertThat(terms.getBuckets().size(), equalTo(9));
int ptr = 9;
for (MultiBucketsAggregation.Bucket bucket : terms.getBuckets()) {
InternalTopHits topHits = bucket.getAggregations().get("top_hits");
assertThat(topHits.getHits().getTotalHits().value, equalTo((long) ptr));
assertEquals(TotalHits.Relation.EQUAL_TO, topHits.getHits().getTotalHits().relation);
if (withScore) {
assertThat(topHits.getHits().getMaxScore(), equalTo(1f));
} else {
assertThat(topHits.getHits().getMaxScore(), equalTo(Float.NaN));
}
--ptr;
}
}
use of org.opensearch.search.aggregations.metrics.InternalTopHits in project OpenSearch by opensearch-project.
the class TermsAggregatorTests method testGlobalAggregationWithScore.
public void testGlobalAggregationWithScore() throws IOException {
try (Directory directory = newDirectory()) {
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
Document document = new Document();
document.add(new SortedDocValuesField("keyword", new BytesRef("a")));
indexWriter.addDocument(document);
document = new Document();
document.add(new SortedDocValuesField("keyword", new BytesRef("c")));
indexWriter.addDocument(document);
document = new Document();
document.add(new SortedDocValuesField("keyword", new BytesRef("e")));
indexWriter.addDocument(document);
try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) {
IndexSearcher indexSearcher = newIndexSearcher(indexReader);
String executionHint = randomFrom(TermsAggregatorFactory.ExecutionMode.values()).toString();
Aggregator.SubAggCollectionMode collectionMode = randomFrom(Aggregator.SubAggCollectionMode.values());
GlobalAggregationBuilder globalBuilder = new GlobalAggregationBuilder("global").subAggregation(new TermsAggregationBuilder("terms").userValueTypeHint(ValueType.STRING).executionHint(executionHint).collectMode(collectionMode).field("keyword").order(BucketOrder.key(true)).subAggregation(new TermsAggregationBuilder("sub_terms").userValueTypeHint(ValueType.STRING).executionHint(executionHint).collectMode(collectionMode).field("keyword").order(BucketOrder.key(true)).subAggregation(new TopHitsAggregationBuilder("top_hits").storedField("_none_"))));
MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("keyword");
InternalGlobal result = searchAndReduce(indexSearcher, new MatchAllDocsQuery(), globalBuilder, fieldType);
InternalMultiBucketAggregation<?, ?> terms = result.getAggregations().get("terms");
assertThat(terms.getBuckets().size(), equalTo(3));
for (MultiBucketsAggregation.Bucket bucket : terms.getBuckets()) {
InternalMultiBucketAggregation<?, ?> subTerms = bucket.getAggregations().get("sub_terms");
assertThat(subTerms.getBuckets().size(), equalTo(1));
MultiBucketsAggregation.Bucket subBucket = subTerms.getBuckets().get(0);
InternalTopHits topHits = subBucket.getAggregations().get("top_hits");
assertThat(topHits.getHits().getHits().length, equalTo(1));
for (SearchHit hit : topHits.getHits()) {
assertThat(hit.getScore(), greaterThan(0f));
}
}
}
}
}
}
use of org.opensearch.search.aggregations.metrics.InternalTopHits in project OpenSearch by opensearch-project.
the class RareTermsAggregatorTests method testWithNestedScoringAggregations.
public void testWithNestedScoringAggregations() 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 (boolean withScore : new boolean[] { true, false }) {
NestedAggregationBuilder nested = new NestedAggregationBuilder("nested", "nested_object").subAggregation(new RareTermsAggregationBuilder("terms").field("nested_value").maxDocCount(2).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))) {
if (withScore) {
IllegalStateException e = expectThrows(IllegalStateException.class, () -> searchAndReduce(newIndexSearcher(indexReader), // match root document only
new DocValuesFieldExistsQuery(PRIMARY_TERM_NAME), nested, fieldType));
assertThat(e.getMessage(), equalTo("RareTerms agg [terms] is the child of the nested agg [nested], " + "and also has a scoring child agg [top_hits]. This combination is not supported because it requires " + "executing in [depth_first] mode, which the RareTerms agg cannot do."));
} else {
InternalNested result = searchAndReduce(newIndexSearcher(indexReader), // match root document only
new DocValuesFieldExistsQuery(PRIMARY_TERM_NAME), nested, fieldType);
InternalMultiBucketAggregation<?, ?> terms = result.getAggregations().get("terms");
assertThat(terms.getBuckets().size(), equalTo(2));
long counter = 1;
for (MultiBucketsAggregation.Bucket bucket : terms.getBuckets()) {
InternalTopHits topHits = bucket.getAggregations().get("top_hits");
TotalHits hits = topHits.getHits().getTotalHits();
assertNotNull(hits);
assertThat(hits.value, equalTo(counter));
assertThat(topHits.getHits().getMaxScore(), equalTo(Float.NaN));
counter += 1;
}
}
}
}
}
}
}
use of org.opensearch.search.aggregations.metrics.InternalTopHits in project OpenSearch by opensearch-project.
the class RareTermsAggregatorTests method testGlobalAggregationWithScore.
public void testGlobalAggregationWithScore() throws IOException {
try (Directory directory = newDirectory()) {
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
Document document = new Document();
document.add(new SortedDocValuesField("keyword", new BytesRef("a")));
indexWriter.addDocument(document);
document = new Document();
document.add(new SortedDocValuesField("keyword", new BytesRef("c")));
indexWriter.addDocument(document);
document = new Document();
document.add(new SortedDocValuesField("keyword", new BytesRef("e")));
indexWriter.addDocument(document);
try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) {
IndexSearcher indexSearcher = newIndexSearcher(indexReader);
GlobalAggregationBuilder globalBuilder = new GlobalAggregationBuilder("global").subAggregation(new RareTermsAggregationBuilder("terms").field("keyword").subAggregation(new RareTermsAggregationBuilder("sub_terms").field("keyword").subAggregation(new TopHitsAggregationBuilder("top_hits").storedField("_none_"))));
MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("keyword");
InternalGlobal result = searchAndReduce(indexSearcher, new MatchAllDocsQuery(), globalBuilder, fieldType);
InternalMultiBucketAggregation<?, ?> terms = result.getAggregations().get("terms");
assertThat(terms.getBuckets().size(), equalTo(3));
for (MultiBucketsAggregation.Bucket bucket : terms.getBuckets()) {
InternalMultiBucketAggregation<?, ?> subTerms = bucket.getAggregations().get("sub_terms");
assertThat(subTerms.getBuckets().size(), equalTo(1));
MultiBucketsAggregation.Bucket subBucket = subTerms.getBuckets().get(0);
InternalTopHits topHits = subBucket.getAggregations().get("top_hits");
assertThat(topHits.getHits().getHits().length, equalTo(1));
for (SearchHit hit : topHits.getHits()) {
assertThat(hit.getScore(), greaterThan(0f));
}
}
}
}
}
}
Aggregations