use of io.vertigo.dynamo.collections.model.FacetValue in project vertigo by KleeGroup.
the class ESFacetedQueryResultBuilder method createCluster.
private Map<FacetValue, DtList<I>> createCluster(final Map<String, I> dtcIndex, final Map<I, Map<DtField, String>> resultHighlights) {
final Map<FacetValue, DtList<I>> resultCluster = new LinkedHashMap<>();
final FacetDefinition facetDefinition = searchQuery.getClusteringFacetDefinition();
final Aggregation facetAggregation = queryResponse.getAggregations().get(facetDefinition.getName());
if (facetDefinition.isRangeFacet()) {
// Cas des facettes par 'range'
final MultiBucketsAggregation multiBuckets = (MultiBucketsAggregation) facetAggregation;
for (final FacetValue facetRange : facetDefinition.getFacetRanges()) {
final Bucket value = getBucketByKey(multiBuckets, facetRange.getListFilter().getFilterValue());
populateCluster(value, facetRange, resultCluster, dtcIndex, resultHighlights);
}
} else {
// Cas des facettes par 'term'
final MultiBucketsAggregation multiBuckets = (MultiBucketsAggregation) facetAggregation;
FacetValue facetValue;
for (final Bucket bucket : multiBuckets.getBuckets()) {
final String term = bucket.getKeyAsString();
final String query = facetDefinition.getDtField().getName() + ":\"" + term + "\"";
final MessageText label = MessageText.of(term);
facetValue = new FacetValue(term, ListFilter.of(query), label);
populateCluster(bucket, facetValue, resultCluster, dtcIndex, resultHighlights);
}
}
return resultCluster;
}
use of io.vertigo.dynamo.collections.model.FacetValue in project vertigo by KleeGroup.
the class ESFacetedQueryResultBuilder method createTermFacet.
private static Facet createTermFacet(final FacetDefinition facetDefinition, final MultiBucketsAggregation multiBuckets) {
final Map<FacetValue, Long> facetValues = new LinkedHashMap<>();
FacetValue facetValue;
for (final Bucket value : multiBuckets.getBuckets()) {
final String term = value.getKeyAsString();
final MessageText label = MessageText.of(term);
final String query = facetDefinition.getDtField().getName() + ":\"" + term + "\"";
facetValue = new FacetValue(term, ListFilter.of(query), label);
facetValues.put(facetValue, value.getDocCount());
}
return new Facet(facetDefinition, facetValues);
}
use of io.vertigo.dynamo.collections.model.FacetValue 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);
}
use of io.vertigo.dynamo.collections.model.FacetValue 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);
}
use of io.vertigo.dynamo.collections.model.FacetValue in project vertigo by KleeGroup.
the class FacetFactory method createTermCluster.
private static <D extends DtObject> Map<FacetValue, DtList<D>> createTermCluster(final FacetDefinition facetDefinition, final DtList<D> dtList) {
// map résultat avec les docs par FacetFilter
final Map<FacetValue, DtList<D>> clusterValues = new LinkedHashMap<>();
// Cas des facettes par Term
final DtField dtField = facetDefinition.getDtField();
// on garde un index pour incrémenter le facetFilter pour chaque Term
final Map<Object, FacetValue> facetFilterIndex = new HashMap<>();
FacetValue facetValue;
for (final D dto : dtList) {
final Object value = dtField.getDataAccessor().getValue(dto);
facetValue = facetFilterIndex.get(value);
if (facetValue == null) {
final String valueAsString = dtField.getDomain().valueToString(value);
final String label;
if (StringUtil.isEmpty(valueAsString)) {
label = "<==no label==>";
} else {
label = valueAsString;
}
final MessageText labelMsg = MessageText.of(label);
// on garde la syntaxe Solr pour l'instant
final ListFilter listFilter = ListFilter.of(dtField.getName() + ":\"" + valueAsString + "\"");
facetValue = new FacetValue(label, listFilter, labelMsg);
facetFilterIndex.put(value, facetValue);
clusterValues.put(facetValue, new DtList<D>(dtList.getDefinition()));
}
clusterValues.get(facetValue).add(dto);
}
// tri des facettes
final Comparator<FacetValue> facetComparator = new FacetComparator<>(clusterValues);
final Map<FacetValue, DtList<D>> sortedFacetValues = new TreeMap<>(facetComparator);
sortedFacetValues.putAll(clusterValues);
return sortedFacetValues;
}
Aggregations