org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket. 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: InternalOrder.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public static void writeOrder(Terms.Order order, StreamOutput out) throws IOException {
    if (order instanceof Aggregation) {
        out.writeByte(order.id());
        Aggregation aggregationOrder = (Aggregation) order;
        out.writeBoolean(((MultiBucketsAggregation.Bucket.SubAggregationComparator) aggregationOrder.comparator).asc());
        AggregationPath path = ((Aggregation) order).path();
        out.writeString(path.toString());
    } else if (order instanceof CompoundOrder) {
        CompoundOrder compoundOrder = (CompoundOrder) order;
            out.writeByte(order.id());
            out.writeVInt(compoundOrder.orderElements.size());
            for (Terms.Order innerOrder : compoundOrder.orderElements) {
                Streams.writeOrder(innerOrder, out);
            }
    } else {
        out.writeByte(order.id());
    }
}
 
Example #2
Source File: ElasticsearchSearchDao.java    From metron with Apache License 2.0 6 votes vote down vote up
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();
  List<GroupResult> searchResultGroups = new ArrayList<>();
  if(aggregations != null) {
    Terms terms = aggregations.get(getGroupByAggregationName(field));
    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;
}
 
Example #3
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public int compare(Bucket o1, Bucket o2) {
    int result = 0;
    for (Iterator<Terms.Order> itr = compoundOrder.iterator(); itr.hasNext() && result == 0;) {
        result = itr.next().comparator(aggregator).compare(o1, o2);
    }
    return result;
}
 
Example #4
Source File: NpmSearchResponseFactoryTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private List<Terms.Bucket> generateBuckets(final int count,
                                           final boolean includeAuthorName,
                                           final boolean includeAuthorEmail)
{
  List<Bucket> buckets = new ArrayList<>();
  for (int index = 0; index < count; index++) {
    Terms.Bucket bucket = mock(Terms.Bucket.class);
    Aggregations aggregations = mock(Aggregations.class);
    TopHits topHits = mock(TopHits.class);
    SearchHits searchHits = mock(SearchHits.class);
    SearchHit searchHit = mock(SearchHit.class);

    when(bucket.getAggregations()).thenReturn(aggregations);
    when(aggregations.get("versions")).thenReturn(topHits);
    when(topHits.getHits()).thenReturn(searchHits);
    when(searchHits.getAt(0)).thenReturn(searchHit);
    when(searchHit.getScore()).thenReturn(1.0F);

    if (includeAuthorEmail) {
      when(npmSearchHitExtractor.extractAuthorEmail(searchHit)).thenReturn("author-email-" + index);
    }
    if (includeAuthorName) {
      when(npmSearchHitExtractor.extractAuthorName(searchHit)).thenReturn("author-name-" + index);
    }
    when(npmSearchHitExtractor.extractBugsUrl(searchHit)).thenReturn("bugs-url-" + index);
    when(npmSearchHitExtractor.extractDescription(searchHit)).thenReturn("description-" + index);
    when(npmSearchHitExtractor.extractHomepage(searchHit)).thenReturn("homepage-url-" + index);
    when(npmSearchHitExtractor.extractRepositoryUrl(searchHit)).thenReturn("repository-url-" + index);
    when(npmSearchHitExtractor.extractKeywords(searchHit)).thenReturn(singletonList("keyword-" + index));
    when(npmSearchHitExtractor.extractLastModified(searchHit)).thenReturn(DateTime.now());
    when(npmSearchHitExtractor.extractName(searchHit)).thenReturn("name-" + index);
    when(npmSearchHitExtractor.extractVersion(searchHit)).thenReturn("version-" + index);

    buckets.add(bucket);
  }
  return buckets;
}
 
Example #5
Source File: SearchResult.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public SearchResult(SearchResponse resp, Select select) throws SqlParseException {
	Aggregations aggs = resp.getAggregations();
	if (aggs.get("filter") != null) {
		InternalFilter inf = aggs.get("filter");
		aggs = inf.getAggregations();
	}
	if (aggs.get("group by") != null) {
		InternalTerms terms = aggs.get("group by");
		Collection<Bucket> buckets = terms.getBuckets();
		this.total = buckets.size();
		results = new ArrayList<>(buckets.size());
		for (Bucket bucket : buckets) {
			Map<String, Object> aggsMap = toAggsMap(bucket.getAggregations().getAsMap());
			aggsMap.put("docCount", bucket.getDocCount());
			results.add(aggsMap);
		}
	} else {
		results = new ArrayList<>(1);
		this.total = 1;
		Map<String, Object> map = new HashMap<>();
		for (Aggregation aggregation : aggs) {
			map.put(aggregation.getName(), covenValue(aggregation));
		}
		results.add(map);
	}

}
 
