Search in sources :

Example 1 with TopHitsAggregationBuilder

use of org.elasticsearch.search.aggregations.metrics.TopHitsAggregationBuilder in project graylog2-server by Graylog2.

the class ESLatestHandler method doCreateAggregation.

@Nonnull
@Override
public Optional<AggregationBuilder> doCreateAggregation(String name, Pivot pivot, Latest latestSpec, ESPivot searchTypeHandler, ESGeneratedQueryContext queryContext) {
    final TopHitsAggregationBuilder latest = AggregationBuilders.topHits(name).size(1).sort(SortBuilders.fieldSort("timestamp").order(SortOrder.DESC));
    record(queryContext, pivot, latestSpec, name, TopHits.class);
    return Optional.of(latest);
}
Also used : TopHitsAggregationBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.metrics.TopHitsAggregationBuilder) Nonnull(javax.annotation.Nonnull)

Example 2 with TopHitsAggregationBuilder

use of org.elasticsearch.search.aggregations.metrics.TopHitsAggregationBuilder in project snow-owl by b2ihealthcare.

the class EsDocumentSearcher method toEsAggregation.

private org.elasticsearch.search.aggregations.AggregationBuilder toEsAggregation(DocumentMapping mapping, AggregationBuilder<?> aggregation, boolean fetchSource) {
    final TermsAggregationBuilder termsAgg = AggregationBuilders.terms(aggregation.getName()).minDocCount(aggregation.getMinBucketSize()).size(Integer.MAX_VALUE);
    boolean isFieldAgg = !Strings.isNullOrEmpty(aggregation.getGroupByField());
    boolean isScriptAgg = !Strings.isNullOrEmpty(aggregation.getGroupByScript());
    if (isFieldAgg) {
        checkArgument(!isScriptAgg, "Specify either field or script parameter, not both");
        termsAgg.field(aggregation.getGroupByField());
    } else if (isScriptAgg) {
        termsAgg.script(aggregation.toEsScript(mapping));
    } else {
        throw new IllegalArgumentException("Specify either field or script parameter");
    }
    boolean isNested = !Strings.isNullOrEmpty(aggregation.getPath());
    // add top hits agg to get the top N items for each bucket
    if (aggregation.getBucketHitsLimit() > 0) {
        TopHitsAggregationBuilder topHitsAgg = AggregationBuilders.topHits(topHitsAggName(aggregation)).size(aggregation.getBucketHitsLimit());
        if (fetchSource) {
            topHitsAgg.storedFields(STORED_FIELDS_ID_ONLY).fetchSource(true);
        } else {
            topHitsAgg.storedFields(STORED_FIELDS_NONE).fetchSource(false);
            aggregation.getFields().forEach(field -> topHitsAgg.docValueField(field));
        }
        if (isNested) {
            termsAgg.subAggregation(AggregationBuilders.reverseNested(reverseNestedAggName(aggregation)).subAggregation(topHitsAgg));
        } else {
            termsAgg.subAggregation(topHitsAgg);
        }
    }
    if (isNested) {
        return AggregationBuilders.nested(nestedAggName(aggregation), aggregation.getPath()).subAggregation(termsAgg);
    }
    return termsAgg;
}
Also used : TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) TopHitsAggregationBuilder(org.elasticsearch.search.aggregations.metrics.TopHitsAggregationBuilder)

Aggregations

Nonnull (javax.annotation.Nonnull)1 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)1 TopHitsAggregationBuilder (org.elasticsearch.search.aggregations.metrics.TopHitsAggregationBuilder)1 TopHitsAggregationBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.metrics.TopHitsAggregationBuilder)1