Search in sources :

Example 1 with FacetValue

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);
}
Also used : FacetValue(com.rbmhtechnology.vind.api.result.facet.FacetValue) ArrayList(java.util.ArrayList) ParsedRange(org.elasticsearch.search.aggregations.bucket.range.ParsedRange) IntervalFacetResult(com.rbmhtechnology.vind.api.result.facet.IntervalFacetResult)

Example 2 with FacetValue

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));
}
Also used : FacetValue(com.rbmhtechnology.vind.api.result.facet.FacetValue) TermFacetResult(com.rbmhtechnology.vind.api.result.facet.TermFacetResult) ArrayList(java.util.ArrayList)

Example 3 with FacetValue

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);
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) StatsFacetResult(com.rbmhtechnology.vind.api.result.facet.StatsFacetResult) GetResponse(org.elasticsearch.action.get.GetResponse) Arrays(java.util.Arrays) FacetValue(com.rbmhtechnology.vind.api.result.facet.FacetValue) ParsedDateRange(org.elasticsearch.search.aggregations.bucket.range.ParsedDateRange) ParsedQuery(org.elasticsearch.index.query.ParsedQuery) ParsedStringTerms(org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms) IntervalFacetResult(com.rbmhtechnology.vind.api.result.facet.IntervalFacetResult) Pair(org.apache.commons.lang3.tuple.Pair) Facet(com.rbmhtechnology.vind.api.query.facet.Facet) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) QueryFacetResult(com.rbmhtechnology.vind.api.result.facet.QueryFacetResult) ParsedSingleValueNumericMetricsAggregation(org.elasticsearch.search.aggregations.metrics.ParsedSingleValueNumericMetricsAggregation) SearchHit(org.elasticsearch.search.SearchHit) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) Aggregations(org.elasticsearch.search.aggregations.Aggregations) Collection(java.util.Collection) ParsedCardinality(org.elasticsearch.search.aggregations.metrics.ParsedCardinality) MultiGetItemResponse(org.elasticsearch.action.get.MultiGetItemResponse) Streams(com.google.common.collect.Streams) GetResult(com.rbmhtechnology.vind.api.result.GetResult) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) RealTimeGet(com.rbmhtechnology.vind.api.query.get.RealTimeGet) List(java.util.List) ParsedPercentiles(org.elasticsearch.search.aggregations.metrics.ParsedPercentiles) Stream(java.util.stream.Stream) ParsedMissing(org.elasticsearch.search.aggregations.bucket.missing.ParsedMissing) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Optional(java.util.Optional) SubdocumentFacetResult(com.rbmhtechnology.vind.api.result.facet.SubdocumentFacetResult) FieldDescriptor(com.rbmhtechnology.vind.model.FieldDescriptor) ArrayUtils(org.apache.commons.lang3.ArrayUtils) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CollectionUtils(org.apache.commons.collections.CollectionUtils) Percentile(org.elasticsearch.search.aggregations.metrics.Percentile) Aggregation(org.elasticsearch.search.aggregations.Aggregation) ParsedFilters(org.elasticsearch.search.aggregations.bucket.filter.ParsedFilters) TermFacetResult(com.rbmhtechnology.vind.api.result.facet.TermFacetResult) FacetResults(com.rbmhtechnology.vind.api.result.FacetResults) ParsedTerms(org.elasticsearch.search.aggregations.bucket.terms.ParsedTerms) RangeFacetResult(com.rbmhtechnology.vind.api.result.facet.RangeFacetResult) ParsedDateHistogram(org.elasticsearch.search.aggregations.bucket.histogram.ParsedDateHistogram) ParsedExtendedStats(org.elasticsearch.search.aggregations.metrics.ParsedExtendedStats) Document(com.rbmhtechnology.vind.api.Document) ParsedRange(org.elasticsearch.search.aggregations.bucket.range.ParsedRange) PivotFacetResult(com.rbmhtechnology.vind.api.result.facet.PivotFacetResult) DocumentFactory(com.rbmhtechnology.vind.model.DocumentFactory) Collections(java.util.Collections) ParsedHistogram(org.elasticsearch.search.aggregations.bucket.histogram.ParsedHistogram) FacetValue(com.rbmhtechnology.vind.api.result.facet.FacetValue) TermFacetResult(com.rbmhtechnology.vind.api.result.facet.TermFacetResult)

Example 4 with FacetValue

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()));
    }
}
Also used : RangeFacetResult(com.rbmhtechnology.vind.api.result.facet.RangeFacetResult) FacetValue(com.rbmhtechnology.vind.api.result.facet.FacetValue) ParsedHistogram(org.elasticsearch.search.aggregations.bucket.histogram.ParsedHistogram) ParsedDateHistogram(org.elasticsearch.search.aggregations.bucket.histogram.ParsedDateHistogram) ArrayList(java.util.ArrayList) Facet(com.rbmhtechnology.vind.api.query.facet.Facet)

Aggregations

FacetValue (com.rbmhtechnology.vind.api.result.facet.FacetValue)4 ArrayList (java.util.ArrayList)4 Facet (com.rbmhtechnology.vind.api.query.facet.Facet)2 IntervalFacetResult (com.rbmhtechnology.vind.api.result.facet.IntervalFacetResult)2 RangeFacetResult (com.rbmhtechnology.vind.api.result.facet.RangeFacetResult)2 TermFacetResult (com.rbmhtechnology.vind.api.result.facet.TermFacetResult)2 ParsedDateHistogram (org.elasticsearch.search.aggregations.bucket.histogram.ParsedDateHistogram)2 ParsedHistogram (org.elasticsearch.search.aggregations.bucket.histogram.ParsedHistogram)2 Streams (com.google.common.collect.Streams)1 Document (com.rbmhtechnology.vind.api.Document)1 RealTimeGet (com.rbmhtechnology.vind.api.query.get.RealTimeGet)1 FacetResults (com.rbmhtechnology.vind.api.result.FacetResults)1 GetResult (com.rbmhtechnology.vind.api.result.GetResult)1 PivotFacetResult (com.rbmhtechnology.vind.api.result.facet.PivotFacetResult)1 QueryFacetResult (com.rbmhtechnology.vind.api.result.facet.QueryFacetResult)1 StatsFacetResult (com.rbmhtechnology.vind.api.result.facet.StatsFacetResult)1 SubdocumentFacetResult (com.rbmhtechnology.vind.api.result.facet.SubdocumentFacetResult)1 DocumentFactory (com.rbmhtechnology.vind.model.DocumentFactory)1 FieldDescriptor (com.rbmhtechnology.vind.model.FieldDescriptor)1 Arrays (java.util.Arrays)1