Search in sources :

Example 6 with FacetDefinition

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;
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) FacetValue(io.vertigo.dynamo.collections.model.FacetValue) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) MessageText(io.vertigo.core.locale.MessageText) FacetDefinition(io.vertigo.dynamo.collections.metamodel.FacetDefinition) DtList(io.vertigo.dynamo.domain.model.DtList) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with FacetDefinition

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);
            }
        }
    }
}
Also used : AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) RangeAggregationBuilder(org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder) DateRangeAggregationBuilder(org.elasticsearch.search.aggregations.bucket.range.date.DateRangeAggregationBuilder) TopHitsAggregationBuilder(org.elasticsearch.search.aggregations.metrics.tophits.TopHitsAggregationBuilder) TopHitsAggregationBuilder(org.elasticsearch.search.aggregations.metrics.tophits.TopHitsAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ArrayList(java.util.ArrayList) FacetDefinition(io.vertigo.dynamo.collections.metamodel.FacetDefinition) FacetedQueryDefinition(io.vertigo.dynamo.collections.metamodel.FacetedQueryDefinition) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)

Example 8 with FacetDefinition

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;
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) ArrayList(java.util.ArrayList) FacetDefinition(io.vertigo.dynamo.collections.metamodel.FacetDefinition) FacetedQueryDefinition(io.vertigo.dynamo.collections.metamodel.FacetedQueryDefinition) Facet(io.vertigo.dynamo.collections.model.Facet)

Example 9 with FacetDefinition

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);
        }
    }
}
Also used : TopHitsBuilder(org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder) ArrayList(java.util.ArrayList) FacetDefinition(io.vertigo.dynamo.collections.metamodel.FacetDefinition) FacetedQueryDefinition(io.vertigo.dynamo.collections.metamodel.FacetedQueryDefinition)

Example 10 with FacetDefinition

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);
}
Also used : ArrayList(java.util.ArrayList) FacetDefinition(io.vertigo.dynamo.collections.metamodel.FacetDefinition) LinkedHashMap(java.util.LinkedHashMap) DtList(io.vertigo.dynamo.domain.model.DtList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DtList(io.vertigo.dynamo.domain.model.DtList)

Aggregations

FacetDefinition (io.vertigo.dynamo.collections.metamodel.FacetDefinition)15 FacetValue (io.vertigo.dynamo.collections.model.FacetValue)8 ArrayList (java.util.ArrayList)7 FacetedQueryDefinition (io.vertigo.dynamo.collections.metamodel.FacetedQueryDefinition)6 MessageText (io.vertigo.core.locale.MessageText)5 Map (java.util.Map)5 DtList (io.vertigo.dynamo.domain.model.DtList)4 HashMap (java.util.HashMap)4 Aggregation (org.elasticsearch.search.aggregations.Aggregation)4 MultiBucketsAggregation (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)4 ListFilter (io.vertigo.dynamo.collections.ListFilter)3 Facet (io.vertigo.dynamo.collections.model.Facet)3 DtField (io.vertigo.dynamo.domain.metamodel.DtField)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 SelectedFacetValuesBuilder (io.vertigo.dynamo.collections.model.SelectedFacetValues.SelectedFacetValuesBuilder)2 DtDefinition (io.vertigo.dynamo.domain.metamodel.DtDefinition)2 DslDefinition (io.vertigo.dynamo.plugins.environment.dsl.dynamic.DslDefinition)2 Assertion (io.vertigo.lang.Assertion)2 Bucket (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket)2