Search in sources :

Example 1 with FacetedQueryResult

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

the class AbstractSearchManagerTest method testSortedClusterByFacetTerm.

/**
 * Test le facettage par term d'une liste.
 */
@Test
public void testSortedClusterByFacetTerm() {
    index(true);
    final SearchQuery searchQuery = SearchQuery.builder(ListFilter.of("*:*")).withFacetClustering(makeFacetDefinition).build();
    final DtListState listState = new DtListState(null, 0, carIndexDefinition.getIndexDtDefinition().getField("YEAR").getName(), true);
    final FacetedQueryResult<Car, SearchQuery> result = searchManager.loadList(carIndexDefinition, searchQuery, listState);
    // On vérifie qu'il existe une valeur pour chaque marques et que la première est bien la plus ancienne
    final Map<String, Set<Car>> databaseCluster = new HashMap<>();
    for (final Car car : carDataBase.getAllCars()) {
        databaseCluster.computeIfAbsent(car.getMake().toLowerCase(Locale.FRENCH), k -> new TreeSet<>((e1, e2) -> e2.getYear().compareTo(e1.getYear()))).add(car);
    }
    Assert.assertEquals(databaseCluster.size(), result.getClusters().size());
    for (final Entry<FacetValue, DtList<Car>> entry : result.getClusters().entrySet()) {
        final String searchFacetLabel = entry.getKey().getLabel().getDisplay().toLowerCase(Locale.FRENCH);
        final Car firstClusterCar = entry.getValue().get(0);
        final Set<Car> carsByMake = databaseCluster.get(searchFacetLabel);
        Assert.assertEquals(carsByMake.iterator().next().getId(), firstClusterCar.getId());
        for (final Car car : entry.getValue()) {
            Assert.assertEquals(searchFacetLabel, car.getMake().toLowerCase(Locale.FRENCH));
        }
    }
}
Also used : SearchQuery(io.vertigo.dynamo.search.model.SearchQuery) ListFilter(io.vertigo.dynamo.collections.ListFilter) URLDecoder(java.net.URLDecoder) BeforeClass(org.junit.BeforeClass) URL(java.net.URL) URI(io.vertigo.dynamo.domain.model.URI) DtObjectUtil(io.vertigo.dynamo.domain.util.DtObjectUtil) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) Facet(io.vertigo.dynamo.collections.model.Facet) SearchIndexDefinition(io.vertigo.dynamo.search.metamodel.SearchIndexDefinition) TreeSet(java.util.TreeSet) Car(io.vertigo.dynamo.search_2_4.data.domain.Car) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) FacetDefinition(io.vertigo.dynamo.collections.metamodel.FacetDefinition) Future(java.util.concurrent.Future) Locale(java.util.Locale) Map(java.util.Map) SearchQuery(io.vertigo.dynamo.search.model.SearchQuery) FacetedQueryResult(io.vertigo.dynamo.collections.model.FacetedQueryResult) CarDataBase(io.vertigo.dynamo.search_2_4.data.domain.CarDataBase) Set(java.util.Set) AbstractTestCaseJU4(io.vertigo.AbstractTestCaseJU4) Test(org.junit.Test) DefinitionSpace(io.vertigo.core.definition.DefinitionSpace) FacetedQueryDefinition(io.vertigo.dynamo.collections.metamodel.FacetedQueryDefinition) DtList(io.vertigo.dynamo.domain.model.DtList) SearchIndex(io.vertigo.dynamo.search.model.SearchIndex) CarSearchLoader(io.vertigo.dynamo.search_2_4.data.domain.CarSearchLoader) File(java.io.File) DtListState(io.vertigo.dynamo.domain.model.DtListState) SelectedFacetValues(io.vertigo.dynamo.collections.model.SelectedFacetValues) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) VUserException(io.vertigo.lang.VUserException) List(java.util.List) FacetedQuery(io.vertigo.dynamo.collections.model.FacetedQuery) Logger(org.apache.logging.log4j.Logger) SearchManager(io.vertigo.dynamo.search.SearchManager) Entry(java.util.Map.Entry) Assert(org.junit.Assert) LogManager(org.apache.logging.log4j.LogManager) FacetValue(io.vertigo.dynamo.collections.model.FacetValue) TreeSet(java.util.TreeSet) Set(java.util.Set) HashMap(java.util.HashMap) FacetValue(io.vertigo.dynamo.collections.model.FacetValue) Car(io.vertigo.dynamo.search_2_4.data.domain.Car) TreeSet(java.util.TreeSet) DtListState(io.vertigo.dynamo.domain.model.DtListState) DtList(io.vertigo.dynamo.domain.model.DtList) Test(org.junit.Test)

