Skip to content

Commit

Permalink
Issue searchbox-io#146 Adding aggregation support
Browse files Browse the repository at this point in the history
I have added new models in the io.searchbox.core.search.aggregation
package which represent all the base aggregations. I have not added
any classes for 'nested' aggregations as these are simply ways of crafting
subaggregations on the request side, but should be handled as one of
the base models when dealing with search results.

TODO:
* implement getAggs() in the SearchResults class
* Add tests around these aggregations models
  • Loading branch information
Clayton F Stout authored and Clayton F Stout committed Feb 12, 2015
1 parent 3f0f407 commit fbb27f9
Show file tree
Hide file tree
Showing 25 changed files with 937 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.searchbox.core.search.aggregation;

/**
* @author cfstout
*/

public abstract class Aggregation {

protected String name;

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.searchbox.core.search.aggregation;

import com.google.gson.JsonObject;

/**
* @author cfstout
*/
public class AvgAggregation extends Aggregation {

public static final String TYPE = "avg";

private Double avg;

public AvgAggregation(String name, JsonObject avgAggregation) {
this.name = name;
avg = avgAggregation.get("value").getAsDouble();
}

public Double getAvg() {
return avg;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.searchbox.core.search.aggregation;

import com.google.gson.JsonObject;

/**
* @author cfstout
*/
public class CardinalityAggregation extends Aggregation{

public static final String TYPE = "cardinality";

private Long cardinality;

public CardinalityAggregation(String name, JsonObject cardinalityAggregation) {
this.name = name;
cardinality = cardinalityAggregation.get("value").getAsLong();
}

public Long getCardinality() {
return cardinality;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.searchbox.core.search.aggregation;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

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

/**
* @author cfstout
*/
public class DateHistogramAggregation extends Aggregation{

public static final String TYPE = "date_histogram";

private List<DateHistogram> dateHistograms;

public DateHistogramAggregation(String name, JsonObject dateHistogramAggregation) {
this.name = name;
dateHistograms = new ArrayList<DateHistogram>();
for (JsonElement bucket : dateHistogramAggregation.get("buckets").getAsJsonArray()) {
JsonElement time = bucket.getAsJsonObject().get("key");
JsonElement timeAsString = bucket.getAsJsonObject().get("key_as_string");
JsonElement count = bucket.getAsJsonObject().get("doc_count");
DateHistogram histogram = new DateHistogram(time.getAsLong(), timeAsString.getAsString(), count.getAsLong());
dateHistograms.add(histogram);
}
}

public List<DateHistogram> getDateHistograms() {
return dateHistograms;
}

public class DateHistogram extends HistogramAggregation.Histogram {

private String timeAsString;

DateHistogram(Long time, String timeAsString, Long count) {
super(time, count);
this.timeAsString = timeAsString;
}

public Long getTime() {
return getKey();
}

public String getTimeAsString() {
return timeAsString;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.searchbox.core.search.aggregation;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

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

/**
* @author cfstout
*/
public class DateRangeAggregation extends Aggregation {

public static final String TYPE = "date_range";

private List<DateRange> ranges;

public DateRangeAggregation(String name, JsonObject rangeAggregation) {
this.name = name;
ranges = new ArrayList<DateRange>();
//todo support keyed:true as well
for (JsonElement bucketv : rangeAggregation.get("buckets").getAsJsonArray()) {
JsonObject bucket = bucketv.getAsJsonObject();
DateRange range = new DateRange(
bucket.has("from") ? bucket.get("from").getAsDouble() : null,
bucket.has("to") ? bucket.get("to").getAsDouble() : null,
bucket.has("count") ? bucket.get("doc_count").getAsLong() : null,
bucket.has("from_as_string") ? bucket.get("from_as_string").getAsString() : null,
bucket.has("to_as_string") ? bucket.get("to_as_string").getAsString() : null);
ranges.add(range);
}
}

public List<DateRange> getRanges() {
return ranges;
}

public class DateRange extends RangeAggregation.Range {
private String fromAsString;
private String toAsString;

public DateRange(Double from, Double to, Long count, String fromString, String toString){
super(from, to, count);
this.fromAsString = fromString;
this.toAsString = toString;
}

public String getFromAsString() {
return fromAsString;
}

public String getToAsString() {
return toAsString;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.searchbox.core.search.aggregation;

import com.google.gson.JsonObject;

/**
* @author cfstout
*/
public class ExtendedStatsAggregation extends StatsAggregation {

private Double sumOfSquares;
private Double variance;
private Double stdDeviation;
private Double stdDeviationUpperBounds;
private Double stdDeviationLowerBounds;

public ExtendedStatsAggregation(String name, JsonObject extendedStatsAggregation) {
super(name, extendedStatsAggregation);
this.sumOfSquares = extendedStatsAggregation.get("sum_of_squares").getAsDouble();
this.variance = extendedStatsAggregation.get("variance").getAsDouble();
this.stdDeviation = extendedStatsAggregation.get("std_deviation").getAsDouble();
JsonObject stdDeviationBounds = extendedStatsAggregation.get("std_deviation_bounds").getAsJsonObject();
this.stdDeviationUpperBounds = stdDeviationBounds.get("upper").getAsDouble();
this.stdDeviationLowerBounds = stdDeviationBounds.get("lower").getAsDouble();
}

public Double getSumOfSquares() {
return sumOfSquares;
}

public Double getVariance() {
return variance;
}

public Double getStdDeviation() {
return stdDeviation;
}

public Double getStdDeviationUpperBounds() {
return stdDeviationUpperBounds;
}

public Double getStdDeviationLowerBounds() {
return stdDeviationLowerBounds;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.searchbox.core.search.aggregation;

import com.google.gson.JsonObject;

/**
* @author cfstout
*/
public class FilterAggregation extends Aggregation{

public static final String TYPE = "filter";

private Long count;

public FilterAggregation(String name, JsonObject filterAggregation) {
this.name = name;
this.count = filterAggregation.get("doc_count").getAsLong();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.searchbox.core.search.aggregation;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

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

/**
* @author cfstout
*/
public class FiltersAggregation extends Aggregation {

public static final String TYPE = "filters";

private List<Long> counts;

public FiltersAggregation(String name, JsonObject filtersAggregation) {
this.name = name;
counts = new ArrayList<Long>();
for (JsonElement bucket : filtersAggregation.get("buckets").getAsJsonArray()) {
Long count = bucket.getAsJsonObject().get("doc_count").getAsLong();
counts.add(count);
}
}

public List<Long> getCounts() {
return counts;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.searchbox.core.search.aggregation;

import com.google.gson.JsonObject;

/**
* @author cfstout
*/
public class GeoBoundsAggregation extends Aggregation {

public static final String TYPE = "geo_bounds";

private Double topLeftLat;
private Double topLeftLon;
private Double bottomRightLat;
private Double bottomRightLon;

public GeoBoundsAggregation(String name, JsonObject geoBoundsAggregation) {
this.name = name;
JsonObject bounds = geoBoundsAggregation.get("bounds").getAsJsonObject();
JsonObject topLeft = bounds.get("top_left").getAsJsonObject();
JsonObject bottomRight = bounds.get("bottom_right").getAsJsonObject();

topLeftLat = topLeft.get("lat").getAsDouble();
topLeftLon = topLeft.get("lon").getAsDouble();
bottomRightLat = bottomRight.get("lat").getAsDouble();
bottomRightLon = bottomRight.get("lon").getAsDouble();
}

public Double getTopLeftLat() {
return topLeftLat;
}

public Double getTopLeftLon() {
return topLeftLon;
}

public Double getBottomRightLat() {
return bottomRightLat;
}

public Double getBottomRightLon() {
return bottomRightLon;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.searchbox.core.search.aggregation;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

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

/**
* @author cfstout
*/
public class GeoDistanceAggregation extends Aggregation {

public static final String TYPE = "geo_distance";

private List<GeoDistance> geoDistances;

public GeoDistanceAggregation(String name, JsonObject rangeAggregation) {
this.name = name;
geoDistances = new ArrayList<GeoDistance>();
//todo support keyed:true as well
for (JsonElement bucketv : rangeAggregation.get("buckets").getAsJsonArray()) {
JsonObject bucket = bucketv.getAsJsonObject();
GeoDistance geoDistance = new GeoDistance(
bucket.has("from") ? bucket.get("from").getAsDouble() : null,
bucket.has("to") ? bucket.get("to").getAsDouble() : null,
bucket.has("count") ? bucket.get("doc_count").getAsLong() : null,
bucket.get("unit").getAsString());
geoDistances.add(geoDistance);
}
}

public class GeoDistance extends RangeAggregation.Range {
private String unit;

public GeoDistance(Double from, Double to, Long count, String unit) {
super(from, to, count);
this.unit = unit;
}

public String getUnit() {
return unit;
}
}
}
Loading

0 comments on commit fbb27f9

Please sign in to comment.