Search in sources :

Example 11 with StringTerms

use of org.elasticsearch.search.aggregations.bucket.terms.StringTerms in project herd by FINRAOS.

the class ElasticSearchHelperTest method testGetTagTagIndexSearchResponseDto.

@Test
public void testGetTagTagIndexSearchResponseDto() {
    SearchResponse searchResponse = mock(SearchResponse.class);
    Terms terms = mock(Terms.class);
    Aggregations aggregations = mock(Aggregations.class);
    Terms.Bucket tagTypeCodeEntry = mock(Terms.Bucket.class);
    List<Terms.Bucket> tagTypeCodeEntryList = Collections.singletonList(tagTypeCodeEntry);
    when(searchResponse.getAggregations()).thenReturn(aggregations);
    when(aggregations.get(TAG_TYPE_FACET_AGGS)).thenReturn(terms);
    when(terms.getBuckets()).thenReturn(tagTypeCodeEntryList);
    when(tagTypeCodeEntry.getKeyAsString()).thenReturn(TAG_TYPE_CODE);
    when(tagTypeCodeEntry.getAggregations()).thenReturn(aggregations);
    Terms tagTypeDisplayNameAggs = mock(Terms.class);
    Terms.Bucket tagTypeDisplayNameEntry = mock(Terms.Bucket.class);
    List<Terms.Bucket> tagTypeDisplayNameEntryList = Collections.singletonList(tagTypeDisplayNameEntry);
    when(aggregations.get(TAGTYPE_NAME_AGGREGATION)).thenReturn(tagTypeDisplayNameAggs);
    when(tagTypeDisplayNameEntry.getAggregations()).thenReturn(aggregations);
    when(tagTypeDisplayNameAggs.getBuckets()).thenReturn(tagTypeDisplayNameEntryList);
    when(tagTypeDisplayNameEntry.getKeyAsString()).thenReturn(TAG_TYPE_DISPLAY_NAME);
    StringTerms tagCodeAggs = mock(StringTerms.class);
    StringTerms.Bucket tagCodeEntry = mock(StringTerms.Bucket.class);
    List<Terms.Bucket> tagCodeEntryList = Collections.singletonList(tagCodeEntry);
    when(aggregations.get(TAG_CODE_AGGREGATION)).thenReturn(tagCodeAggs);
    when(tagCodeAggs.getBuckets()).thenReturn(tagCodeEntryList);
    when(tagCodeEntry.getAggregations()).thenReturn(aggregations);
    when(tagCodeEntry.getKeyAsString()).thenReturn(TAG_CODE);
    when(tagCodeEntry.getDocCount()).thenReturn((long) TAG_CODE_COUNT);
    Terms tagNameAggs = mock(Terms.class);
    Terms.Bucket tagNameEntry = mock(Terms.Bucket.class);
    List<Terms.Bucket> tagNameEntryList = Collections.singletonList(tagNameEntry);
    when(tagNameEntry.getAggregations()).thenReturn(aggregations);
    when(aggregations.get(TAG_NAME_AGGREGATION)).thenReturn(tagNameAggs);
    when(tagNameAggs.getBuckets()).thenReturn(tagNameEntryList);
    when(tagNameEntry.getKeyAsString()).thenReturn(TAG_DISPLAY_NAME);
    List<TagTypeIndexSearchResponseDto> resultList = elasticsearchHelper.getTagTagIndexSearchResponseDto(searchResponse);
    List<TagTypeIndexSearchResponseDto> expectedList = new ArrayList<>();
    List<TagIndexSearchResponseDto> expectedTagList = new ArrayList<>();
    expectedTagList.add(new TagIndexSearchResponseDto(TAG_CODE, TAG_CODE_COUNT, TAG_DISPLAY_NAME));
    expectedList.add(new TagTypeIndexSearchResponseDto(TAG_TYPE_CODE, expectedTagList, TAG_TYPE_DISPLAY_NAME));
    assertEquals(expectedList, resultList);
}
Also used : Aggregations(org.elasticsearch.search.aggregations.Aggregations) TagIndexSearchResponseDto(org.finra.herd.model.dto.TagIndexSearchResponseDto) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) TagTypeIndexSearchResponseDto(org.finra.herd.model.dto.TagTypeIndexSearchResponseDto) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Example 12 with StringTerms

use of org.elasticsearch.search.aggregations.bucket.terms.StringTerms 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

StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)12 SearchResponse (org.elasticsearch.action.search.SearchResponse)10 Aggregation (org.elasticsearch.search.aggregations.Aggregation)6 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)6 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)6 SignificantTerms (org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms)5 AggregationBuilders.significantTerms (org.elasticsearch.search.aggregations.AggregationBuilders.significantTerms)3 Aggregations (org.elasticsearch.search.aggregations.Aggregations)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 MultiBucketsAggregation (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)2 LongTerms (org.elasticsearch.search.aggregations.bucket.terms.LongTerms)2 TermsBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder)2 SearchRequest (org.elasticsearch.action.search.SearchRequest)1 BytesArray (org.elasticsearch.common.bytes.BytesArray)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 SearchPlugin (org.elasticsearch.plugins.SearchPlugin)1 AggregationSpec (org.elasticsearch.plugins.SearchPlugin.AggregationSpec)1 PipelineAggregationSpec (org.elasticsearch.plugins.SearchPlugin.PipelineAggregationSpec)1