org.elasticsearch.search.aggregations.bucket.terms.ParsedTerms Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.bucket.terms.ParsedTerms. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JdbcResponseExtractor.java    From elasticsearch-sql with MIT License 6 votes vote down vote up
private void parseAggregations(Aggregations aggregations, Map<String, Object> aggMap, String parent) {
    for (Aggregation aggregation : aggregations) {
        if (aggregation instanceof ParsedNested) {
            parseNestedAggregation(aggregation, aggMap);
        } else if (aggregation instanceof ParsedComposite) {
            parseCompositeAggregation(aggregation, aggMap, parent);
        } else if (aggregation instanceof ParsedTerms) {
            parseTermsAggregation(aggregation, aggMap, parent);
        } else if (aggregation instanceof ParsedTopHits) {
            parseTopHitsAggregation(aggregation, aggMap);
        } else if (aggregation instanceof ParsedCardinality) {
            parseCardinalityAggregation(aggregation, aggMap);
        } else if (aggregation instanceof ParsedRange) {
            parseRangeAggregation(aggregation, aggMap);
        } else if (aggregation instanceof ParsedGeoBounds) {
            parseGeoBoundAggregation(aggregation, aggMap);
        }
    }
}
 
Example #2
Source File: ResultUtils.java    From vind with Apache License 2.0 6 votes vote down vote up
private static Pair<FieldDescriptor<?> ,TermFacetResult<?>> getTermFacetResults(Aggregation aggregation, Facet.TermFacet termFacet, DocumentFactory factory) {
    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, FieldUtil.Fieldname.UseCase.Facet), p.getValue()))
                        .forEach(result::addFacetValue));

    return Pair.of(termFacet.getFieldDescriptor(), result);
}
 
Example #3
Source File: ElasticsearchCatalogService.java    From staccato with Apache License 2.0 4 votes vote down vote up
/**
 * Produces a list of all unique values for a given field in a collection.
 *
 * @param collection The collection that will be queried.
 * @param path The request path containing property fieldsExtension and values for which to find unique values
 * @return A list of unique values
 */
@Override
public List<String> getValuesForField(CollectionMetadata collection, List<String> path) {
    String fieldName = path.get(path.size() - 1);

    List<String> values = new LinkedList<>();
    fieldName = "properties." + fieldName;

    // build the term aggregation from the last subcatalog property in the url path
    TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder(fieldName + "_Agg", ValueType.STRING).size(10000);
    aggregationBuilder.field(fieldName);

    // the query based on all of the unique subcatalog value previously selected
    QueryBuilder query = QueryBuilders.boolQuery();
    for (int i = 2; i * 2 <= path.size(); i = i + 2) {
        String property = "properties." + path.get(i);
        String value = path.get(i + 1);
        QueryBuilder pathQuery = QueryBuilders.termQuery(property, value);

        ((BoolQueryBuilder) query).must(pathQuery);
    }

    SearchRequest request = new SearchRequest().indices(indexAliasLookup.getReadAlias(collection.getId()))
            .searchType(SearchType.DFS_QUERY_THEN_FETCH)
            .source(new SearchSourceBuilder().query(query).aggregation(aggregationBuilder).size(0));

    SearchResponse response;
    try {
        response = client.search(request, RequestOptions.DEFAULT);
    } catch (Exception ex) {
        log.error("Error getting aggregations.", ex);
        throw new RuntimeException("Error getting aggregations.", ex);
    }

    if (response == null) return Collections.EMPTY_LIST;

    ParsedTerms terms = response.getAggregations().get(fieldName + "_Agg");
    if (terms != null) {
        List<ParsedTerms.ParsedBucket> buckets = (List<ParsedTerms.ParsedBucket>) terms.getBuckets();
        if (buckets != null && buckets.size() > 0) {
            for (ParsedTerms.ParsedBucket bucket : buckets) {
                values.add(bucket.getKeyAsString());
            }
        }
        return values;
    }
    return Collections.EMPTY_LIST;
}
 
Example #4
Source File: ResultUtils.java    From vind with Apache License 2.0 4 votes vote down vote up
private static Pair<String, List<PivotFacetResult>> getPivotFacetResults(Aggregation aggregation, Facet.PivotFacet pivotFacet, Map<String, Facet> vindFacets) {

        final FieldDescriptor field = pivotFacet.getFieldDescriptors().get(0);

        if( Objects.nonNull(aggregation) ){
            final ParsedTerms rootPivot = (ParsedTerms) aggregation;
            final List<PivotFacetResult> pivotFacetResult = rootPivot.getBuckets().stream()
                    .map(bucket -> {
                        final Map<String, Aggregation> aggMap = bucket.getAggregations().asMap();
                        final Aggregation pivotAgg = aggMap.get(pivotFacet.getFacetName());
                        final Map<String, RangeFacetResult<?>> rangeSubfacets = new HashMap<>();
                        final Map<String, QueryFacetResult<?>> querySubfacets = new HashMap<>();
                        final Map<String, StatsFacetResult<?>> statsSubfacets = new HashMap<>();

                        aggMap.values().forEach(agg -> {
                            if (ParsedExtendedStats.class.isAssignableFrom(agg.getClass())) {
                                final HashMap<String, Aggregation> statsMap = new HashMap<>();
                                statsMap.put(agg.getName(), agg);
                                statsSubfacets.put(
                                        agg.getName(),
                                        ResultUtils.getStatsFacetResults(statsMap, (Facet.StatsFacet) vindFacets.get(agg.getName())).getValue());
                            }
                            if (ParsedQuery.class.isAssignableFrom(agg.getClass())) {

                                querySubfacets.put(
                                        agg.getName(),
                                        ResultUtils.getQueryFacetResults(agg, (Facet.QueryFacet) vindFacets.get(agg.getName())).getValue());
                            }
                            if (ParsedRange.class.isAssignableFrom(agg.getClass())) {
                                rangeSubfacets.put(
                                        agg.getName(),
                                        ResultUtils.getRangeFacetResults(agg, vindFacets.get(agg.getName())).getValue());
                            }
                        });

                        final List<PivotFacetResult> subPivot = getPivotFacetResults(pivotAgg, pivotFacet, vindFacets).getValue();
                        return new PivotFacetResult(
                                subPivot,
                                bucket.getKey(),
                                field,
                                Long.valueOf(bucket.getDocCount()).intValue(),
                                rangeSubfacets,
                                querySubfacets,
                                statsSubfacets);
                    })
                    .collect(Collectors.toList());

            return Pair.of(pivotFacet.getFacetName(), pivotFacetResult);
        }
        return Pair.of(null, null);
    }