use of org.elasticsearch.search.aggregations.Aggregation in project tutorials by eugenp.
the class ElasticSearchQueryIntegrationTest method givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately.
@Test
public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() {
final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("title");
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).sorted().collect(toList());
assertEquals(asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys);
}
use of 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;
}
use of org.elasticsearch.search.aggregations.Aggregation in project vertigo by KleeGroup.
the class ESFacetedQueryResultBuilder method createFacetList.
private static List<Facet> createFacetList(final SearchQuery searchQuery, final SearchResponse queryResponse) {
final List<Facet> facets = new ArrayList<>();
if (searchQuery.getFacetedQuery().isPresent() && queryResponse.getAggregations() != null) {
final FacetedQueryDefinition queryDefinition = searchQuery.getFacetedQuery().get().getDefinition();
for (final FacetDefinition facetDefinition : queryDefinition.getFacetDefinitions()) {
final Aggregation aggregation = queryResponse.getAggregations().get(facetDefinition.getName());
if (aggregation != null) {
final Facet facet = createFacet(facetDefinition, (MultiBucketsAggregation) aggregation);
facets.add(facet);
}
}
}
return facets;
}
use of org.elasticsearch.search.aggregations.Aggregation 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.elasticsearch.search.aggregations.Aggregation in project vertexium by visallo.
the class ElasticsearchGraphQueryIterable method reduceRangeResults.
private static RangeResult reduceRangeResults(ElasticsearchSearchQueryBase query, List<Aggregation> aggs) {
Map<Object, List<MultiBucketsAggregation.Bucket>> bucketsByKey = new HashMap<>();
for (Aggregation agg : aggs) {
if (agg instanceof Range) {
Range r = (Range) agg;
for (Range.Bucket b : r.getBuckets()) {
List<MultiBucketsAggregation.Bucket> l = bucketsByKey.computeIfAbsent(b.getKey(), k -> new ArrayList<>());
l.add(b);
}
} else {
throw new VertexiumException("Aggregation is not a range: " + agg.getClass().getName());
}
}
return new MultiBucketsAggregationReducer<RangeResult, RangeBucket>() {
@Override
protected RangeBucket createBucket(Object key, long count, Map<String, AggregationResult> nestedResults, List<MultiBucketsAggregation.Bucket> buckets) {
return new RangeBucket(key, count, nestedResults);
}
@Override
protected RangeResult bucketsToResults(List<RangeBucket> buckets) {
return new RangeResult(buckets);
}
}.reduce(query, bucketsByKey);
}
Aggregations