Search in sources :

Example 21 with FacetValue

use of io.vertigo.dynamo.collections.model.FacetValue 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 22 with FacetValue

use of io.vertigo.dynamo.collections.model.FacetValue in project vertigo by KleeGroup.

the class ESSearchRequestBuilder method numberRangeFacetToAggregationBuilder.

private static AggregationBuilder numberRangeFacetToAggregationBuilder(final FacetDefinition facetDefinition, final DtField dtField) {
    final RangeAggregationBuilder rangeBuilder = // 
    AggregationBuilders.range(facetDefinition.getName()).field(dtField.getName());
    for (final FacetValue facetRange : facetDefinition.getFacetRanges()) {
        final String filterValue = facetRange.getListFilter().getFilterValue();
        Assertion.checkState(filterValue.contains(dtField.getName()), "RangeFilter query ({1}) should use defined fieldName {0}", dtField.getName(), filterValue);
        final String[] parsedFilter = DtListPatternFilterUtil.parseFilter(filterValue, RANGE_PATTERN).get();
        final Optional<Double> minValue = convertToDouble(parsedFilter[3]);
        final Optional<Double> maxValue = convertToDouble(parsedFilter[4]);
        if (!minValue.isPresent()) {
            rangeBuilder.addUnboundedTo(filterValue, maxValue.get());
        } else if (!maxValue.isPresent()) {
            rangeBuilder.addUnboundedFrom(filterValue, minValue.get());
        } else {
            // always min include and max exclude in ElasticSearch
            rangeBuilder.addRange(filterValue, minValue.get(), maxValue.get());
        }
    }
    return rangeBuilder;
}
Also used : FacetValue(io.vertigo.dynamo.collections.model.FacetValue) RangeAggregationBuilder(org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder) DateRangeAggregationBuilder(org.elasticsearch.search.aggregations.bucket.range.date.DateRangeAggregationBuilder)

Example 23 with FacetValue

use of io.vertigo.dynamo.collections.model.FacetValue in project vertigo by KleeGroup.

the class ESSearchRequestBuilder method rangeFacetToAggregationBuilder.

private static AggregationBuilder rangeFacetToAggregationBuilder(final FacetDefinition facetDefinition, final DtField dtField) {
    // facette par range
    final DataType dataType = dtField.getDomain().getDataType();
    if (dataType == DataType.Date) {
        return dateRangeFacetToAggregationBuilder(facetDefinition, dtField);
    } else if (dataType.isNumber()) {
        return numberRangeFacetToAggregationBuilder(facetDefinition, dtField);
    }
    final List<KeyedFilter> filters = new ArrayList<>();
    for (final FacetValue facetRange : facetDefinition.getFacetRanges()) {
        final String filterValue = facetRange.getListFilter().getFilterValue();
        Assertion.checkState(filterValue.contains(dtField.getName()), "RangeFilter query ({1}) should use defined fieldName {0}", dtField.getName(), filterValue);
        filters.add(new KeyedFilter(filterValue, QueryBuilders.queryStringQuery(filterValue)));
    }
    return AggregationBuilders.filters(facetDefinition.getName(), filters.toArray(new KeyedFilter[filters.size()]));
}
Also used : KeyedFilter(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter) FacetValue(io.vertigo.dynamo.collections.model.FacetValue) ArrayList(java.util.ArrayList) DataType(io.vertigo.dynamo.domain.metamodel.DataType)

Example 24 with FacetValue

use of io.vertigo.dynamo.collections.model.FacetValue in project vertigo by KleeGroup.

the class ESSearchRequestBuilder method dateRangeFacetToAggregationBuilder.

