use of com.rbmhtechnology.vind.api.result.facet.FacetValue in project vind by RBMHTechnology.
the class ResultUtils method getIntervalFacetResults.
private static Pair<String, IntervalFacetResult> getIntervalFacetResults(Aggregation aggregation, Facet.IntervalFacet intervalFacet) {
final List<FacetValue<String>> values = new ArrayList<>();
Optional.ofNullable(aggregation).ifPresent(agg -> ((ParsedRange) agg).getBuckets().stream().map(bucket -> new FacetValue(bucket.getKey(), bucket.getDocCount())).forEach(values::add));
final IntervalFacetResult intervalFacetResult = new IntervalFacetResult(values);
return Pair.of(intervalFacet.getFacetName(), intervalFacetResult);
}
use of com.rbmhtechnology.vind.api.result.facet.FacetValue in project vind by RBMHTechnology.
the class ResultUtils method addSuggestionValue.
private static void addSuggestionValue(HashMap<FieldDescriptor, TermFacetResult<?>> suggestionValues, String suggestionFieldName, String suggestedValue, DocumentFactory factory, String context) {
final String fieldName = FieldUtil.getSourceFieldName(suggestionFieldName.replaceAll(".suggestion", ""), context);
final FieldDescriptor<?> suggestionField = factory.getField(fieldName);
final String cleanValue = suggestedValue.replaceAll("</*em>", "");
final TermFacetResult<?> termSuggestions = Optional.ofNullable(suggestionValues.get(suggestionField)).orElse(new TermFacetResult(Collections.singletonList(new FacetValue(cleanValue, 0))));
final List<FacetValue> updatedFacetValues = new ArrayList<>();
final Optional<FacetValue> updatedValue = termSuggestions.getValues().stream().map((FacetValue<?> oldFacetValue) -> {
if (oldFacetValue.getValue().toString().equals(cleanValue)) {
final long updatedCount = oldFacetValue.getCount() + 1;
final FacetValue updatedFacetValue = new FacetValue(cleanValue, updatedCount);
updatedFacetValues.add(updatedFacetValue);
return updatedFacetValue;
} else {
updatedFacetValues.add(oldFacetValue);
return oldFacetValue;
}
}).filter((FacetValue oldFacetValue) -> oldFacetValue.getValue().toString().equals(cleanValue)).findAny();
if (!updatedValue.isPresent()) {
updatedFacetValues.add(new FacetValue(cleanValue, 1));
}
suggestionValues.put(suggestionField, new TermFacetResult(updatedFacetValues));
}
use of com.rbmhtechnology.vind.api.result.facet.FacetValue in project vind by RBMHTechnology.
the class ResultUtils method getTermFacetResults.
private static Pair<FieldDescriptor<?>, TermFacetResult<?>> getTermFacetResults(Aggregation aggregation, Facet.TermFacet termFacet, DocumentFactory factory, FieldDescriptor.UseCase useCase) {
final TermFacetResult<?> result = new TermFacetResult<>();
final FieldDescriptor<?> field = factory.getField(termFacet.getFieldName());
Optional.ofNullable(aggregation).ifPresent(agg -> ((ParsedTerms) aggregation).getBuckets().stream().map(bucket -> {
final Object key = Number.class.isAssignableFrom(field.getType()) ? bucket.getKeyAsNumber() : bucket.getKeyAsString();
return Pair.of(key, bucket.getDocCount());
}).map(p -> new FacetValue(DocumentUtil.castForDescriptor(p.getKey(), field, useCase), p.getValue())).forEach(result::addFacetValue));
return Pair.of(field, result);
}
use of com.rbmhtechnology.vind.api.result.facet.FacetValue in project vind by RBMHTechnology.
the class ResultUtils method getRangeFacetResults.
private static Pair<String, RangeFacetResult<?>> getRangeFacetResults(Aggregation aggregation, Facet rangeFacet) {
switch(rangeFacet.getType()) {
case "NumericRangeFacet":
final Facet.NumericRangeFacet numericRangeFacet = (Facet.NumericRangeFacet) rangeFacet;
final List<FacetValue> numericValues = new ArrayList<>();
Optional.ofNullable(aggregation).ifPresent(agg -> ((ParsedHistogram) ((ParsedRange) agg).getBuckets().get(0).getAggregations().getAsMap().get(rangeFacet.getFacetName())).getBuckets().stream().map(rangeBucket -> new FacetValue(DocumentUtil.castForDescriptor(rangeBucket.getKey(), numericRangeFacet.getFieldDescriptor(), FieldDescriptor.UseCase.Facet), rangeBucket.getDocCount())).forEach(numericValues::add));
return Pair.of(rangeFacet.getFacetName(), new RangeFacetResult(numericValues, numericRangeFacet.getStart(), numericRangeFacet.getEnd(), numericRangeFacet.getGap().longValue()));
default:
final Facet.DateRangeFacet dateRangeFacet = (Facet.DateRangeFacet) rangeFacet;
final List<FacetValue> dateValues = new ArrayList<>();
Optional.ofNullable(aggregation).ifPresent(agg -> ((ParsedDateHistogram) ((ParsedDateRange) agg).getBuckets().get(0).getAggregations().getAsMap().get(dateRangeFacet.getFacetName())).getBuckets().stream().map(rangeBucket -> new FacetValue(DocumentUtil.castForDescriptor(rangeBucket.getKey(), dateRangeFacet.getFieldDescriptor(), FieldDescriptor.UseCase.Facet), rangeBucket.getDocCount())).forEach(dateValues::add));
return Pair.of(rangeFacet.getFacetName(), new RangeFacetResult(dateValues, dateRangeFacet.getStart(), dateRangeFacet.getEnd(), dateRangeFacet.getGap().longValue()));
}
}
Aggregations