本文整理汇总了Java中org.elasticsearch.search.aggregations.Aggregations.get方法的典型用法代码示例。如果您正苦于以下问题:Java Aggregations.get方法的具体用法?Java Aggregations.get怎么用?Java Aggregations.get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.elasticsearch.search.aggregations.Aggregations
的用法示例。
在下文中一共展示了Aggregations.get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: mapResults
import org.elasticsearch.search.aggregations.Aggregations; //导入方法依赖的package包/类
@Override
public <T> FacetedPageImpl<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
Aggregations aggregations = response.getAggregations();
Terms agg = aggregations.get(term) ;
long total = agg.getSumOfOtherDocCounts() ;
List<T> results = new ArrayList<T>();
if(agg.getBuckets()!=null && agg.getBuckets().size()>0){
for (Terms.Bucket entry : agg.getBuckets()) {
if(!StringUtils.isBlank(name) && entry.getAggregations().get(name)!=null){
TopHits topHits = entry.getAggregations().get(name);
for (SearchHit hit : topHits.getHits().getHits()) {
T data = mapEntity(hit.getSourceAsString() , hit , clazz) ;
if(data instanceof UKAgg){
((UKAgg) data).setRowcount((int) topHits.getHits().getTotalHits());
}
results.add(data) ;
}
}
}
}
return new FacetedPageImpl<T>(results, pageable, total);
}
示例2: groupByOnNestedFieldWithFilterTest
import org.elasticsearch.search.aggregations.Aggregations; //导入方法依赖的package包/类
@Test
public void groupByOnNestedFieldWithFilterTest() throws Exception {
Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info),filter('myFilter',message.info = 'a')", TEST_INDEX));
InternalNested nested = result.get("[email protected]");
InternalFilter filter = nested.getAggregations().get("[email protected]");
Terms infos = filter.getAggregations().get("message.info");
Assert.assertEquals(1,infos.getBuckets().size());
for(Terms.Bucket bucket : infos.getBuckets()) {
String key = bucket.getKey().toString();
long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue();
if(key.equalsIgnoreCase("a")) {
Assert.assertEquals(2, count);
}
else {
throw new Exception(String.format("Unexpected key. expected: only a . found: %s", key));
}
}
}
示例3: histogramOnNestedField
import org.elasticsearch.search.aggregations.Aggregations; //导入方法依赖的package包/类
@Test
public void histogramOnNestedField() throws Exception {
Aggregations result = query(String.format("select count(*) from %s/nestedType group by histogram('field'='message.dayOfWeek','nested'='message','interval'='2' , 'alias' = 'someAlias' )", TEST_INDEX));
InternalNested nested = result.get("[email protected]");
Histogram histogram = nested.getAggregations().get("someAlias");
for(Histogram.Bucket bucket : histogram.getBuckets()){
long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue();
String key = ((Double)bucket.getKey()).intValue()+"";
if(key.equals("0") || key.equals("4")){
Assert.assertEquals(2,count);
}
else if (key.equals("2")){
Assert.assertEquals(1,count);
}
else{
Assert.assertTrue("only 0 2 4 keys are allowed got:" + key,false);
}
}
}
示例4: groupByTest
import org.elasticsearch.search.aggregations.Aggregations; //导入方法依赖的package包/类
@Test
public void groupByTest() throws Exception {
Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY gender", TEST_INDEX));
Terms gender = result.get("gender");
for(Terms.Bucket bucket : gender.getBuckets()) {
String key = bucket.getKey().toString();
long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue();
if(key.equalsIgnoreCase("m")) {
Assert.assertEquals(507, count);
}
else if(key.equalsIgnoreCase("f")) {
Assert.assertEquals(493, count);
}
else {
throw new Exception(String.format("Unexpected key. expected: m OR f. found: %s", key));
}
}
}
示例5: getAggregation
import org.elasticsearch.search.aggregations.Aggregations; //导入方法依赖的package包/类
/**
* Returns the aggregation that is associated with the specified name. This method also validates that the retrieved aggregation exists.
*
* @param searchResponse the response of the search request
* @param aggregationName the name of the aggregation
*
* @return the aggregation
*/
public Terms getAggregation(SearchResponse searchResponse, String aggregationName)
{
// Retrieve the aggregations from the search response.
Aggregations aggregations = getAggregationsFromSearchResponse(searchResponse);
// Retrieve the specified aggregation.
Terms aggregation = aggregations.get(aggregationName);
// Fail if retrieved aggregation is null.
if (aggregation == null)
{
// Log the error along with the search response contents.
LOGGER.error("Failed to retrieve \"{}\" aggregation from the search response. searchResponse={}", aggregationName,
jsonHelper.objectToJson(searchResponse));
// Throw an exception.
throw new IllegalStateException("Invalid search result.");
}
return aggregation;
}
示例6: getGroupResults
import org.elasticsearch.search.aggregations.Aggregations; //导入方法依赖的package包/类
private List<GroupResult> getGroupResults(GroupRequest groupRequest, int index, Aggregations aggregations, Map<String, FieldType> commonColumnMetadata) {
List<Group> groups = groupRequest.getGroups();
String field = groups.get(index).getField();
Terms terms = aggregations.get(getGroupByAggregationName(field));
List<GroupResult> searchResultGroups = new ArrayList<>();
for(Bucket bucket: terms.getBuckets()) {
GroupResult groupResult = new GroupResult();
groupResult.setKey(formatKey(bucket.getKey(), commonColumnMetadata.get(field)));
groupResult.setTotal(bucket.getDocCount());
Optional<String> scoreField = groupRequest.getScoreField();
if (scoreField.isPresent()) {
Sum score = bucket.getAggregations().get(getSumAggregationName(scoreField.get()));
groupResult.setScore(score.getValue());
}
if (index < groups.size() - 1) {
groupResult.setGroupedBy(groups.get(index + 1).getField());
groupResult.setGroupResults(getGroupResults(groupRequest, index + 1, bucket.getAggregations(), commonColumnMetadata));
}
searchResultGroups.add(groupResult);
}
return searchResultGroups;
}
示例7: groupByTest
import org.elasticsearch.search.aggregations.Aggregations; //导入方法依赖的package包/类
@Test
public void groupByTest() throws Exception {
Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY gender", TestsConstants.TEST_INDEX));
Terms gender = result.get("gender");
for(Terms.Bucket bucket : gender.getBuckets()) {
String key = bucket.getKey().toString();
long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue();
if(key.equalsIgnoreCase("m")) {
Assert.assertEquals(507, count);
}
else if(key.equalsIgnoreCase("f")) {
Assert.assertEquals(493, count);
}
else {
throw new Exception(String.format("Unexpected key. expected: m OR f. found: %s", key));
}
}
}
示例8: multipleGroupByTest
import org.elasticsearch.search.aggregations.Aggregations; //导入方法依赖的package包/类
@Test
public void multipleGroupByTest() throws Exception {
Set expectedAges = new HashSet<Integer>(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers()));
Map<String, Set<Integer>> buckets = new HashMap<>();
Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY gender, age", TestsConstants.TEST_INDEX));
Terms gender = result.get("gender");
for(Terms.Bucket genderBucket : gender.getBuckets()) {
String genderKey = genderBucket.getKey().toString();
buckets.put(genderKey, new HashSet<Integer>());
Terms ageBuckets = (Terms) genderBucket.getAggregations().get("age");
for(Terms.Bucket ageBucket : ageBuckets.getBuckets()) {
buckets.get(genderKey).add(Integer.parseInt(ageBucket.getKey().toString()));
}
}
Assert.assertEquals(2, buckets.keySet().size());
Assert.assertEquals(expectedAges, buckets.get("m"));
Assert.assertEquals(expectedAges, buckets.get("f"));
}
示例9: convertResult
import org.elasticsearch.search.aggregations.Aggregations; //导入方法依赖的package包/类
@Override
public AggregationResults convertResult(ResultsConverter resultsConverter, Aggregation aggregation, Aggregations aggs) {
AggregationResults result = null;
String name = aggregation.getName();
if (aggs.get(name) instanceof Range) {
Range ranges = (Range) aggs.get(name);
List<Bucket> buckets = new ArrayList<>();
for (Range.Bucket bucket : ranges.getBuckets()) {
String label = bucket.getFrom() + " TO " + bucket.getTo();
String key = bucket.getKey();
Bucket b = new Bucket(key, bucket.getDocCount(), label);
buckets.add(b);
for (Aggregation subAgg : aggregation.getAggs().values()) {
AggregationResults subResults = resultsConverter.convertResults(subAgg, bucket.getAggregations());
if (subResults != null) {
b.getAggs().put(subAgg.getName(), subResults);
}
}
}
result = new AggregationResults(name, buckets);
}
return result;
}
示例10: loadValueStats
import org.elasticsearch.search.aggregations.Aggregations; //导入方法依赖的package包/类
protected synchronized void loadValueStats() {
if (stats != null) {
return;
}
// TODO join userQueryBuilder and itemQueryBuilder
final SearchResponse response = client
.prepareSearch(preferenceIndex)
.setTypes(preferenceType)
.setQuery(getLastAccessedFilterQuery())
.setSize(0)
.addAggregation(
AggregationBuilders.stats(valueField).field(valueField))
.execute().actionGet();
final Aggregations aggregations = response.getAggregations();
stats = aggregations.get(valueField);
}
示例11: buildNestedTrendStats
import org.elasticsearch.search.aggregations.Aggregations; //导入方法依赖的package包/类
private List<BucketResponse<List<StatsTrendValue>>> buildNestedTrendStats(List<String> nesting,
Aggregations aggregations) {
final String field = nesting.get(0);
final List<String> remainingFields = (nesting.size() > 1) ? nesting.subList(1, nesting.size())
: new ArrayList<>();
Terms terms = aggregations.get(Utils.sanitizeFieldForAggregation(field));
List<BucketResponse<List<StatsTrendValue>>> bucketResponses = Lists.newArrayList();
for (Terms.Bucket bucket : terms.getBuckets()) {
BucketResponse<List<StatsTrendValue>> bucketResponse = new BucketResponse<>();
bucketResponse.setKey(bucket.getKey());
if (nesting.size() == 1) {
bucketResponse.setResult(buildStatsTrendValue(getParameter().getField(), bucket.getAggregations()));
} else {
bucketResponse.setBuckets(buildNestedTrendStats(remainingFields, bucket.getAggregations()));
}
bucketResponses.add(bucketResponse);
}
return bucketResponses;
}
示例12: buildStatsTrendValue
import org.elasticsearch.search.aggregations.Aggregations; //导入方法依赖的package包/类
private List<StatsTrendValue> buildStatsTrendValue(String field, Aggregations aggregations) {
String dateHistogramKey = Utils.getDateHistogramKey(getParameter().getTimestamp());
DateHistogram dateHistogram = aggregations.get(dateHistogramKey);
Collection<? extends DateHistogram.Bucket> buckets = dateHistogram.getBuckets();
String metricKey = Utils.getExtendedStatsAggregationKey(field);
String percentileMetricKey = Utils.getPercentileAggregationKey(field);
List<StatsTrendValue> statsValueList = Lists.newArrayList();
for (DateHistogram.Bucket bucket : buckets) {
StatsTrendValue statsTrendValue = new StatsTrendValue();
statsTrendValue.setPeriod(bucket.getKeyAsNumber());
InternalExtendedStats extendedStats = InternalExtendedStats.class.cast(bucket.getAggregations().getAsMap().get(metricKey));
statsTrendValue.setStats(Utils.createExtendedStatsResponse(extendedStats));
InternalPercentiles internalPercentile = InternalPercentiles.class.cast(bucket.getAggregations().getAsMap().get(percentileMetricKey));
statsTrendValue.setPercentiles(Utils.createPercentilesResponse(internalPercentile));
statsValueList.add(statsTrendValue);
}
return statsValueList;
}
示例13: buildNestedStats
import org.elasticsearch.search.aggregations.Aggregations; //导入方法依赖的package包/类
private List<BucketResponse<StatsValue>> buildNestedStats(List<String> nesting, Aggregations aggregations) {
final String field = nesting.get(0);
final List<String> remainingFields = (nesting.size() > 1) ? nesting.subList(1, nesting.size())
: new ArrayList<>();
Terms terms = aggregations.get(Utils.sanitizeFieldForAggregation(field));
List<BucketResponse<StatsValue>> bucketResponses = Lists.newArrayList();
for (Terms.Bucket bucket : terms.getBuckets()) {
BucketResponse<StatsValue> bucketResponse = new BucketResponse<>();
bucketResponse.setKey(bucket.getKey());
if (nesting.size() == 1) {
bucketResponse.setResult(buildStatsValue(getParameter().getField(), bucket.getAggregations()));
} else {
bucketResponse.setBuckets(buildNestedStats(remainingFields, bucket.getAggregations()));
}
bucketResponses.add(bucketResponse);
}
return bucketResponses;
}
示例14: buildResponse
import org.elasticsearch.search.aggregations.Aggregations; //导入方法依赖的package包/类
private HistogramResponse buildResponse(Aggregations aggregations) {
if (aggregations == null) {
return new HistogramResponse(Collections.<HistogramResponse.Count>emptyList());
}
String dateHistogramKey = Utils.getDateHistogramKey(getParameter().getField());
DateHistogram dateHistogram = aggregations.get(dateHistogramKey);
Collection<? extends DateHistogram.Bucket> buckets = dateHistogram.getBuckets();
List<HistogramResponse.Count> counts = new ArrayList<>(buckets.size());
for (DateHistogram.Bucket bucket : buckets) {
if (!CollectionUtils.isNullOrEmpty(getParameter().getUniqueCountOn())) {
String key = Utils.sanitizeFieldForAggregation(getParameter().getUniqueCountOn());
Cardinality cardinality = bucket.getAggregations().get(key);
counts.add(new HistogramResponse.Count(bucket.getKeyAsNumber(), cardinality.getValue()));
} else {
counts.add(new HistogramResponse.Count(bucket.getKeyAsNumber(), bucket.getDocCount()));
}
}
return new HistogramResponse(counts);
}
示例15: buildResponse
import org.elasticsearch.search.aggregations.Aggregations; //导入方法依赖的package包/类
private TrendResponse buildResponse(TrendRequest request, Aggregations aggregations) {
String field = request.getField();
Map<String, List<TrendResponse.Count>> trendCounts = new TreeMap<>();
Terms terms = aggregations.get(Utils.sanitizeFieldForAggregation(field));
for (Terms.Bucket bucket : terms.getBuckets()) {
final String key = bucket.getKeyAsText().string();
List<TrendResponse.Count> counts = Lists.newArrayList();
Aggregations subAggregations = bucket.getAggregations();
Histogram histogram = subAggregations.get(Utils.getDateHistogramKey(request.getTimestamp()));
for (Histogram.Bucket histogramBucket : histogram.getBuckets()) {
if (!CollectionUtils.isNullOrEmpty(getParameter().getUniqueCountOn())) {
String uniqueCountKey = Utils.sanitizeFieldForAggregation(getParameter().getUniqueCountOn());
Cardinality cardinality = histogramBucket.getAggregations().get(uniqueCountKey);
counts.add(new TrendResponse.Count(histogramBucket.getKeyAsNumber(), cardinality.getValue()));
} else {
counts.add(new TrendResponse.Count(histogramBucket.getKeyAsNumber(), histogramBucket.getDocCount()));
}
}
trendCounts.put(key, counts);
}
return new TrendResponse(trendCounts);
}