use of org.opensearch.search.aggregations.bucket.global.InternalGlobal 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.bucket.global.InternalGlobal in project OpenSearch by opensearch-project.
the class MinAggregatorTests method testGetProperty.
public void testGetProperty() throws IOException {
GlobalAggregationBuilder globalBuilder = new GlobalAggregationBuilder("global").subAggregation(new MinAggregationBuilder("min").field("number"));
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER);
testCase(globalBuilder, new MatchAllDocsQuery(), iw -> {
iw.addDocument(singleton(new NumericDocValuesField("number", 7)));
iw.addDocument(singleton(new NumericDocValuesField("number", 1)));
}, (Consumer<InternalGlobal>) global -> {
assertEquals(2, global.getDocCount());
assertTrue(AggregationInspectionHelper.hasValue(global));
assertNotNull(global.getAggregations().asMap().get("min"));
InternalMin min = (InternalMin) global.getAggregations().asMap().get("min");
assertEquals(1.0, min.getValue(), 0);
assertThat(global.getProperty("min"), equalTo(min));
assertThat(global.getProperty("min.value"), equalTo(1.0));
assertThat(min.getProperty("value"), equalTo(1.0));
}, fieldType);
}
use of org.opensearch.search.aggregations.bucket.global.InternalGlobal in project OpenSearch by opensearch-project.
the class GlobalAggregatorTests method testCase.
// Note that `global`'s fancy support for ignoring the query comes from special code in AggregationPhase. We don't test that here.
private void testCase(CheckedConsumer<RandomIndexWriter, IOException> buildIndex, BiConsumer<InternalGlobal, InternalMin> verify) throws IOException {
Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
buildIndex.accept(indexWriter);
indexWriter.close();
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
GlobalAggregationBuilder aggregationBuilder = new GlobalAggregationBuilder("_name");
aggregationBuilder.subAggregation(new MinAggregationBuilder("in_global").field("number"));
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG);
GlobalAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
aggregator.preCollection();
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
aggregator.postCollection();
InternalGlobal result = (InternalGlobal) aggregator.buildTopLevel();
verify.accept(result, (InternalMin) result.getAggregations().asMap().get("in_global"));
indexReader.close();
directory.close();
}
use of org.opensearch.search.aggregations.bucket.global.InternalGlobal 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