Example 2 with FacetedQueryResult

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

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

the class SwaggerApiBuilder method createSchemaObject.

private Map<String, Object> createSchemaObject(final Type type) {
    if (type == null) {
        // Si le type est null, on a pas réussi à récupérer la class : souvant dans le cas des generics
        return unknownObjectRef;
    }
    // -----
    final Map<String, Object> schema = new LinkedHashMap<>();
    final Class<?> objectClass = WebServiceTypeUtil.castAsClass(type);
    final String[] typeAndFormat = toSwaggerType(objectClass);
    schema.put("type", typeAndFormat[0]);
    if (typeAndFormat[1] != null) {
        schema.put("format", typeAndFormat[1]);
    }
    if (WebServiceTypeUtil.isAssignableFrom(Collection.class, type)) {
        // we known that List has one parameterized type
        final Type itemsType = ((ParameterizedType) type).getActualTypeArguments()[0];
        // Si le itemsType est null, on prend le unknownObject
        // type argument can't be void
        schema.put("items", createSchemaObject(itemsType));
    } else if ("object".equals(typeAndFormat[0])) {
        final String objectName;
        final Class<?> parameterClass;
        if (type instanceof ParameterizedType && (((ParameterizedType) type).getActualTypeArguments().length == 1 || FacetedQueryResult.class.isAssignableFrom(objectClass)) && !(((ParameterizedType) type).getActualTypeArguments()[0] instanceof WildcardType)) {
            // We have checked there is one parameter or we known that FacetedQueryResult has two parameterized type
            final Type itemsType = ((ParameterizedType) type).getActualTypeArguments()[0];
            parameterClass = WebServiceTypeUtil.castAsClass(itemsType);
            objectName = objectClass.getSimpleName() + "&lt;" + parameterClass.getSimpleName() + "&gt;";
        } else {
            objectName = objectClass.getSimpleName();
            parameterClass = null;
        }
        schema.put("$ref", "#/definitions/" + objectName);
        schema.remove("type");
        if (!builderDefinitions.containsKey(objectName)) {
            final Map<String, Object> definition = new LinkedHashMap<>();
            // we put definitions first to avoid infinite resolution loop
            builderDefinitions.put(objectName, definition);
            if (DtObject.class.isAssignableFrom(objectClass)) {
                final Class<? extends DtObject> dtClass = (Class<? extends DtObject>) objectClass;
                appendPropertiesDtObject(definition, dtClass);
            } else {
                appendPropertiesObject(definition, objectClass, parameterClass);
            }
        }
    }
    return schema;
}
Also used : DtObject(io.vertigo.dynamo.domain.model.DtObject) FacetedQueryResult(io.vertigo.dynamo.collections.model.FacetedQueryResult) LinkedHashMap(java.util.LinkedHashMap) ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) WebServiceParamType(io.vertigo.vega.webservice.metamodel.WebServiceParam.WebServiceParamType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) DtObject(io.vertigo.dynamo.domain.model.DtObject) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 4 with FacetedQueryResult

use of io.vertigo.dynamo.collections.model.FacetedQueryResult 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)

Example 5 with FacetedQueryResult

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

the class AbstractSearchManagerTest method testSortedClusterByFacetTerm.

/**
 * Test le facettage par term d'une liste.
 */
