use of io.vertigo.dynamo.collections.metamodel.FacetDefinition 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;
}
use of io.vertigo.dynamo.collections.metamodel.FacetDefinition 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, final boolean useHighlight) {
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 TopHitsAggregationBuilder topHitsBuilder = AggregationBuilders.topHits(TOPHITS_SUBAGGREGATION_NAME).size(myListState.getMaxRows().orElse(TOPHITS_SUBAGGREGATION_SIZE)).from(myListState.getSkipRows());
if (useHighlight) {
// .addHighlightedField("*"); HOW TO ?
topHitsBuilder.highlighter(new HighlightBuilder().numOfFragments(3));
}
if (myListState.getSortFieldName().isPresent()) {
topHitsBuilder.sort(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);
final BoolQueryBuilder aggsFilterBoolQueryBuilder = QueryBuilders.boolQuery();
for (final FacetDefinition filterFacetDefinition : searchQuery.getFacetedQuery().get().getDefinition().getFacetDefinitions()) {
if (filterFacetDefinition.isMultiSelectable() && !facetDefinition.equals(filterFacetDefinition)) {
// on ne doit refiltrer que les multiSelectable (les autres sont dans le filter de la request), sauf la facet qu'on est entrain de traiter
appendSelectedFacetValuesFilter(aggsFilterBoolQueryBuilder, searchQuery.getFacetedQuery().get().getSelectedFacetValues().getFacetValues(filterFacetDefinition));
}
}
if (aggsFilterBoolQueryBuilder.hasClauses()) {
final AggregationBuilder filterAggregationBuilder = AggregationBuilders.filter(facetDefinition.getName() + "_FILTER", aggsFilterBoolQueryBuilder);
filterAggregationBuilder.subAggregation(aggregationBuilder);
searchRequestBuilder.addAggregation(filterAggregationBuilder);
} else {
searchRequestBuilder.addAggregation(aggregationBuilder);
}
}
}
}
use of io.vertigo.dynamo.collections.metamodel.FacetDefinition in project vertigo by KleeGroup.
the class ESFacetedQueryResultBuilder method createFacetList.
private static List<Facet> createFacetList(final SearchQuery searchQuery, final SearchResponse queryResponse) {
final List<Facet> facets = new ArrayList<>();
if (searchQuery.getFacetedQuery().isPresent() && queryResponse.getAggregations() != null) {
final FacetedQueryDefinition queryDefinition = searchQuery.getFacetedQuery().get().getDefinition();
for (final FacetDefinition facetDefinition : queryDefinition.getFacetDefinitions()) {
final Aggregation aggregation = queryResponse.getAggregations().get(facetDefinition.getName());
if (aggregation != null) {
final Facet facet = createFacet(facetDefinition, (MultiBucketsAggregation) aggregation);
facets.add(facet);
}
}
}
return facets;
}
use of io.vertigo.dynamo.collections.metamodel.FacetDefinition 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);
}
}
}
use of io.vertigo.dynamo.collections.metamodel.FacetDefinition in project vertigo by KleeGroup.
the class FacetedQueryResultMerger method toFacetedQueryResult.
/**
* Merge data.
* @return get merged result as a new result
*/
public FacetedQueryResult<R, S> toFacetedQueryResult() {
Assertion.checkArgument(otherResults.size() > 0, "You need at least one FacetedQueryResult in order to build a FacetedQueryResult");
// On accepte de ne pas avoir de FacetedQueryResults pour les cas ou les resultats sont filtrés par la sécurité, certains éléments à merger sont peut-être absent.
// -----
long totalCount = 0;
final Map<FacetValue, DtList<R>> clustersDtc = new LinkedHashMap<>(otherResults.size());
final Map<FacetValue, Long> clustersCount = new LinkedHashMap<>(otherResults.size());
final List<Facet> facets = new ArrayList<>();
final Map<R, Map<DtField, String>> highlights = Collections.emptyMap();
for (final Entry<FacetValue, List<FacetedQueryResult<?, S>>> otherResult : otherResults.entrySet()) {
long clusterCount = 0;
// merge count
for (final FacetedQueryResult<?, S> result : otherResult.getValue()) {
clusterCount += result.getCount();
}
// cluster result
final DtList clusterDtList = new DtList(otherResult.getValue().get(0).getDtList().getDefinition());
for (final FacetedQueryResult<?, S> result : otherResult.getValue()) {
clusterDtList.addAll(result.getDtList());
}
clustersDtc.put(otherResult.getKey(), clusterDtList);
clustersCount.put(otherResult.getKey(), clusterCount);
totalCount += clusterCount;
// TODO merge facets
// TODO merge highlights
}
// On garde les infos qui sont basés sur le premier élément
final Optional<FacetedQuery> facetedQueryOpt = firstResult.getFacetedQuery();
// faux : le type de la liste est incorrect, mais heureusement elle est vide.
final DtList<R> results = new DtList<>(firstResult.getDtList().getDefinition());
final S source = firstResult.getSource();
final Optional<FacetDefinition> clusterFacetDefinitionOpt;
if (facetDefinitionNameOpt.isPresent()) {
final FacetDefinition clusterFacetDefinition = FacetDefinition.createFacetDefinitionByTerm(facetDefinitionNameOpt.get(), results.getDefinition().getFields().get(0), MessageText.of("cluster"), false, FacetOrder.definition);
final Facet clusterFacet = new Facet(clusterFacetDefinition, clustersCount);
facets.add(clusterFacet);
clusterFacetDefinitionOpt = Optional.of(clusterFacetDefinition);
} else {
clusterFacetDefinitionOpt = Optional.empty();
}
return new FacetedQueryResult<>(facetedQueryOpt, totalCount, results, facets, clusterFacetDefinitionOpt, clustersDtc, highlights, source);
}
Aggregations