Example #6
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(Terms.Bucket o1, Terms.Bucket o2) {
    return  Long.compare(o2.getDocCount(), o1.getDocCount());
}
 
Example #7
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(Terms.Bucket o1, Terms.Bucket o2) {
    return Long.compare(o1.getDocCount(), o2.getDocCount());
}
 
Example #8
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(Terms.Bucket o1, Terms.Bucket o2) {
    return o2.compareTerm(o1);
}
 
Example #9
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(Terms.Bucket o1, Terms.Bucket o2) {
    return o1.compareTerm(o2);
}
 
Example #10
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
InternalOrder(byte id, String key, boolean asc, Comparator<Terms.Bucket> comparator) {
    this.id = id;
    this.key = key;
    this.asc = asc;
    this.comparator = comparator;
}
 
Example #11
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected Comparator<Terms.Bucket> comparator(Aggregator aggregator) {
    return comparator;
}
 
Example #12
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
Aggregation(String key, boolean asc) {
    super(ID, key, asc, new MultiBucketsAggregation.Bucket.SubAggregationComparator<Terms.Bucket>(key, asc));
}
 
Example #13
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
AggregationPath path() {
    return ((MultiBucketsAggregation.Bucket.SubAggregationComparator) comparator).path();
}
 
Example #14
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected Comparator<Terms.Bucket> comparator(Aggregator termsAggregator) {
    if (termsAggregator == null) {
        return comparator;
    }

    // Internal Optimization:
    //
    // in this phase, if the order is based on sub-aggregations, we need to use a different comparator
    // to avoid constructing buckets for ordering purposes (we can potentially have a lot of buckets and building
    // them will cause loads of redundant object constructions). The "special" comparators here will fetch the
    // sub aggregation values directly from the sub aggregators bypassing bucket creation. Note that the comparator
    // attached to the order will still be used in the reduce phase of the Aggregation.

    AggregationPath path = path();
    final Aggregator aggregator = path.resolveAggregator(termsAggregator);
    final String key = path.lastPathElement().key;

    if (aggregator instanceof SingleBucketAggregator) {
        assert key == null : "this should be picked up before the aggregation is executed - on validate";
        return new Comparator<Terms.Bucket>() {
            @Override
            public int compare(Terms.Bucket o1, Terms.Bucket o2) {
                int mul = asc ? 1 : -1;
                int v1 = ((SingleBucketAggregator) aggregator).bucketDocCount(((InternalTerms.Bucket) o1).bucketOrd);
                int v2 = ((SingleBucketAggregator) aggregator).bucketDocCount(((InternalTerms.Bucket) o2).bucketOrd);
                return mul * (v1 - v2);
            }
        };
    }

    // with only support single-bucket aggregators
    assert !(aggregator instanceof BucketsAggregator) : "this should be picked up before the aggregation is executed - on validate";

    if (aggregator instanceof NumericMetricsAggregator.MultiValue) {
        assert key != null : "this should be picked up before the aggregation is executed - on validate";
        return new Comparator<Terms.Bucket>() {
            @Override
            public int compare(Terms.Bucket o1, Terms.Bucket o2) {
                double v1 = ((NumericMetricsAggregator.MultiValue) aggregator).metric(key, ((InternalTerms.Bucket) o1).bucketOrd);
                double v2 = ((NumericMetricsAggregator.MultiValue) aggregator).metric(key, ((InternalTerms.Bucket) o2).bucketOrd);
                // some metrics may return NaN (eg. avg, variance, etc...) in which case we'd like to push all of those to
                // the bottom
                return Comparators.compareDiscardNaN(v1, v2, asc);
            }
        };
    }

    // single-value metrics agg
    return new Comparator<Terms.Bucket>() {
        @Override
        public int compare(Terms.Bucket o1, Terms.Bucket o2) {
            double v1 = ((NumericMetricsAggregator.SingleValue) aggregator).metric(((InternalTerms.Bucket) o1).bucketOrd);
            double v2 = ((NumericMetricsAggregator.SingleValue) aggregator).metric(((InternalTerms.Bucket) o2).bucketOrd);
            // some metrics may return NaN (eg. avg, variance, etc...) in which case we'd like to push all of those to
            // the bottom
            return Comparators.compareDiscardNaN(v1, v2, asc);
        }
    };
}
 
Example #15
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected Comparator<Bucket> comparator(Aggregator aggregator) {
    return new CompoundOrderComparator(orderElements, aggregator);
}