Search in sources :

Example 61 with TermsAggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder in project pancm_project by xuwujing.

the class EsAggregationSearchTest method maxGroupSearch.

private static void maxGroupSearch() throws IOException {
    String agg = "t_class_max";
    String buk = "t_grade";
    // terms 就是分组统计 根据student的grade成绩进行分组并创建一个新的聚合
    TermsAggregationBuilder aggregation = AggregationBuilders.terms(agg).field("class");
    aggregation.subAggregation(AggregationBuilders.max(buk).field("grade"));
    logger.info("根据班级求最大分数:");
    agg(aggregation, agg, buk);
}
Also used : TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)

Example 62 with TermsAggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder in project pancm_project by xuwujing.

the class EsAggregationSearchTest method avgGroupSearch.

/**
 * @Author pancm
 * @Description 平均聚合查询测试用例
 * @Date  2019/4/1
 * @Param []
 * @return void
 */
private static void avgGroupSearch() throws IOException {
    String agg = "t_class_avg";
    String buk = "t_grade";
    // terms 就是分组统计 根据student的grade成绩进行分组并创建一个新的聚合
    TermsAggregationBuilder aggregation = AggregationBuilders.terms(agg).field("class");
    aggregation.subAggregation(AggregationBuilders.avg(buk).field("grade"));
    logger.info("根据班级求平均分数:");
    agg(aggregation, agg, buk);
}
Also used : TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)

Example 63 with TermsAggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder in project vertexium by visallo.

the class Elasticsearch5SearchIndex method getVertexPropertyCountByValue.

@Override
public Map<Object, Long> getVertexPropertyCountByValue(Graph graph, String propertyName, Authorizations authorizations) {
    TermQueryBuilder elementTypeFilterBuilder = new TermQueryBuilder(ELEMENT_TYPE_FIELD_NAME, ElasticsearchDocumentType.VERTEX.getKey());
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery()).filter(elementTypeFilterBuilder);
    SearchRequestBuilder q = getClient().prepareSearch(getIndexNamesAsArray(graph)).setQuery(queryBuilder).setSize(0);
    for (String p : getAllMatchingPropertyNames(graph, propertyName, authorizations)) {
        String countAggName = "count-" + p;
        PropertyDefinition propertyDefinition = getPropertyDefinition(graph, p);
        p = replaceFieldnameDots(p);
        if (propertyDefinition != null && propertyDefinition.getTextIndexHints().contains(TextIndexHint.EXACT_MATCH)) {
            p = p + EXACT_MATCH_PROPERTY_NAME_SUFFIX;
        }
        TermsAggregationBuilder countAgg = AggregationBuilders.terms(countAggName).field(p).size(500000);
        q = q.addAggregation(countAgg);
    }
    if (ElasticsearchSearchQueryBase.QUERY_LOGGER.isTraceEnabled()) {
        ElasticsearchSearchQueryBase.QUERY_LOGGER.trace("query: %s", q);
    }
    SearchResponse response = checkForFailures(getClient().search(q.request()).actionGet());
    Map<Object, Long> results = new HashMap<>();
    for (Aggregation agg : response.getAggregations().asList()) {
        Terms propertyCountResults = (Terms) agg;
        for (Terms.Bucket propertyCountResult : propertyCountResults.getBuckets()) {
            String mapKey = ((String) propertyCountResult.getKey()).toLowerCase();
            Long previousValue = results.get(mapKey);
            if (previousValue == null) {
                previousValue = 0L;
            }
            results.put(mapKey, previousValue + propertyCountResult.getDocCount());
        }
    }
    return results;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) Aggregation(org.elasticsearch.search.aggregations.Aggregation) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 64 with TermsAggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder in project vertexium by visallo.

the class ElasticsearchSearchQueryBase method getElasticsearchTermsAggregations.

