use of 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;
}
use of 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");
}
use of 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);
}
use of 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);
}
use of org.elasticsearch.search.aggregations.Aggregation in project vertigo by KleeGroup.
the class ESFacetedQueryResultBuilder method createCluster.
private Map<FacetValue, DtList<I>> createCluster(final Map<String, I> dtcIndex, final Map<I, Map<DtField, String>> resultHighlights) {
final Map<FacetValue, DtList<I>> resultCluster = new LinkedHashMap<>();
final FacetDefinition facetDefinition = searchQuery.getClusteringFacetDefinition();
final Aggregation facetAggregation = queryResponse.getAggregations().get(facetDefinition.getName());
if (facetDefinition.isRangeFacet()) {
// Cas des facettes par 'range'
final MultiBucketsAggregation multiBuckets = (MultiBucketsAggregation) facetAggregation;
for (final FacetValue facetRange : facetDefinition.getFacetRanges()) {
final Bucket value = getBucketByKey(multiBuckets, facetRange.getListFilter().getFilterValue());
populateCluster(value, facetRange, resultCluster, dtcIndex, resultHighlights);
}
} else {
// Cas des facettes par 'term'
final MultiBucketsAggregation multiBuckets = (MultiBucketsAggregation) facetAggregation;
FacetValue facetValue;
for (final Bucket bucket : multiBuckets.getBuckets()) {
final String term = bucket.getKeyAsString();
final String query = facetDefinition.getDtField().getName() + ":\"" + term + "\"";
final MessageText label = MessageText.of(term);
facetValue = new FacetValue(term, ListFilter.of(query), label);
populateCluster(bucket, facetValue, resultCluster, dtcIndex, resultHighlights);
}
}
return resultCluster;
}
Aggregations