private static AggregationBuilder dateRangeFacetToAggregationBuilder(final FacetDefinition facetDefinition, final DtField dtField) {
    final DateRangeAggregationBuilder dateRangeBuilder = AggregationBuilders.dateRange(facetDefinition.getName()).field(dtField.getName()).format(DATE_PATTERN);
    for (final FacetValue facetRange : facetDefinition.getFacetRanges()) {
        final String filterValue = facetRange.getListFilter().getFilterValue();
        Assertion.checkState(filterValue.contains(dtField.getName()), "RangeFilter query ({1}) should use defined fieldName {0}", dtField.getName(), filterValue);
        final String[] parsedFilter = DtListPatternFilterUtil.parseFilter(filterValue, RANGE_PATTERN).get();
        final String minValue = parsedFilter[3];
        final String maxValue = parsedFilter[4];
        if ("*".equals(minValue)) {
            dateRangeBuilder.addUnboundedTo(filterValue, maxValue);
        } else if ("*".equals(maxValue)) {
            dateRangeBuilder.addUnboundedFrom(filterValue, minValue);
        } else {
            // always min include and max exclude in ElasticSearch
            dateRangeBuilder.addRange(filterValue, minValue, maxValue);
        }
    }
    return dateRangeBuilder;
}
Also used : DateRangeAggregationBuilder(org.elasticsearch.search.aggregations.bucket.range.date.DateRangeAggregationBuilder) FacetValue(io.vertigo.dynamo.collections.model.FacetValue)

Example 25 with FacetValue

use of io.vertigo.dynamo.collections.model.FacetValue in project vertigo by KleeGroup.

the class FacetManagerTest method testFacetResultByTerm.

private void testFacetResultByTerm(final FacetedQueryResult<SmartCar, ?> result) {
    Assert.assertEquals(smartCarDataBase.size(), result.getCount());
    // On vérifie qu'il y a le bon nombre de facettes.
    Assert.assertEquals(3, result.getFacets().size());
    // On recherche la facette constructeur
    final Facet manufacturerFacet = getFacetByName(result, "FCT_MANUFACTURER_CAR");
    // On vérifie que l'on est sur le champ Make
    Assert.assertEquals("MANUFACTURER", manufacturerFacet.getDefinition().getDtField().getName());
    Assert.assertFalse(manufacturerFacet.getDefinition().isRangeFacet());
    // On vérifie qu'il existe une valeur pour peugeot et que le nombre d'occurrences est correct
    boolean found = false;
    final String manufacturer = "peugeot";
    for (final Entry<FacetValue, Long> entry : manufacturerFacet.getFacetValues().entrySet()) {
        if (entry.getKey().getLabel().getDisplay().toLowerCase(Locale.FRENCH).equals(manufacturer)) {
            found = true;
            // System.out.println("manufacturer" + entry.getKey().getLabel().getDisplay());
            Assert.assertEquals(smartCarDataBase.getCarsByManufacturer(manufacturer).size(), entry.getValue().intValue());
        }
    }
    Assert.assertTrue(found);
}
Also used : FacetValue(io.vertigo.dynamo.collections.model.FacetValue) Facet(io.vertigo.dynamo.collections.model.Facet)

Aggregations

FacetValue (io.vertigo.dynamo.collections.model.FacetValue)44 Facet (io.vertigo.dynamo.collections.model.Facet)24 DtList (io.vertigo.dynamo.domain.model.DtList)17 HashMap (java.util.HashMap)13 ArrayList (java.util.ArrayList)12 FacetDefinition (io.vertigo.dynamo.collections.metamodel.FacetDefinition)11 LinkedHashMap (java.util.LinkedHashMap)10 MessageText (io.vertigo.core.locale.MessageText)9 List (java.util.List)9 Map (java.util.Map)9 ListFilter (io.vertigo.dynamo.collections.ListFilter)8 FacetedQuery (io.vertigo.dynamo.collections.model.FacetedQuery)8 FacetedQueryResult (io.vertigo.dynamo.collections.model.FacetedQueryResult)8 SelectedFacetValues (io.vertigo.dynamo.collections.model.SelectedFacetValues)8 SearchQuery (io.vertigo.dynamo.search.model.SearchQuery)8 Test (org.junit.Test)8 DtListState (io.vertigo.dynamo.domain.model.DtListState)6 Bucket (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket)6 FacetedQueryDefinition (io.vertigo.dynamo.collections.metamodel.FacetedQueryDefinition)5 DtField (io.vertigo.dynamo.domain.metamodel.DtField)5