protected List<AggregationBuilder> getElasticsearchTermsAggregations(TermsAggregation agg) {
    List<AggregationBuilder> termsAggs = new ArrayList<>();
    String fieldName = agg.getPropertyName();
    if (Edge.LABEL_PROPERTY_NAME.equals(fieldName) || ExtendedDataRow.TABLE_NAME.equals(fieldName)) {
        TermsAggregationBuilder termsAgg = AggregationBuilders.terms(createAggregationName(agg.getAggregationName(), "0"));
        termsAgg.field(fieldName);
        if (agg.getSize() != null) {
            termsAgg.size(agg.getSize());
        }
        termsAgg.shardSize(termAggregationShardSize);
        termsAggs.add(termsAgg);
    } else {
        PropertyDefinition propertyDefinition = getPropertyDefinition(fieldName);
        for (String propertyName : getPropertyNames(fieldName)) {
            boolean exactMatchProperty = isExactMatchPropertyDefinition(propertyDefinition);
            String propertyNameWithSuffix;
            if (exactMatchProperty) {
                propertyNameWithSuffix = propertyName + Elasticsearch5SearchIndex.EXACT_MATCH_PROPERTY_NAME_SUFFIX;
            } else {
                propertyNameWithSuffix = propertyName;
            }
            String visibilityHash = getSearchIndex().getPropertyVisibilityHashFromPropertyName(propertyNameWithSuffix);
            String aggregationName = createAggregationName(agg.getAggregationName(), visibilityHash);
            TermsAggregationBuilder termsAgg = AggregationBuilders.terms(aggregationName);
            termsAgg.field(propertyNameWithSuffix);
            if (agg.getSize() != null) {
                termsAgg.size(agg.getSize());
            }
            termsAgg.shardSize(termAggregationShardSize);
            if (exactMatchProperty && propertyDefinition.getTextIndexHints().contains(TextIndexHint.FULL_TEXT)) {
                termsAgg.subAggregation(AggregationBuilders.topHits(TOP_HITS_AGGREGATION_NAME).fetchSource(new String[] { propertyName }, new String[0]).size(1));
            }
            for (AggregationBuilder subAgg : getElasticsearchAggregations(agg.getNestedAggregations())) {
                termsAgg.subAggregation(subAgg);
            }
            termsAggs.add(termsAgg);
        }
    }
    return termsAggs;
}
Also used : TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) RangeAggregationBuilder(org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder) GeoGridAggregationBuilder(org.elasticsearch.search.aggregations.bucket.geogrid.GeoGridAggregationBuilder) AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) DateRangeAggregationBuilder(org.elasticsearch.search.aggregations.bucket.range.date.DateRangeAggregationBuilder) PercentilesAggregationBuilder(org.elasticsearch.search.aggregations.metrics.percentiles.PercentilesAggregationBuilder) ExtendedStatsAggregationBuilder(org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStatsAggregationBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) HistogramAggregationBuilder(org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder) DateHistogramAggregationBuilder(org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder)

Example 65 with TermsAggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder in project graylog2-server by Graylog2.

the class ESValuesHandler method doCreateAggregation.

@Nonnull
@Override
public Optional<AggregationBuilder> doCreateAggregation(String name, Pivot pivot, Values valuesSpec, ESPivot searchTypeHandler, ESGeneratedQueryContext esGeneratedQueryContext, Query query) {
    final List<BucketOrder> ordering = orderListForPivot(pivot, valuesSpec, esGeneratedQueryContext);
    final TermsAggregationBuilder builder = AggregationBuilders.terms(name).minDocCount(1).field(valuesSpec.field()).order(ordering.isEmpty() ? Collections.singletonList(BucketOrder.count(false)) : ordering).size(valuesSpec.limit());
    record(esGeneratedQueryContext, pivot, valuesSpec, name, TermsAggregation.class);
    return Optional.of(builder);
}
Also used : TermsAggregationBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) BucketOrder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.BucketOrder) Nonnull(javax.annotation.Nonnull)

Aggregations

TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)64 SignificantTermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.significant.SignificantTermsAggregationBuilder)20 SearchResponse (org.elasticsearch.action.search.SearchResponse)14 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)14 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)13 Test (org.junit.Test)11 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)8 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)8 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)8 LinkedList (java.util.LinkedList)7 AggregationBuilder (org.elasticsearch.search.aggregations.AggregationBuilder)6 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude)6 Arrays (java.util.Arrays)4 Map (java.util.Map)4 SearchRequest (org.apache.metron.indexing.dao.search.SearchRequest)4 AbstractAggregationBuilder (org.elasticsearch.search.aggregations.AbstractAggregationBuilder)4 AggregationBuilders (org.elasticsearch.search.aggregations.AggregationBuilders)4 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)4 Nullable (javax.annotation.Nullable)3 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)3