@Test
public void testSortedClusterByFacetTerm() {
    index(true);
    final SearchQuery searchQuery = SearchQuery.builder(ListFilter.of("*:*")).withFacetClustering(manufacturerFacetDefinition).build();
    final DtListState listState = new DtListState(null, 0, itemIndexDefinition.getIndexDtDefinition().getField("YEAR").getName(), true);
    final FacetedQueryResult<Item, SearchQuery> result = searchManager.loadList(itemIndexDefinition, searchQuery, listState);
    // On vérifie qu'il existe une valeur pour chaque marques et que la première est bien la plus ancienne
    final Map<String, Set<Item>> databaseCluster = new HashMap<>();
    for (final Item item : itemDataBase.getAllItems()) {
        databaseCluster.computeIfAbsent(item.getManufacturer().toLowerCase(Locale.FRENCH), k -> new TreeSet<>((e1, e2) -> e2.getYear().compareTo(e1.getYear()))).add(item);
    }
    Assert.assertEquals(databaseCluster.size(), result.getClusters().size());
    for (final Entry<FacetValue, DtList<Item>> entry : result.getClusters().entrySet()) {
        final String searchFacetLabel = entry.getKey().getLabel().getDisplay().toLowerCase(Locale.FRENCH);
        final Item firstClusterItem = entry.getValue().get(0);
        final Set<Item> itemsByManufacturer = databaseCluster.get(searchFacetLabel);
        Assert.assertEquals(itemsByManufacturer.iterator().next().getId(), firstClusterItem.getId());
        for (final Item item : entry.getValue()) {
            Assert.assertEquals(searchFacetLabel, item.getManufacturer().toLowerCase(Locale.FRENCH));
        }
    }
}
Also used : SearchQuery(io.vertigo.dynamo.search.model.SearchQuery) ListFilter(io.vertigo.dynamo.collections.ListFilter) ItemSearchLoader(io.vertigo.dynamo.search.data.domain.ItemSearchLoader) URLDecoder(java.net.URLDecoder) BeforeClass(org.junit.BeforeClass) URL(java.net.URL) URI(io.vertigo.dynamo.domain.model.URI) DtObjectUtil(io.vertigo.dynamo.domain.util.DtObjectUtil) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) Facet(io.vertigo.dynamo.collections.model.Facet) SearchIndexDefinition(io.vertigo.dynamo.search.metamodel.SearchIndexDefinition) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) FacetDefinition(io.vertigo.dynamo.collections.metamodel.FacetDefinition) Locale(java.util.Locale) Map(java.util.Map) SearchQuery(io.vertigo.dynamo.search.model.SearchQuery) ItemDataBase(io.vertigo.dynamo.search.data.domain.ItemDataBase) Item(io.vertigo.dynamo.search.data.domain.Item) FacetedQueryResult(io.vertigo.dynamo.collections.model.FacetedQueryResult) Set(java.util.Set) AbstractTestCaseJU4(io.vertigo.AbstractTestCaseJU4) Test(org.junit.Test) DefinitionSpace(io.vertigo.core.definition.DefinitionSpace) FacetedQueryDefinition(io.vertigo.dynamo.collections.metamodel.FacetedQueryDefinition) DtList(io.vertigo.dynamo.domain.model.DtList) SearchIndex(io.vertigo.dynamo.search.model.SearchIndex) Collectors(java.util.stream.Collectors) File(java.io.File) DtListState(io.vertigo.dynamo.domain.model.DtListState) SelectedFacetValues(io.vertigo.dynamo.collections.model.SelectedFacetValues) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) VUserException(io.vertigo.lang.VUserException) List(java.util.List) FacetedQuery(io.vertigo.dynamo.collections.model.FacetedQuery) Logger(org.apache.logging.log4j.Logger) Entry(java.util.Map.Entry) Assert(org.junit.Assert) LogManager(org.apache.logging.log4j.LogManager) FacetValue(io.vertigo.dynamo.collections.model.FacetValue) TreeSet(java.util.TreeSet) Set(java.util.Set) HashMap(java.util.HashMap) Item(io.vertigo.dynamo.search.data.domain.Item) FacetValue(io.vertigo.dynamo.collections.model.FacetValue) TreeSet(java.util.TreeSet) DtListState(io.vertigo.dynamo.domain.model.DtListState) DtList(io.vertigo.dynamo.domain.model.DtList) Test(org.junit.Test)

Aggregations

FacetedQueryResult (io.vertigo.dynamo.collections.model.FacetedQueryResult)8 Facet (io.vertigo.dynamo.collections.model.Facet)7 FacetValue (io.vertigo.dynamo.collections.model.FacetValue)7 DtList (io.vertigo.dynamo.domain.model.DtList)7 Map (java.util.Map)7 HashMap (java.util.HashMap)6 AbstractTestCaseJU4 (io.vertigo.AbstractTestCaseJU4)4 DefinitionSpace (io.vertigo.core.definition.DefinitionSpace)4 ListFilter (io.vertigo.dynamo.collections.ListFilter)4 FacetDefinition (io.vertigo.dynamo.collections.metamodel.FacetDefinition)4 FacetedQueryDefinition (io.vertigo.dynamo.collections.metamodel.FacetedQueryDefinition)4 FacetedQuery (io.vertigo.dynamo.collections.model.FacetedQuery)4 SelectedFacetValues (io.vertigo.dynamo.collections.model.SelectedFacetValues)4 DtListState (io.vertigo.dynamo.domain.model.DtListState)4 URI (io.vertigo.dynamo.domain.model.URI)4 DtObjectUtil (io.vertigo.dynamo.domain.util.DtObjectUtil)4 SearchIndexDefinition (io.vertigo.dynamo.search.metamodel.SearchIndexDefinition)4 SearchIndex (io.vertigo.dynamo.search.model.SearchIndex)4 SearchQuery (io.vertigo.dynamo.search.model.SearchQuery)4 VUserException (io.vertigo.lang.VUserException)4