Java Code Examples for io.searchbox.core.search.aggregation.TermsAggregation#Entry

The following examples show how to use io.searchbox.core.search.aggregation.TermsAggregation#Entry . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticSearchReportService.java    From vind with Apache License 2.0 6 votes vote down vote up
@Override
public LinkedHashMap<String, Long> getTopUsers() {
    final String query = elasticClient.loadQueryFromFile("topUsers",
            getEsFilters(),
            this.configuration.getMessageWrapper(),
            this.configuration.getMessageWrapper(),
            this.configuration.getApplicationId(),
            this.configuration.getMessageWrapper(),
            this.configuration.getMessageWrapper(),
            this.configuration.getMessageWrapper(),
            this.getFrom().toInstant().toEpochMilli(),
            this.getTo().toInstant().toEpochMilli(),
            this.configuration.getMessageWrapper());

    final SearchResult searchResult = elasticClient.getQuery(query);
    final List<TermsAggregation.Entry> termEntries = searchResult.getAggregations().getTermsAggregation("user").getBuckets();
    final LinkedHashMap<String, Long> result = new LinkedHashMap<>();
    termEntries.stream().sorted(Comparator.comparingLong(TermsAggregation.Entry::getCount).reversed())
            .forEach(entry -> result.put(entry.getKey(), entry.getCount()));

    return result;
}
 
Example 2
Source File: ElasticSearchReportService.java    From vind with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> getTopFacetFields() {
    final String query = elasticClient.loadQueryFromFile("topFacetFields",
            getEsFilters(),
            this.configuration.getMessageWrapper(),
            this.configuration.getMessageWrapper(),
            this.configuration.getApplicationId(),
            this.configuration.getMessageWrapper(),
            this.configuration.getMessageWrapper(),
            this.configuration.getMessageWrapper(),
            this.getFrom().toInstant().toEpochMilli(),
            this.getTo().toInstant().toEpochMilli(),
            this.configuration.getMessageWrapper(),
            this.configuration.getMessageWrapper());

    final SearchResult searchResult = elasticClient.getQuery(query);
    final List<TermsAggregation.Entry> termEntries = searchResult.getAggregations().getTermsAggregation("facets").getBuckets();
    return termEntries.stream()
            .map(TermsAggregation.Entry::getKey)
            .collect(Collectors.toList());
}
 
Example 3
Source File: ElasticSearchReportService.java    From vind with Apache License 2.0 6 votes vote down vote up
@Override
public LinkedHashMap<String, JsonObject> getTopSuggestionFields() {
    final String query = elasticClient.loadQueryFromFile("topSuggestionFields",
            getEsFilters(),
            this.configuration.getMessageWrapper(),
            this.configuration.getMessageWrapper(),
            this.configuration.getApplicationId(),
            this.configuration.getMessageWrapper(),
            this.getFrom().toInstant().toEpochMilli(),
            this.getTo().toInstant().toEpochMilli(),
            this.configuration.getMessageWrapper());

    final SearchResult searchResult = elasticClient.getQuery(query);
    final List<TermsAggregation.Entry> termEntries = searchResult.getAggregations()
            .getTermsAggregation("suggestionFields")
            .getBuckets();
    final List<String> suggestionFields = termEntries.stream()
            .map(TermsAggregation.Entry::getKey)
            .collect(Collectors.toList());

    return prepareScopeFilterResults(suggestionFields, "Suggest");
}
 
Example 4
Source File: ElasticSearchReportService.java    From vind with Apache License 2.0 6 votes vote down vote up
@Override
public LinkedHashMap<String, Long> getTopQueries() {
    final String query = elasticClient.loadQueryFromFile("topQueries",
            getEsFilters(),
            this.configuration.getMessageWrapper(),
            this.configuration.getMessageWrapper(),
            this.configuration.getApplicationId(),
            this.configuration.getMessageWrapper(),
            this.configuration.getMessageWrapper(),
            this.configuration.getMessageWrapper(),
            this.getFrom().toInstant().toEpochMilli(),
            this.getTo().toInstant().toEpochMilli(),
            this.configuration.getMessageWrapper());

    final SearchResult searchResult = elasticClient.getQuery(query);
    final List<TermsAggregation.Entry> termEntries = searchResult.getAggregations().getTermsAggregation("queries").getBuckets();
    final LinkedHashMap<String, Long> result = new LinkedHashMap<>();
    termEntries.stream().sorted(Comparator.comparingLong(TermsAggregation.Entry::getCount).reversed())
            .forEach(entry -> result.put(entry.getKey(), entry.getCount()));

    return result;
}
 
