use of io.searchbox.core.search.aggregation.TermsAggregation in project herd by FINRAOS.
the class ElasticsearchHelper method getResultTypeIndexSearchResponseDto.
/**
* 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;
}
use of io.searchbox.core.search.aggregation.TermsAggregation in project graylog2-server by Graylog2.
the class QuerySuggestionsES6 method suggest.
@Override
public SuggestionResponse suggest(SuggestionRequest req) {
final Set<String> affectedIndices = indexLookup.indexNamesForStreamsInTimeRange(req.streams(), req.timerange());
final SearchSourceBuilder search = new SearchSourceBuilder().query(QueryBuilders.prefixQuery(req.field(), req.input())).size(0).aggregation(AggregationBuilders.terms("fieldvalues").field(req.field()).size(req.size())).suggest(new SuggestBuilder().addSuggestion("corrections", SuggestBuilders.termSuggestion(req.field()).text(req.input()).size(req.size())));
final Search.Builder searchBuilder = new Search.Builder(search.toString()).addType(IndexMapping.TYPE_MESSAGE).addIndex(affectedIndices.isEmpty() ? Collections.singleton("") : affectedIndices).allowNoIndices(false).ignoreUnavailable(false);
try {
final SearchResult result = JestUtils.execute(jestClient, searchBuilder.build(), () -> "Unable to perform aggregation: ");
final TermsAggregation aggregation = result.getAggregations().getTermsAggregation("fieldvalues");
final List<SuggestionEntry> entries = aggregation.getBuckets().stream().map(b -> new SuggestionEntry(b.getKeyAsString(), b.getCount())).collect(Collectors.toList());
if (!entries.isEmpty()) {
return SuggestionResponse.forSuggestions(req.field(), req.input(), entries, aggregation.getSumOtherDocCount());
} else {
final List<SuggestionEntry> corrections = Optional.of(result.getJsonObject()).map(o -> o.get("suggest")).map(o -> o.get("corrections")).map(o -> o.get(0)).map(o -> o.get("options")).map(options -> StreamSupport.stream(Spliterators.spliteratorUnknownSize(options.elements(), Spliterator.ORDERED), false).map(option -> new SuggestionEntry(option.get("text").textValue(), option.get("freq").longValue())).collect(Collectors.toList())).orElseGet(Collections::emptyList);
return SuggestionResponse.forSuggestions(req.field(), req.input(), corrections, null);
}
} catch (Exception e) {
final SuggestionError err = SuggestionError.create(e.getClass().getSimpleName(), e.getMessage());
return SuggestionResponse.forError(req.field(), req.input(), err);
}
}
use of io.searchbox.core.search.aggregation.TermsAggregation in project graylog2-server by Graylog2.
the class IndexToolsAdapterES6 method fieldHistogram.
@Override
public Map<DateTime, Map<String, Long>> fieldHistogram(String fieldName, Set<String> indices, Optional<Set<String>> includedStreams, long interval) {
final BoolQueryBuilder queryBuilder = buildStreamIdFilter(includedStreams);
final FilterAggregationBuilder the_filter = AggregationBuilders.filter(AGG_FILTER, queryBuilder).subAggregation(AggregationBuilders.dateHistogram(AGG_DATE_HISTOGRAM).field("timestamp").subAggregation(AggregationBuilders.terms(AGG_MESSAGE_FIELD).field(fieldName)).interval(interval).minDocCount(1L));
final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()).aggregation(the_filter);
final Search.Builder searchBuilder = new Search.Builder(searchSourceBuilder.toString()).addIndex(indices).addType(IndexMapping.TYPE_MESSAGE);
final SearchResult searchResult = JestUtils.execute(this.jestClient, searchBuilder.build(), () -> "Unable to retrieve field histogram.");
final FilterAggregation filterAggregation = searchResult.getAggregations().getFilterAggregation(AGG_FILTER);
final DateHistogramAggregation dateHistogram = filterAggregation.getDateHistogramAggregation(AGG_DATE_HISTOGRAM);
final List<DateHistogramAggregation.DateHistogram> histogramBuckets = dateHistogram.getBuckets();
final Map<DateTime, Map<String, Long>> result = Maps.newHashMapWithExpectedSize(histogramBuckets.size());
for (HistogramAggregation.Histogram bucket : histogramBuckets) {
final DateTime date = new DateTime(bucket.getKey()).toDateTime(DateTimeZone.UTC);
final TermsAggregation sourceFieldAgg = bucket.getTermsAggregation(AGG_MESSAGE_FIELD);
final List<TermsAggregation.Entry> termBuckets = sourceFieldAgg.getBuckets();
final HashMap<String, Long> termCounts = Maps.newHashMapWithExpectedSize(termBuckets.size());
for (TermsAggregation.Entry termBucket : termBuckets) {
termCounts.put(termBucket.getKeyAsString(), termBucket.getCount());
}
result.put(date, termCounts);
}
return ImmutableMap.copyOf(result);
}
use of io.searchbox.core.search.aggregation.TermsAggregation in project herd by FINRAOS.
the class ElasticSearchHelperTest method testGetResultTypeIndexSearchResponseDtoSearchResult.
@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()));
}
use of io.searchbox.core.search.aggregation.TermsAggregation in project herd by FINRAOS.
the class ElasticsearchHelper method getNestedTagTagIndexSearchResponseDto.
/**
* create tag tag index response dto
*
* @param searchResult search result
*
* @return tag type index search response dto list
*/
public List<TagTypeIndexSearchResponseDto> getNestedTagTagIndexSearchResponseDto(SearchResult searchResult) {
MetricAggregation metricAggregation = searchResult.getAggregations();
MetricAggregation tagFacetAggregation = metricAggregation.getSumAggregation(TAG_FACET_AGGS);
TermsAggregation tagTypeCodesAggregation = tagFacetAggregation.getTermsAggregation(TAGTYPE_CODE_AGGREGATION);
return getTagTypeIndexSearchResponseDtosFromTermsAggregation(tagTypeCodesAggregation);
}
Aggregations