use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation in project metron by apache.
the class ElasticsearchDao method getFacetCounts.
public Map<String, Map<String, Long>> getFacetCounts(List<String> fields, Aggregations aggregations, Map<String, FieldType> commonColumnMetadata) {
Map<String, Map<String, Long>> fieldCounts = new HashMap<>();
for (String field : fields) {
Map<String, Long> valueCounts = new HashMap<>();
Aggregation aggregation = aggregations.get(getFacetAggregationName(field));
if (aggregation instanceof Terms) {
Terms terms = (Terms) aggregation;
terms.getBuckets().stream().forEach(bucket -> valueCounts.put(formatKey(bucket.getKey(), commonColumnMetadata.get(field)), bucket.getDocCount()));
}
fieldCounts.put(field, valueCounts);
}
return fieldCounts;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation in project tutorials by eugenp.
the class ElasticSearchQueryIntegrationTest method givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually.
@Test
public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() {
final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags").order(Terms.Order.aggregation("_count", false));
final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation).execute().actionGet();
final Map<String, Aggregation> results = response.getAggregations().asMap();
final StringTerms topTags = (StringTerms) results.get("top_tags");
final List<String> keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString).collect(toList());
assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation in project metasfresh-webui-api by metasfresh.
the class KPIDataLoader method loadData.
private void loadData(final KPIDataResult.Builder data, final TimeRange timeRange) {
logger.trace("Loading data for {}", timeRange);
//
// Create query evaluation context
final Evaluatee evalCtx = Evaluatees.mapBuilder().put("MainFromMillis", data.getRange().getFromMillis()).put("MainToMillis", data.getRange().getToMillis()).put("FromMillis", timeRange.getFromMillis()).put("ToMillis", timeRange.getToMillis()).build().andComposeWith(Evaluatees.ofCtx(Env.getCtx()));
//
// Resolve esQuery's variables
final IStringExpression esQuery = kpi.getESQuery();
final String esQueryParsed = esQuery.evaluate(evalCtx, OnVariableNotFound.Preserve);
//
// Execute the query
final SearchResponse response;
try {
logger.trace("Executing: \n{}", esQueryParsed);
response = elasticsearchClient.prepareSearch(kpi.getESSearchIndex()).setTypes(kpi.getESSearchTypes()).setSource(esQueryParsed).get();
logger.trace("Got response: \n{}", response);
} catch (final NoNodeAvailableException e) {
// elastic search transport error => nothing to do about it
throw e;
} catch (final Exception e) {
throw new AdempiereException("Failed executing query for " + this + ": " + e.getLocalizedMessage() + "\nQuery: " + esQueryParsed, e);
}
// Fetch data
try {
final List<Aggregation> aggregations = response.getAggregations().asList();
for (final Aggregation agg : aggregations) {
if (agg instanceof MultiBucketsAggregation) {
final String aggName = agg.getName();
final MultiBucketsAggregation multiBucketsAggregation = (MultiBucketsAggregation) agg;
for (final Bucket bucket : multiBucketsAggregation.getBuckets()) {
final Object key = dataSetValueKeyExtractor.apply(bucket, timeRange);
for (final KPIField field : kpi.getFields()) {
final Object value = field.getBucketValueExtractor().extractValue(aggName, bucket);
final Object jsonValue = formatValue(field, value);
if (jsonValue == null) {
continue;
}
final String fieldName = fieldNameExtractor.apply(field, timeRange);
data.putValue(aggName, key, fieldName, jsonValue);
}
//
// Make sure the groupByField's value is present in our dataSet value.
// If not exist, we can use the key as it's value.
final KPIField groupByField = kpi.getGroupByFieldOrNull();
if (groupByField != null) {
data.putValueIfAbsent(aggName, key, groupByField.getFieldName(), key);
}
}
} else if (agg instanceof NumericMetricsAggregation.SingleValue) {
final NumericMetricsAggregation.SingleValue singleValueAggregation = (NumericMetricsAggregation.SingleValue) agg;
// N/A
final String key = "NO_KEY";
for (final KPIField field : kpi.getFields()) {
final Object value;
if ("value".equals(field.getESPathAsString())) {
value = singleValueAggregation.value();
} else {
throw new IllegalStateException("Only ES path ending with 'value' allowed for field: " + field);
}
final Object jsonValue = field.convertValueToJson(value);
data.putValue(agg.getName(), key, field.getFieldName(), jsonValue);
}
} else {
new AdempiereException("Aggregation type not supported: " + agg.getClass()).throwIfDeveloperModeOrLogWarningElse(logger);
}
}
} catch (final Exception e) {
throw new AdempiereException(e.getLocalizedMessage() + "\n KPI: " + this + "\n Query: " + esQueryParsed + "\n Response: " + response, e);
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method searchResponse.
private SearchResponse<T> searchResponse(org.elasticsearch.action.search.SearchResponse response) {
SearchHit[] hits = response.getHits().getHits();
List<T> items = new ArrayList<>(hits.length);
for (SearchHit hit : hits) {
items.add(reader.fromJSON(BytesReference.toBytes(hit.getSourceRef())));
}
Aggregations aggregationResponse = response.getAggregations();
Map<String, Aggregation> aggregations = aggregationResponse == null ? Maps.newHashMap() : aggregationResponse.asMap();
return new SearchResponse<>(items, response.getHits().getTotalHits(), aggregations);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation in project vertigo by KleeGroup.
the class ESFacetedQueryResultBuilder method createCluster.
private Map<FacetValue, DtList<I>> createCluster(final Map<String, I> dtcIndex, final Map<I, Map<DtField, String>> resultHighlights) {
final Map<FacetValue, DtList<I>> resultCluster = new LinkedHashMap<>();
final FacetDefinition facetDefinition = searchQuery.getClusteringFacetDefinition();
final Aggregation facetAggregation = queryResponse.getAggregations().get(facetDefinition.getName());
if (facetDefinition.isRangeFacet()) {
// Cas des facettes par 'range'
final MultiBucketsAggregation multiBuckets = (MultiBucketsAggregation) facetAggregation;
for (final FacetValue facetRange : facetDefinition.getFacetRanges()) {
final Bucket value = getBucketByKey(multiBuckets, facetRange.getListFilter().getFilterValue());
populateCluster(value, facetRange, resultCluster, dtcIndex, resultHighlights);
}
} else {
// Cas des facettes par 'term'
final MultiBucketsAggregation multiBuckets = (MultiBucketsAggregation) facetAggregation;
FacetValue facetValue;
for (final Bucket bucket : multiBuckets.getBuckets()) {
final String term = bucket.getKeyAsString();
final String query = facetDefinition.getDtField().getName() + ":\"" + term + "\"";
final MessageText label = MessageText.of(term);
facetValue = new FacetValue(term, ListFilter.of(query), label);
populateCluster(bucket, facetValue, resultCluster, dtcIndex, resultHighlights);
}
}
return resultCluster;
}
Aggregations