Search in sources :

Example 31 with FacetValue

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

the class ESSearchRequestBuilder method dateRangeFacetToAggregationBuilder.

private static AggregationBuilder<DateRangeBuilder> dateRangeFacetToAggregationBuilder(final FacetDefinition facetDefinition, final DtField dtField) {
    final DateRangeBuilder 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 : DateRangeBuilder(org.elasticsearch.search.aggregations.bucket.range.date.DateRangeBuilder) FacetValue(io.vertigo.dynamo.collections.model.FacetValue)

Example 32 with FacetValue

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

the class AbstractSearchManagerTest method testClusterByFacetTerm.

/**
 * Test le facettage par term d'une liste.
 */
@Test
public void testClusterByFacetTerm() {
    index(true);
    final SearchQuery searchQuery = SearchQuery.builder(ListFilter.of("*:*")).withFacetClustering(makeFacetDefinition).build();
    final FacetedQueryResult<Car, SearchQuery> result = searchManager.loadList(carIndexDefinition, searchQuery, null);
    // On vérifie qu'il existe une valeur pour chaque marques et que le nombre d'occurrences est correct
    final Map<String, List<Car>> databaseCluster = new HashMap<>();
    for (final Car car : carDataBase.getAllCars()) {
        databaseCluster.computeIfAbsent(car.getMake().toLowerCase(Locale.FRENCH), k -> new ArrayList<>()).add(car);
    }
    int previousCount = Integer.MAX_VALUE;
    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 int searchFacetCount = entry.getValue().size();
        final List<Car> carsByMake = databaseCluster.get(searchFacetLabel);
        Assert.assertEquals(carsByMake.size(), searchFacetCount);
        Assert.assertTrue("Group order invalid", previousCount >= searchFacetCount);
        previousCount = searchFacetCount;
        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) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FacetValue(io.vertigo.dynamo.collections.model.FacetValue) Car(io.vertigo.dynamo.search_2_4.data.domain.Car) ArrayList(java.util.ArrayList) DtList(io.vertigo.dynamo.domain.model.DtList) List(java.util.List) DtList(io.vertigo.dynamo.domain.model.DtList) Test(org.junit.Test)

Example 33 with FacetValue

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

the class AbstractSearchManagerTest method testClusterByFacetRange.

/**
 * Test le facettage par term d'une liste.
 */
@Test
public void testClusterByFacetRange() {
    index(true);
    final SearchQuery searchQuery = SearchQuery.builder(ListFilter.of("*:*")).withFacetClustering(// "avant 2000", "2000-2005", "après 2005"
    yearFacetDefinition).build();
    final FacetedQueryResult<Car, SearchQuery> result = searchManager.loadList(carIndexDefinition, searchQuery, null);
    // On vérifie qu'il existe une valeur pour chaque marques et que le nombre d'occurrences est correct
    final Map<String, List<Car>> databaseCluster = new HashMap<>();
    databaseCluster.put(YearCluster.before2000.getLabel(), new ArrayList<>());
    databaseCluster.put(YearCluster.between2000and2005.getLabel(), new ArrayList<>());
    databaseCluster.put(YearCluster.after2005.getLabel(), new ArrayList<>());
    for (final Car car : carDataBase.getAllCars()) {
        if (car.getYear() < 2000) {
            databaseCluster.get(YearCluster.before2000.getLabel()).add(car);
        } else if (car.getYear() < 2005) {
            databaseCluster.get(YearCluster.between2000and2005.getLabel()).add(car);
        } else {
            databaseCluster.get(YearCluster.after2005.getLabel()).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 int searchFacetCount = entry.getValue().size();
        final List<Car> carsByYear = databaseCluster.get(searchFacetLabel);
        Assert.assertEquals(carsByYear.size(), searchFacetCount);
        for (final Car car : entry.getValue()) {
            if (car.getYear() < 2000) {
                Assert.assertEquals(searchFacetLabel, YearCluster.before2000.getLabel());
            } else if (car.getYear() < 2005) {
                Assert.assertEquals(searchFacetLabel, YearCluster.between2000and2005.getLabel());
            } else {
                Assert.assertEquals(searchFacetLabel, YearCluster.after2005.getLabel());
            }
        }
    }
}
Also used : SearchQuery(io.vertigo.dynamo.search.model.SearchQuery) HashMap(java.util.HashMap) FacetValue(io.vertigo.dynamo.collections.model.FacetValue) Car(io.vertigo.dynamo.search_2_4.data.domain.Car) ArrayList(java.util.ArrayList) DtList(io.vertigo.dynamo.domain.model.DtList) List(java.util.List) DtList(io.vertigo.dynamo.domain.model.DtList) Test(org.junit.Test)

Example 34 with FacetValue

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

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

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