Example 5
Source File: ElasticSearchReportService.java    From vind with Apache License 2.0 6 votes vote down vote up
@Override
public LinkedHashMap<String, Long> getTopFilteredQueries() {
    final String query = elasticClient.loadQueryFromFile("topFilteredQueries",
            getEsFilters(),
            this.configuration.getMessageWrapper(),
            this.configuration.getMessageWrapper(),
            this.configuration.getApplicationId(),
            this.configuration.getMessageWrapper(),
            this.configuration.getMessageWrapper(),
            this.configuration.getMessageWrapper(),
            this.configuration.getReportWriterConfiguration().getQueryFilter(),
            this.configuration.getMessageWrapper(),
            this.getFrom().toInstant().toEpochMilli(),
            this.getTo().toInstant().toEpochMilli(),
            this.configuration.getMessageWrapper());


    final SearchResult searchResult = elasticClient.getQuery(query);
    final List<TermsAggregation.Entry> termEntries = searchResult.getAggregations().getTermsAggregation("queries").getBuckets();
    final LinkedHashMap<String, Long> result = new LinkedHashMap<>();
    termEntries.stream().sorted(Comparator.comparingLong(TermsAggregation.Entry::getCount).reversed())
            .forEach(entry -> result.put(entry.getKey(), entry.getCount()));

    return result;
}
 
Example 6
Source File: ElasticSearchReportService.java    From vind with Apache License 2.0 6 votes vote down vote up
@Override
public LinkedHashMap<String, JsonObject> getTopFilterFields() {
    final String query = elasticClient.loadQueryFromFile("topFilterFields",
            getEsFilters(),
            this.configuration.getMessageWrapper(),
            this.configuration.getMessageWrapper(),
            this.configuration.getApplicationId(),
            this.configuration.getMessageWrapper(),
            this.configuration.getMessageWrapper(),
            this.configuration.getMessageWrapper(),
            this.configuration.getMessageWrapper(),
            this.getFrom().toInstant().toEpochMilli(),
            this.getTo().toInstant().toEpochMilli(),
            this.configuration.getMessageWrapper());

    final SearchResult searchResult = elasticClient.getQuery(query);
    final List<TermsAggregation.Entry> termEntries = searchResult.getAggregations()
            .getTermsAggregation("filterFields")
            .getBuckets();
    final List<String> filterFields = termEntries.stream()
            .map(TermsAggregation.Entry::getKey)
            .collect(Collectors.toList());

    return prepareScopeFilterResults(filterFields, "Filter");
}
 
Example 7
Source File: ElasticsearchHelper.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Creates result type facet response dto
 *
 * @param searchResult search result
 *
 * @return result type facet response dto list
 */
public List<ResultTypeIndexSearchResponseDto> getResultTypeIndexSearchResponseDto(SearchResult searchResult)
{
    MetricAggregation metricAggregation = searchResult.getAggregations();
    TermsAggregation resultTypeAggregation = metricAggregation.getTermsAggregation(RESULT_TYPE_AGGS);

    List<TermsAggregation.Entry> buckets = resultTypeAggregation.getBuckets();

    List<ResultTypeIndexSearchResponseDto> resultTypeIndexSearchResponseDtos = new ArrayList<>();

    for (TermsAggregation.Entry entry : buckets)
    {
        ResultTypeIndexSearchResponseDto dto = new ResultTypeIndexSearchResponseDto();
        dto.setResultTypeCode(entry.getKeyAsString());
        dto.setResultTypeDisplayName(entry.getKeyAsString());
        dto.setCount(entry.getCount());
        resultTypeIndexSearchResponseDtos.add(dto);
    }

    return resultTypeIndexSearchResponseDtos;
}
 
Example 8
Source File: ElasticSearchHelperTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetResultTypeIndexSearchResponseDtoSearchResult()
{
    SearchResult searchResult = mock(SearchResult.class);
    MetricAggregation metricAggregation = mock(MetricAggregation.class);
    TermsAggregation termsAggregation = mock(TermsAggregation.class);
    List<TermsAggregation.Entry> buckets = new ArrayList<>();
    buckets.add(new TermsAggregation("TermAggregation", new JsonObject()).new Entry(new JsonObject(), "key", 1L));

    when(searchResult.getAggregations()).thenReturn(metricAggregation);
    when(metricAggregation.getTermsAggregation(RESULT_TYPE_AGGS)).thenReturn(termsAggregation);
    when(termsAggregation.getBuckets()).thenReturn(buckets);

    List<ResultTypeIndexSearchResponseDto> result = elasticsearchHelper.getResultTypeIndexSearchResponseDto(searchResult);
    assertThat("Result is null.", result, is(notNullValue()));
}
 
Example 9
Source File: ElasticsearchHelper.java    From herd with Apache License 2.0 4 votes vote down vote up
/**
 * get Tag Type index response
 *
 * @param termsAggregation termsAggregation
 *
 * @return list of tag type index search dto
 */
private List<TagTypeIndexSearchResponseDto> getTagTypeIndexSearchResponseDtosFromTermsAggregation(TermsAggregation termsAggregation)
{
    List<TermsAggregation.Entry> bucketsL0 = termsAggregation.getBuckets();

    List<TagTypeIndexSearchResponseDto> tagTypeIndexSearchResponseDtos = new ArrayList<>();

    for (TermsAggregation.Entry entryL1 : bucketsL0)
    {
        List<TagIndexSearchResponseDto> tagIndexSearchResponseDtos = new ArrayList<>();

        TermsAggregation termsAggregationL1 = entryL1.getTermsAggregation(TAGTYPE_NAME_AGGREGATION);
        List<TermsAggregation.Entry> bucketsL1 = termsAggregationL1.getBuckets();

        TagTypeIndexSearchResponseDto tagTypeIndexSearchResponseDto =
            new TagTypeIndexSearchResponseDto(entryL1.getKeyAsString(), tagIndexSearchResponseDtos, null);

        tagTypeIndexSearchResponseDtos.add(tagTypeIndexSearchResponseDto);

        for (TermsAggregation.Entry entryL2 : bucketsL1)
        {
            tagTypeIndexSearchResponseDto.setDisplayName(entryL2.getKeyAsString());
            TermsAggregation entryTermsAggregation = entryL2.getTermsAggregation(TAG_CODE_AGGREGATION);
            List<TermsAggregation.Entry> bucketsL2 = entryTermsAggregation.getBuckets();

            for (TermsAggregation.Entry entryL3 : bucketsL2)
            {
                TagIndexSearchResponseDto tagIndexSearchResponseDto = new TagIndexSearchResponseDto(entryL3.getKeyAsString(), entryL3.getCount(), null);
                tagIndexSearchResponseDtos.add(tagIndexSearchResponseDto);
                TermsAggregation entryEntryTermsAggregation = entryL3.getTermsAggregation(TAG_NAME_AGGREGATION);

                List<TermsAggregation.Entry> bucketsL3 = entryEntryTermsAggregation.getBuckets();

                for (TermsAggregation.Entry entryL4 : bucketsL3)
                {
                    tagIndexSearchResponseDto.setTagDisplayName(entryL4.getKeyAsString());
                }
            }
        }
    }

    return tagTypeIndexSearchResponseDtos;
}
 
Example 10
Source File: ElasticSearchHelperTest.java    From herd with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetTagTagIndexSearchResponseDtoSearchResult()
{
    SearchResult searchResult = mock(SearchResult.class);
    MetricAggregation metricAggregation = mock(MetricAggregation.class);
    TermsAggregation termsAggregation = mock(TermsAggregation.class);

    when(searchResult.getAggregations()).thenReturn(metricAggregation);
    when(metricAggregation.getTermsAggregation(TAG_TYPE_FACET_AGGS)).thenReturn(termsAggregation);

    List<TermsAggregation.Entry> buckets = new ArrayList<>();
    TermsAggregation.Entry entryL1 = mock(TermsAggregation.Entry.class);
    buckets.add(entryL1);
    when(termsAggregation.getBuckets()).thenReturn(buckets);

    TermsAggregation termsAggregationL1 = mock(TermsAggregation.class);
    when(entryL1.getTermsAggregation(TAGTYPE_NAME_AGGREGATION)).thenReturn(termsAggregationL1);

    List<TermsAggregation.Entry> bucketsL1 = new ArrayList<>();
    TermsAggregation.Entry entryL2 = mock(TermsAggregation.Entry.class);
    bucketsL1.add(entryL2);
    when(termsAggregationL1.getBuckets()).thenReturn(bucketsL1);

    TermsAggregation entryTermsAggregation = mock(TermsAggregation.class);
    when(entryL2.getTermsAggregation(TAG_CODE_AGGREGATION)).thenReturn(entryTermsAggregation);

    List<TermsAggregation.Entry> bucketsL2 = new ArrayList<>();
    TermsAggregation.Entry entryL3 = mock(TermsAggregation.Entry.class);
    bucketsL2.add(entryL3);
    when(entryTermsAggregation.getBuckets()).thenReturn(bucketsL2);

    TermsAggregation entryEntryTermsAggregation = mock(TermsAggregation.class);
    when(entryL3.getTermsAggregation(TAG_NAME_AGGREGATION)).thenReturn(entryEntryTermsAggregation);

    List<TermsAggregation.Entry> bucketsL3 = new ArrayList<>();
    TermsAggregation.Entry entryL4 = mock(TermsAggregation.Entry.class);
    bucketsL3.add(entryL4);
    when(entryEntryTermsAggregation.getBuckets()).thenReturn(bucketsL3);

    List<TagTypeIndexSearchResponseDto> result = elasticsearchHelper.getTagTagIndexSearchResponseDto(searchResult);
    assertThat("Result is null.", result, is(notNullValue()));
}