All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scaleset.search.es.agg.GeoHashGridAggregationConverter Maven / Gradle / Ivy

There is a newer version: 0.24.0
Show newest version
package com.scaleset.search.es.agg;

import com.scaleset.search.Aggregation;
import com.scaleset.search.AggregationResults;
import com.scaleset.search.Bucket;
import com.scaleset.search.es.QueryConverter;
import com.scaleset.search.es.ResultsConverter;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid;
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGridBuilder;

import java.util.ArrayList;
import java.util.List;

public class GeoHashGridAggregationConverter extends AbstractCombinedConverter {

    @Override
    public AggregationBuilder convert(QueryConverter queryConverter, Aggregation aggregation) {

        GeoHashGridBuilder result = AggregationBuilders.geohashGrid(getName(aggregation));
        String field = aggregation.getString("field");
        Integer precision = aggregation.getInteger("precision");
        Integer size = aggregation.getInteger("size");
        if (field != null) {
            result.field(field);
        }
        if (precision != null) {
            result.precision(precision);
        }
        if (size != null) {
            result.size(size);
        }
        // not sure we really want to support sub-aggs, but we will
        addSubAggs(queryConverter, aggregation, result);
        return result;
    }

    @Override
    public AggregationResults convertResult(ResultsConverter resultsConverter, Aggregation
            aggregation, Aggregations aggs) {
        String name = aggregation.getName();
        AggregationResults result = null;
        if (aggs.get(name) instanceof GeoHashGrid) {
            GeoHashGrid grid = (GeoHashGrid) (aggs.get(name));
            List buckets = new ArrayList<>();
            for (GeoHashGrid.Bucket bucket : grid.getBuckets()) {
                if (bucket.getDocCount() > 0) {
                    Bucket b = new Bucket(bucket.getKey(), bucket.getDocCount());
                    buckets.add(b);
                    for (Aggregation subAgg : aggregation.getAggs().values()) {
                        AggregationResults subResults = resultsConverter.convertResults(subAgg, bucket.getAggregations());
                        if (subResults != null) {
                            b.getAggs().put(subAgg.getName(), subResults);
                        }
                    }
                }
            }
            result = new AggregationResults(name, buckets);
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy