use of org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder in project alien4cloud by alien4cloud.
the class AbstractToscaIndexSearchService method search.
public FacetedSearchResult search(Class<? extends T> clazz, String query, Integer size, Map<String, String[]> filters) {
TopHitsBuilder topHitAggregation = AggregationBuilders.topHits("highest_version").setSize(1).addSort(new FieldSortBuilder("nestedVersion.majorVersion").order(SortOrder.DESC)).addSort(new FieldSortBuilder("nestedVersion.minorVersion").order(SortOrder.DESC)).addSort(new FieldSortBuilder("nestedVersion.incrementalVersion").order(SortOrder.DESC)).addSort(new FieldSortBuilder("nestedVersion.qualifier").order(SortOrder.DESC).missing("_first"));
AggregationBuilder aggregation = AggregationBuilders.terms("query_aggregation").field(getAggregationField()).size(size).subAggregation(topHitAggregation);
FacetedSearchResult<? extends T> searchResult = alienDAO.buildSearchQuery(clazz, query).setFilters(FilterUtil.singleKeyFilter(filters, "workspace", AlienConstants.GLOBAL_WORKSPACE_ID)).prepareSearch().setFetchContext(FetchContext.SUMMARY, topHitAggregation).facetedSearch(new IAggregationQueryManager() {
@Override
public AggregationBuilder getQueryAggregation() {
return aggregation;
}
@Override
@SneakyThrows({ IOException.class })
public void setData(ObjectMapper objectMapper, Function getClassFromType, FacetedSearchResult result, Aggregation aggregation) {
List<Object> resultData = Lists.newArrayList();
List<String> resultTypes = Lists.newArrayList();
if (aggregation == null) {
result.setData(getArray(0));
result.setTypes(new String[0]);
}
for (Terms.Bucket bucket : safe(((Terms) aggregation).getBuckets())) {
TopHits topHits = bucket.getAggregations().get("highest_version");
for (SearchHit hit : topHits.getHits()) {
resultTypes.add(hit.getType());
resultData.add(objectMapper.readValue(hit.getSourceAsString(), ((Function<String, Class>) getClassFromType).apply(hit.getType())));
}
}
result.setData(resultData.toArray(getArray(resultData.size())));
result.setTypes(resultTypes.toArray(new String[resultTypes.size()]));
result.setFrom(0);
result.setTo(resultData.size());
if (size == Integer.MAX_VALUE || resultData.size() < size) {
result.setTotalResults(resultData.size());
} else {
// just to show that there is more results to fetch but iteration is not possible through aggregations.
result.setTotalResults(resultData.size() + ((Terms) aggregation).getSumOfOtherDocCounts());
}
}
});
return searchResult;
}
use of org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder in project sonarqube by SonarSource.
the class ComponentIndex method createSubAggregation.
private static TopHitsBuilder createSubAggregation(ComponentIndexQuery query) {
TopHitsBuilder sub = AggregationBuilders.topHits(DOCS_AGGREGATION_NAME);
query.getLimit().ifPresent(sub::setSize);
return sub.setFetchSource(false);
}
use of org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder in project vertigo by KleeGroup.
the class ESSearchRequestBuilder method appendFacetDefinition.
private static void appendFacetDefinition(final SearchQuery searchQuery, final SearchRequestBuilder searchRequestBuilder, final SearchIndexDefinition myIndexDefinition, final DtListState myListState) {
Assertion.checkNotNull(searchRequestBuilder);
// On ajoute le cluster, si présent
if (searchQuery.isClusteringFacet()) {
// si il y a un cluster on le place en premier
final FacetDefinition clusteringFacetDefinition = searchQuery.getClusteringFacetDefinition();
final AggregationBuilder<?> aggregationBuilder = facetToAggregationBuilder(clusteringFacetDefinition);
final TopHitsBuilder topHitsBuilder = AggregationBuilders.topHits(TOPHITS_SUBAGGREGATION_NAME).setSize(myListState.getMaxRows().orElse(TOPHITS_SUBAGGREGATION_SIZE)).setFrom(myListState.getSkipRows()).setHighlighterNumOfFragments(3).addHighlightedField("*");
if (myListState.getSortFieldName().isPresent()) {
topHitsBuilder.addSort(getFieldSortBuilder(myIndexDefinition, myListState));
}
aggregationBuilder.subAggregation(topHitsBuilder);
// We fetch source, because it's our only source to create result list
searchRequestBuilder.addAggregation(aggregationBuilder);
}
// Puis les facettes liées à la query, si présent
if (searchQuery.getFacetedQuery().isPresent()) {
final FacetedQueryDefinition facetedQueryDefinition = searchQuery.getFacetedQuery().get().getDefinition();
final Collection<FacetDefinition> facetDefinitions = new ArrayList<>(facetedQueryDefinition.getFacetDefinitions());
if (searchQuery.isClusteringFacet() && facetDefinitions.contains(searchQuery.getClusteringFacetDefinition())) {
facetDefinitions.remove(searchQuery.getClusteringFacetDefinition());
}
for (final FacetDefinition facetDefinition : facetDefinitions) {
final AggregationBuilder<?> aggregationBuilder = facetToAggregationBuilder(facetDefinition);
searchRequestBuilder.addAggregation(aggregationBuilder);
}
}
}
Aggregations