Search in sources :

Example 16 with Facet

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

the class ESFacetedQueryResultBuilder method build.

/**
 * {@inheritDoc}
 */
@Override
public FacetedQueryResult<I, SearchQuery> build() {
    final Map<I, Map<DtField, String>> resultHighlights = new HashMap<>();
    final Map<FacetValue, DtList<I>> resultCluster;
    final DtList<I> dtc = new DtList<>(indexDefinition.getIndexDtDefinition());
    if (searchQuery.isClusteringFacet()) {
        final Map<String, I> dtcIndex = new LinkedHashMap<>();
        resultCluster = createCluster(dtcIndex, resultHighlights);
        dtc.addAll(dtcIndex.values());
    } else {
        for (final SearchHit searchHit : queryResponse.getHits()) {
            final SearchIndex<?, I> index = esDocumentCodec.searchHit2Index(indexDefinition, searchHit);
            final I result = index.getIndexDtObject();
            dtc.add(result);
            final Map<DtField, String> highlights = createHighlight(searchHit, indexDefinition.getIndexDtDefinition());
            resultHighlights.put(result, highlights);
        }
        resultCluster = Collections.emptyMap();
    }
    // On fabrique à la volée le résultat.
    final List<Facet> facets = createFacetList(searchQuery, queryResponse);
    final long count = queryResponse.getHits().getTotalHits();
    return new FacetedQueryResult<>(searchQuery.getFacetedQuery(), count, dtc, facets, searchQuery.isClusteringFacet() ? Optional.of(searchQuery.getClusteringFacetDefinition()) : Optional.empty(), resultCluster, resultHighlights, searchQuery);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FacetedQueryResult(io.vertigo.dynamo.collections.model.FacetedQueryResult) LinkedHashMap(java.util.LinkedHashMap) DtField(io.vertigo.dynamo.domain.metamodel.DtField) FacetValue(io.vertigo.dynamo.collections.model.FacetValue) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DtList(io.vertigo.dynamo.domain.model.DtList) Facet(io.vertigo.dynamo.collections.model.Facet)

Example 17 with Facet

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

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

the class AbstractSearchManagerTest method createFacetQuery.

private static FacetedQuery createFacetQuery(final String facetName, final String facetValueLabel, final FacetedQueryResult<Car, ?> result) {
    // pb d'initialisation, et assert.notNull ne suffit pas
    FacetValue facetValue = null;
    final Facet facet = getFacetByName(result, facetName);
    for (final Entry<FacetValue, Long> entry : facet.getFacetValues().entrySet()) {
        if (entry.getKey().getLabel().getDisplay().toLowerCase(Locale.FRENCH).contains(facetValueLabel)) {
            facetValue = entry.getKey();
            break;
        }
    }
    if (facetValue == null) {
        throw new IllegalArgumentException("Pas de FacetValue contenant " + facetValueLabel + " dans la facette " + facetName);
    }
    final FacetedQuery previousQuery = result.getFacetedQuery().get();
    final SelectedFacetValues queryFilters = SelectedFacetValues.of(previousQuery.getSelectedFacetValues()).add(facet.getDefinition(), facetValue).build();
    return new FacetedQuery(previousQuery.getDefinition(), queryFilters);
}
Also used : FacetValue(io.vertigo.dynamo.collections.model.FacetValue) SelectedFacetValues(io.vertigo.dynamo.collections.model.SelectedFacetValues) FacetedQuery(io.vertigo.dynamo.collections.model.FacetedQuery) Facet(io.vertigo.dynamo.collections.model.Facet)

Example 19 with Facet

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

the class AbstractSearchManagerTest method testFacetResultByTerm.

private void testFacetResultByTerm(final FacetedQueryResult<Item, ?> result) {
    Assert.assertEquals(itemDataBase.size(), result.getCount());
    // On vérifie qu'il y a le bon nombre de facettes.
    Assert.assertEquals(4, result.getFacets().size());
    // On recherche la facette constructeur
    final Facet manufacturerFacet = getFacetByName(result, "FCT_MANUFACTURER_ITEM");
    // On vérifie que l'on est sur le champ Manufacturer
    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;
            Assert.assertEquals(itemDataBase.getItemsByManufacturer(manufacturer).size(), entry.getValue().intValue());
        }
    }
    Assert.assertTrue(found);
    checkOrderByCount(manufacturerFacet);
    checkOrderByAlpha(getFacetByName(result, "FCT_MANUFACTURER_ITEM_ALPHA"));
    checkOrderByCount(getFacetByName(result, "FCT_DESCRIPTION_ITEM"));
}
Also used : FacetValue(io.vertigo.dynamo.collections.model.FacetValue) Facet(io.vertigo.dynamo.collections.model.Facet)

Example 20 with Facet

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

the class AbstractSearchManagerTest method createFacetQuery.

private static FacetedQuery createFacetQuery(final String facetName, final String facetValueLabel, final FacetedQueryResult<Item, ?> result) {
    // pb d'initialisation, et assert.notNull ne suffit pas
    FacetValue facetValue = null;
    final Facet facet = getFacetByName(result, facetName);
    for (final Entry<FacetValue, Long> entry : facet.getFacetValues().entrySet()) {
        if (entry.getKey().getLabel().getDisplay().toLowerCase(Locale.FRENCH).contains(facetValueLabel)) {
            facetValue = entry.getKey();
            break;
        }
    }
    if (facetValue == null) {
        throw new IllegalArgumentException("Pas de FacetValue contenant " + facetValueLabel + " dans la facette " + facetName);
    }
    final FacetedQuery previousQuery = result.getFacetedQuery().get();
    final SelectedFacetValues queryFilters = SelectedFacetValues.of(previousQuery.getSelectedFacetValues()).add(facet.getDefinition(), facetValue).build();
    return new FacetedQuery(previousQuery.getDefinition(), queryFilters);
}
Also used : FacetValue(io.vertigo.dynamo.collections.model.FacetValue) SelectedFacetValues(io.vertigo.dynamo.collections.model.SelectedFacetValues) FacetedQuery(io.vertigo.dynamo.collections.model.FacetedQuery) Facet(io.vertigo.dynamo.collections.model.Facet)

Aggregations

Facet (io.vertigo.dynamo.collections.model.Facet)21 FacetValue (io.vertigo.dynamo.collections.model.FacetValue)19 LinkedHashMap (java.util.LinkedHashMap)6 JsonArray (com.google.gson.JsonArray)4 JsonObject (com.google.gson.JsonObject)4 DtList (io.vertigo.dynamo.domain.model.DtList)4 ArrayList (java.util.ArrayList)4 Bucket (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket)4 FacetedQuery (io.vertigo.dynamo.collections.model.FacetedQuery)3 FacetedQueryResult (io.vertigo.dynamo.collections.model.FacetedQueryResult)3 SelectedFacetValues (io.vertigo.dynamo.collections.model.SelectedFacetValues)3 MessageText (io.vertigo.core.locale.MessageText)2 FacetDefinition (io.vertigo.dynamo.collections.metamodel.FacetDefinition)2 FacetedQueryDefinition (io.vertigo.dynamo.collections.metamodel.FacetedQueryDefinition)2 DtField (io.vertigo.dynamo.domain.metamodel.DtField)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 SearchHit (org.elasticsearch.search.SearchHit)2 Aggregation (org.elasticsearch.search.aggregations.Aggregation)2 MultiBucketsAggregation (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)2