Search in sources :

Example 41 with Aggregation

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation in project elasticsearch by elastic.

the class TopHitsAggregatorTests method testInsideTerms.

/**
     * Tests {@code top_hits} inside of {@code terms}. While not strictly a unit test this is a fairly common way to run {@code top_hits}
     * and serves as a good example of running {@code top_hits} inside of another aggregation.
     */
public void testInsideTerms() throws Exception {
    Aggregation result;
    if (randomBoolean()) {
        result = testCase(new MatchAllDocsQuery(), terms("term").field("string").subAggregation(topHits("top").sort("string", SortOrder.DESC)));
    } else {
        Query query = new QueryParser("string", new KeywordAnalyzer()).parse("d^1000 c^100 b^10 a^1");
        result = testCase(query, terms("term").field("string").subAggregation(topHits("top")));
    }
    Terms terms = (Terms) result;
    // The "a" bucket
    TopHits hits = (TopHits) terms.getBucketByKey("a").getAggregations().get("top");
    SearchHits searchHits = (hits).getHits();
    assertEquals(2L, searchHits.getTotalHits());
    assertEquals("2", searchHits.getAt(0).getId());
    assertEquals("1", searchHits.getAt(1).getId());
    // The "b" bucket
    searchHits = ((TopHits) terms.getBucketByKey("b").getAggregations().get("top")).getHits();
    assertEquals(2L, searchHits.getTotalHits());
    assertEquals("3", searchHits.getAt(0).getId());
    assertEquals("1", searchHits.getAt(1).getId());
    // The "c" bucket
    searchHits = ((TopHits) terms.getBucketByKey("c").getAggregations().get("top")).getHits();
    assertEquals(1L, searchHits.getTotalHits());
    assertEquals("2", searchHits.getAt(0).getId());
    // The "d" bucket
    searchHits = ((TopHits) terms.getBucketByKey("d").getAggregations().get("top")).getHits();
    assertEquals(1L, searchHits.getTotalHits());
    assertEquals("3", searchHits.getAt(0).getId());
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchHits(org.elasticsearch.search.SearchHits) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 42 with Aggregation

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation in project elasticsearch by elastic.

the class TopHitsAggregatorTests method testCase.

private Aggregation testCase(Query query, AggregationBuilder builder) throws IOException {
    Directory directory = newDirectory();
    RandomIndexWriter iw = new RandomIndexWriter(random(), directory);
    iw.addDocument(document("1", "a", "b"));
    iw.addDocument(document("2", "c", "a"));
    iw.addDocument(document("3", "b", "d"));
    iw.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    // We do not use LuceneTestCase.newSearcher because we need a DirectoryReader for "testInsideTerms"
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    Aggregation result = searchAndReduce(indexSearcher, query, builder, STRING_FIELD_TYPE);
    indexReader.close();
    directory.close();
    return result;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Aggregation(org.elasticsearch.search.aggregations.Aggregation) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 43 with Aggregation

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation in project zipkin by openzipkin.

the class BucketKeysTest method namesAggBucketKeysAreSpanNames.

@Test
public void namesAggBucketKeysAreSpanNames() {
    when(response.getAggregations()).thenReturn(aggregations);
    Terms terms = mock(Terms.class);
    Collection<Aggregation> list = asList(terms);
    when(aggregations.iterator()).thenReturn(list.iterator());
    Terms.Bucket bucket = mock(Terms.Bucket.class);
    when(terms.getBuckets()).thenReturn(asList(bucket));
    when(bucket.getKeyAsString()).thenReturn("service");
    assertThat(BucketKeys.INSTANCE.apply(response)).containsExactly("service");
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Test(org.junit.Test)

Example 44 with Aggregation

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation in project alien4cloud by alien4cloud.

the class ESGenericSearchDAO method parseAggregations.

/**
 * Parse aggregations and set facets to the given non null FacetedSearchResult instance.
 *
 * @param searchResponse The search response that contains aggregation results.
 * @param facetedSearchResult The instance in which to set facets.
 * @param aggregationQueryManager If not null the data of the FacetedSearchResult will be processed from an aggregation based on the given manager.
 */
private void parseAggregations(SearchResponse searchResponse, FacetedSearchResult facetedSearchResult, IAggregationQueryManager aggregationQueryManager) {
    if (searchResponse.getAggregations() == null) {
        return;
    }
    List<Aggregation> internalAggregationsList = searchResponse.getAggregations().asList();
    if (internalAggregationsList.size() == 0) {
        return;
    }
    Map<String, FacetedSearchFacet[]> facetMap = Maps.newHashMap();
    for (Aggregation aggregation : internalAggregationsList) {
        if (aggregationQueryManager != null && aggregation.getName().equals(aggregationQueryManager.getQueryAggregation().getName())) {
            aggregationQueryManager.setData(getJsonMapper(), getClassFromTypeFunc(), facetedSearchResult, aggregation);
        } else if (aggregation instanceof InternalTerms) {
            InternalTerms internalTerms = (InternalTerms) aggregation;
            List<FacetedSearchFacet> facets = new ArrayList<>();
            for (int i = 0; i < internalTerms.getBuckets().size(); i++) {
                Terms.Bucket bucket = internalTerms.getBuckets().get(i);
                facets.add(new FacetedSearchFacet(bucket.getKey(), bucket.getDocCount()));
            }
            // Find the missing aggregation
            internalAggregationsList.stream().filter(missingAggregation -> missingAggregation instanceof InternalMissing && missingAggregation.getName().equals("missing_" + internalTerms.getName())).findAny().ifPresent(missingAggregation -> {
                InternalMissing internalMissingAggregation = (InternalMissing) missingAggregation;
                if (internalMissingAggregation.getDocCount() > 0) {
                    facets.add(new FacetedSearchFacet(null, internalMissingAggregation.getDocCount()));
                }
            });
            facetMap.put(internalTerms.getName(), facets.toArray(new FacetedSearchFacet[facets.size()]));
        } else {
            log.debug("Aggregation is not a facet aggregation (terms) ignore. Name: {} ,Type: {}", aggregation.getName(), aggregation.getClass().getName());
        }
    }
    facetedSearchResult.setFacets(facetMap);
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) SortBuilders(org.elasticsearch.search.sort.SortBuilders) Array(java.lang.reflect.Array) CountRequestBuilder(org.elasticsearch.action.count.CountRequestBuilder) SneakyThrows(lombok.SneakyThrows) MappingBuilder(org.elasticsearch.mapping.MappingBuilder) TopHitsBuilder(org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder) HashMap(java.util.HashMap) MapUtil(alien4cloud.utils.MapUtil) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) FacetedSearchFacet(alien4cloud.dao.model.FacetedSearchFacet) Function(java.util.function.Function) ISearchBuilderAdapter(org.elasticsearch.mapping.ISearchBuilderAdapter) ScoreFunctionBuilders(org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) Aggregation(org.elasticsearch.search.aggregations.Aggregation) SortBuilder(org.elasticsearch.search.sort.SortBuilder) InternalMissing(org.elasticsearch.search.aggregations.bucket.missing.InternalMissing) SearchHit(org.elasticsearch.search.SearchHit) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchType(org.elasticsearch.action.search.SearchType) ElasticSearchClient(org.elasticsearch.mapping.ElasticSearchClient) ElasticSearchUtil(alien4cloud.utils.ElasticSearchUtil) FilterBuilder(org.elasticsearch.index.query.FilterBuilder) GetMultipleDataResult(alien4cloud.dao.model.GetMultipleDataResult) InternalTerms(org.elasticsearch.search.aggregations.bucket.terms.InternalTerms) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Resource(javax.annotation.Resource) IOException(java.io.IOException) JsonUtil(alien4cloud.rest.utils.JsonUtil) Field(java.lang.reflect.Field) Maps(com.google.common.collect.Maps) FilterValuesStrategy(org.elasticsearch.mapping.FilterValuesStrategy) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) FacetedSearchResult(alien4cloud.dao.model.FacetedSearchResult) CombineFunction(org.elasticsearch.common.lucene.search.function.CombineFunction) QueryHelper(org.elasticsearch.mapping.QueryHelper) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) QueryBuilderAdapter(org.elasticsearch.mapping.QueryBuilderAdapter) SortOrder(org.elasticsearch.search.sort.SortOrder) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) SourceFetchContext(org.elasticsearch.mapping.SourceFetchContext) InternalTerms(org.elasticsearch.search.aggregations.bucket.terms.InternalTerms) InternalMissing(org.elasticsearch.search.aggregations.bucket.missing.InternalMissing) FacetedSearchFacet(alien4cloud.dao.model.FacetedSearchFacet) ArrayList(java.util.ArrayList) List(java.util.List)

Example 45 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)

Aggregations

Aggregation (org.elasticsearch.search.aggregations.Aggregation)47 SearchResponse (org.elasticsearch.action.search.SearchResponse)23 HashMap (java.util.HashMap)21 List (java.util.List)20 ArrayList (java.util.ArrayList)17 MultiBucketsAggregation (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)16 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)16 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)14 Script (org.elasticsearch.script.Script)13 ScriptedMetric (org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetric)13 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)10 Map (java.util.Map)9 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