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);
}
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);
}
Aggregations