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);
}
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);
}
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;
}
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;
}
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);
}
Aggregations