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

io.orchestrate.client.DistanceAggregateResult Maven / Gradle / Ivy

package io.orchestrate.client;

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

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;

/**
 * This class represents the results of a DistanceAggregate function. The list of
 * RangeBucket objects included with this aggregate indicate how many geo-point
 * objects in the designated database field were found within each distance
 * interval from the central anchor point.
 */
public class DistanceAggregateResult extends AggregateResult {

    private final List buckets;

    DistanceAggregateResult(
        String fieldName,
        long valueCount,
        List buckets
    ) {
        super(fieldName, "distance", valueCount);
        this.buckets = buckets;
    }

    /**
     * Returns a list of RangeBuckets representing the number of geo-point field values
     * found within each distance interval from the central anchor point designated in
     * the accompanying distance query (inside the mandatory NEAR clause).
     *
     * Likewise, the bounds of the range are expressed in terms of the same distance units
     * (miles, kilometers, etc) used within the accompanying distance query.
     *
     * @return The range buckets.
     */
    public List getBuckets() {
        return buckets;
    }

    static DistanceAggregateResult from(JsonNode json) {

        String fieldName = json.get("field_name").asText();
        long valueCount = json.get("value_count").asLong();

        String aggregateKind = json.get("aggregate_kind").asText();
        assert aggregateKind.equals("distance");

        ArrayNode bucketNodes = (ArrayNode) json.get("buckets");
        List buckets = new ArrayList(bucketNodes.size());
        for (JsonNode bucketNode : bucketNodes) {
            double min = 0;
            if (bucketNode.has("min")) {
                min = bucketNode.get("min").asDouble();
            }
            double max = Double.POSITIVE_INFINITY;
            if (bucketNode.has("max")) {
                max = bucketNode.get("max").asDouble();
            }
            long count = bucketNode.get("count").asLong();
            buckets.add(new RangeBucket(min, max, count));
        }

        return new DistanceAggregateResult(fieldName, valueCount, buckets);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy