Search in sources :

Example 51 with Aggregation

use of org.graylog.shaded.elasticsearch7.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);
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) TermsBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

Example 52 with Aggregation

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;
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) FacetValue(io.vertigo.dynamo.collections.model.FacetValue) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) MessageText(io.vertigo.core.locale.MessageText) FacetDefinition(io.vertigo.dynamo.collections.metamodel.FacetDefinition) DtList(io.vertigo.dynamo.domain.model.DtList) LinkedHashMap(java.util.LinkedHashMap)

Example 53 with Aggregation

use of org.graylog.shaded.elasticsearch7.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;
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) ArrayList(java.util.ArrayList) FacetDefinition(io.vertigo.dynamo.collections.metamodel.FacetDefinition) FacetedQueryDefinition(io.vertigo.dynamo.collections.metamodel.FacetedQueryDefinition) Facet(io.vertigo.dynamo.collections.model.Facet)

Example 54 with Aggregation

use of org.graylog.shaded.elasticsearch7.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;
}
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 55 with Aggregation

use of org.graylog.shaded.elasticsearch7.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);
}
Also used : Range(org.elasticsearch.search.aggregations.bucket.range.Range) InternalRange(org.elasticsearch.search.aggregations.bucket.range.InternalRange) VertexiumException(org.vertexium.VertexiumException) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)

Aggregations

Aggregation (org.elasticsearch.search.aggregations.Aggregation)53 HashMap (java.util.HashMap)25 SearchResponse (org.elasticsearch.action.search.SearchResponse)24 List (java.util.List)20 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)19 ArrayList (java.util.ArrayList)17 MultiBucketsAggregation (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)17 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)16 Script (org.elasticsearch.script.Script)13 ScriptedMetric (org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetric)13 Map (java.util.Map)11 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)10 Test (org.junit.Test)8 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)7 BucketSpec (org.graylog.plugins.views.search.searchtypes.pivot.BucketSpec)6 DateTime (org.joda.time.DateTime)6 Aggregation (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation)5 VertexiumException (org.vertexium.VertexiumException)5 FacetDefinition (io.vertigo.dynamo.collections.metamodel.FacetDefinition)4 IOException (java.io.IOException)4