use of com.b2international.index.aggregations.Aggregation in project snow-owl by b2ihealthcare.
the class EsDocumentSearcher method aggregate.
@Override
public <T> Aggregation<T> aggregate(AggregationBuilder<T> aggregation) throws IOException {
final String aggregationName = aggregation.getName();
final EsClient client = admin.client();
final DocumentMapping mapping = admin.mappings().getMapping(aggregation.getFrom());
final EsQueryBuilder esQueryBuilder = new EsQueryBuilder(mapping, admin.settings(), admin.log());
final QueryBuilder esQuery = esQueryBuilder.build(aggregation.getQuery());
final SearchRequest req = new SearchRequest(admin.getTypeIndex(mapping));
final SearchSourceBuilder reqSource = req.source().query(esQuery).size(0).trackScores(false).trackTotalHitsUpTo(Integer.MAX_VALUE);
// field selection
final boolean fetchSource = applySourceFiltering(aggregation.getFields(), mapping, reqSource);
reqSource.aggregation(toEsAggregation(mapping, aggregation, fetchSource));
SearchResponse response = null;
try {
response = client.search(req);
} catch (Exception e) {
admin.log().error("Couldn't execute aggregation", e);
throw new IndexException("Couldn't execute aggregation: " + e.getMessage(), null);
}
ImmutableMap.Builder<Object, Bucket<T>> buckets = ImmutableMap.builder();
Aggregations topLevelAggregations = response.getAggregations();
Nested nested = topLevelAggregations.get(nestedAggName(aggregation));
Terms aggregationResult;
if (nested != null) {
aggregationResult = nested.getAggregations().get(aggregationName);
} else {
aggregationResult = topLevelAggregations.get(aggregationName);
}
for (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket bucket : aggregationResult.getBuckets()) {
final TopHits topHits;
if (nested != null) {
final ReverseNested reverseNested = bucket.getAggregations().get(reverseNestedAggName(aggregation));
topHits = reverseNested.getAggregations().get(topHitsAggName(aggregation));
} else {
topHits = bucket.getAggregations().get(topHitsAggName(aggregation));
}
Hits<T> hits;
if (topHits != null) {
hits = toHits(aggregation.getSelect(), List.of(aggregation.getFrom()), aggregation.getFields(), fetchSource, aggregation.getBucketHitsLimit(), (int) bucket.getDocCount(), null, topHits.getHits());
} else {
hits = new Hits<>(Collections.emptyList(), null, aggregation.getBucketHitsLimit(), (int) bucket.getDocCount());
}
buckets.put(bucket.getKey(), new Bucket<>(bucket.getKey(), hits));
}
return new Aggregation<>(aggregationName, buckets.build());
}
use of com.b2international.index.aggregations.Aggregation in project snow-owl by b2ihealthcare.
the class RevisionIndexReadRequest method execute.
@Override
public B execute(final BranchContext context) {
final String branchPath = context.path();
RevisionIndex index = context.service(RevisionIndex.class);
if (snapshot) {
return index.read(branchPath, searcher -> {
try {
return next(context.inject().bind(RevisionSearcher.class, searcher).build());
} catch (QueryParseException e) {
throw new IllegalQueryParameterException(e.getMessage());
}
});
} else {
return next(context.inject().bind(RevisionSearcher.class, new RevisionSearcher() {
@Override
public <T> Aggregation<T> aggregate(AggregationBuilder<T> aggregation) throws IOException {
return index.read(branchPath, searcher -> searcher.aggregate(aggregation));
}
@Override
public Searcher searcher() {
return index.read(branchPath, searcher -> searcher.searcher());
}
@Override
public <T> Hits<T> search(Query<T> query) throws IOException {
return index.read(branchPath, searcher -> searcher.search(query));
}
@Override
public <T> Iterable<T> get(Class<T> type, Iterable<String> keys) throws IOException {
return index.read(branchPath, searcher -> searcher.get(type, keys));
}
@Override
public <T> T get(Class<T> type, String key) throws IOException {
return index.read(branchPath, searcher -> searcher.get(type, key));
}
@Override
public String branch() {
return branchPath;
}
}).build());
}
}
Aggregations