Search in sources :

Example 21 with Facet

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

the class FacetedQueryResultJsonSerializerV2 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 JsonArray jsonList = (JsonArray) context.serialize(cluster.getValue());
            final JsonObject jsonClusterElement = new JsonObject();
            jsonClusterElement.add(cluster.getKey().getLabel().getDisplay(), 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()) {
            final JsonObject jsonFacetValuesElement = new JsonObject();
            jsonFacetValuesElement.addProperty(entry.getKey().getLabel().getDisplay(), entry.getValue());
            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) 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