Search in sources :

Example 6 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 = obtainAggregation(queryResponse, 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 7 with Facet

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

the class FacetManagerTest method addFacetQuery.

private static FacetedQuery addFacetQuery(final String facetName, final String facetValueLabel, final FacetedQueryResult<SmartCar, ?> result) {
    // pb d'initialisation, et assert.notNull ne suffit pas
    FacetValue facetFilter = null;
    final Facet yearFacet = getFacetByName(result, facetName);
    for (final Entry<FacetValue, Long> entry : yearFacet.getFacetValues().entrySet()) {
        if (entry.getKey().getLabel().getDisplay().toLowerCase(Locale.FRENCH).contains(facetValueLabel)) {
            facetFilter = entry.getKey();
            break;
        }
    }
    if (facetFilter == 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(yearFacet.getDefinition(), facetFilter).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 8 with Facet

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

the class FacetedQueryResultJsonSerializerV1 method serialize.

/**
 * {@inheritDoc}
 */
@Override
public JsonElement serialize(final FacetedQueryResult<?, ?> facetedQueryResult, final Type typeOfSrc, final JsonSerializationContext context) {
    final JsonObject jsonObject = new JsonObject();
    // 1- add result list as data
    if (facetedQueryResult.getClusters().isEmpty()) {
        final JsonArray jsonList = (JsonArray) context.serialize(facetedQueryResult.getDtList());
        jsonObject.add("list", jsonList);
    } else {
        // if it's a cluster add data's cluster
        final JsonObject jsonCluster = new JsonObject();
        for (final Entry<FacetValue, ?> cluster : facetedQueryResult.getClusters().entrySet()) {
            final JsonArray jsonList = (JsonArray) context.serialize(cluster.getValue());
            jsonCluster.add(cluster.getKey().getLabel().getDisplay(), jsonList);
        }
        jsonObject.add("groups", jsonCluster);
    }
    // 2- add facet list as facets
    final List<Facet> facets = facetedQueryResult.getFacets();
    final JsonObject jsonFacet = new JsonObject();
    for (final Facet facet : facets) {
        final JsonObject jsonFacetValues = new JsonObject();
        facet.getFacetValues().forEach((k, v) -> jsonFacetValues.addProperty(k.getLabel().getDisplay(), v));
        final String facetName = facet.getDefinition().getName();
        jsonFacet.add(facetName, jsonFacetValues);
    }
    jsonObject.add("facets", jsonFacet);
    // 3 -add totalCount
    jsonObject.addProperty(DtList.TOTAL_COUNT_META, facetedQueryResult.getCount());
    return jsonObject;
}
Also used : JsonArray(com.google.gson.JsonArray) FacetValue(io.vertigo.dynamo.collections.model.FacetValue) JsonObject(com.google.gson.JsonObject) Facet(io.vertigo.dynamo.collections.model.Facet)

Example 9 with Facet

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

the class FacetedQueryResultJsonSerializerV3 method serialize.

/**
 * {@inheritDoc}
 */
@Override
public JsonElement serialize(final FacetedQueryResult<?, ?> facetedQueryResult, final Type typeOfSrc, final JsonSerializationContext context) {
    final JsonObject jsonObject = new JsonObject();
    // 1- add result list as data
    if (facetedQueryResult.getClusters().isEmpty()) {
        final JsonArray jsonList = (JsonArray) context.serialize(facetedQueryResult.getDtList());
        jsonObject.add("list", jsonList);
    } else {
        // if it's a cluster add data's cluster
        final JsonArray jsonCluster = new JsonArray();
        for (final Entry<FacetValue, ?> cluster : facetedQueryResult.getClusters().entrySet()) {
            final DtList<?> dtList = (DtList<?>) cluster.getValue();
            if (!dtList.isEmpty()) {
                final JsonArray jsonList = (JsonArray) context.serialize(dtList);
                final JsonObject jsonClusterElement = new JsonObject();
                jsonClusterElement.addProperty("code", cluster.getKey().getCode());
                jsonClusterElement.addProperty("label", cluster.getKey().getLabel().getDisplay());
                jsonClusterElement.add("list", jsonList);
                jsonCluster.add(jsonClusterElement);
            }
        }
        jsonObject.add("groups", jsonCluster);
    }
    // 2- add facet list as facets
    final List<Facet> facets = facetedQueryResult.getFacets();
    final JsonArray jsonFacet = new JsonArray();
    for (final Facet facet : facets) {
        final JsonArray jsonFacetValues = new JsonArray();
        for (final Entry<FacetValue, Long> entry : facet.getFacetValues().entrySet()) {
            if (entry.getValue() > 0) {
                final JsonObject jsonFacetValuesElement = new JsonObject();
                jsonFacetValuesElement.addProperty("code", entry.getKey().getCode());
                jsonFacetValuesElement.addProperty("count", entry.getValue());
                jsonFacetValuesElement.addProperty("label", entry.getKey().getLabel().getDisplay());
                jsonFacetValues.add(jsonFacetValuesElement);
            }
        }
        final String facetName = facet.getDefinition().getName();
        final JsonObject jsonFacetElement = new JsonObject();
        jsonFacetElement.add(facetName, jsonFacetValues);
        jsonFacet.add(jsonFacetElement);
    }
    jsonObject.add("facets", jsonFacet);
    // 3 -add totalCount
    jsonObject.addProperty(DtList.TOTAL_COUNT_META, facetedQueryResult.getCount());
    return jsonObject;
}
Also used : JsonArray(com.google.gson.JsonArray) FacetValue(io.vertigo.dynamo.collections.model.FacetValue) JsonObject(com.google.gson.JsonObject) DtList(io.vertigo.dynamo.domain.model.DtList) Facet(io.vertigo.dynamo.collections.model.Facet)

Example 10 with Facet

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

the class FacetedQueryResultJsonSerializerV4 method serialize.

/**
 * {@inheritDoc}
 */
@Override
public JsonElement serialize(final FacetedQueryResult<?, ?> facetedQueryResult, final Type typeOfSrc, final JsonSerializationContext context) {
    final JsonObject jsonObject = new JsonObject();
    // 1- add result list as data, with highlight
    if (!facetedQueryResult.getClusterFacetDefinition().isPresent()) {
        final DtList<?> dtList = facetedQueryResult.getDtList();
        final JsonArray jsonList = (JsonArray) context.serialize(dtList);
        jsonObject.add("list", jsonList);
        jsonObject.addProperty("listType", dtList.getDefinition().getClassSimpleName());
        jsonObject.add("highlight", serializeHighLight(dtList, (FacetedQueryResult) facetedQueryResult));
    } else {
        // if it's a cluster add data's cluster
        final JsonArray jsonCluster = new JsonArray();
        for (final Entry<FacetValue, ?> cluster : facetedQueryResult.getClusters().entrySet()) {
            final DtList<?> dtList = (DtList<?>) cluster.getValue();
            if (!dtList.isEmpty()) {
                final JsonArray jsonList = (JsonArray) context.serialize(dtList);
                final JsonObject jsonClusterElement = new JsonObject();
                jsonClusterElement.addProperty("code", cluster.getKey().getCode());
                jsonClusterElement.addProperty("label", cluster.getKey().getLabel().getDisplay());
                jsonClusterElement.add("list", jsonList);
                jsonClusterElement.addProperty("listType", dtList.getDefinition().getClassSimpleName());
                jsonClusterElement.addProperty("totalCount", getFacetCount(cluster.getKey(), facetedQueryResult));
                jsonClusterElement.add("highlight", serializeHighLight(dtList, (FacetedQueryResult) facetedQueryResult));
                jsonCluster.add(jsonClusterElement);
            }
        }
        jsonObject.add("groups", jsonCluster);
    }
    // 2- add facet list as facets
    final List<Facet> facets = facetedQueryResult.getFacets();
    final JsonArray jsonFacet = new JsonArray();
    for (final Facet facet : facets) {
        final JsonArray jsonFacetValues = new JsonArray();
        for (final Entry<FacetValue, Long> entry : facet.getFacetValues().entrySet()) {
            if (entry.getValue() > 0) {
                final JsonObject jsonFacetValuesElement = new JsonObject();
                jsonFacetValuesElement.addProperty("code", entry.getKey().getCode());
                jsonFacetValuesElement.addProperty("count", entry.getValue());
                jsonFacetValuesElement.addProperty("label", entry.getKey().getLabel().getDisplay());
                jsonFacetValues.add(jsonFacetValuesElement);
            }
        }
        final JsonObject jsonFacetElement = new JsonObject();
        jsonFacetElement.addProperty("code", facet.getDefinition().getName());
        jsonFacetElement.addProperty("label", facet.getDefinition().getLabel().getDisplay());
        jsonFacetElement.add("values", jsonFacetValues);
        jsonFacet.add(jsonFacetElement);
    }
    jsonObject.add("facets", jsonFacet);
    // 3 -add totalCount
    jsonObject.addProperty(DtList.TOTAL_COUNT_META, facetedQueryResult.getCount());
    return jsonObject;
}
Also used : JsonArray(com.google.gson.JsonArray) FacetValue(io.vertigo.dynamo.collections.model.FacetValue) FacetedQueryResult(io.vertigo.dynamo.collections.model.FacetedQueryResult) JsonObject(com.google.gson.JsonObject) DtList(io.vertigo.dynamo.domain.model.DtList) 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