forked from searchbox-io/Jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue searchbox-io#146 Adding aggregation support
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 11, 2015
1 parent
3f0f407
commit 5e34e0c
Showing
26 changed files
with
945 additions
and
8 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
jest-common/src/main/java/io/searchbox/core/search/aggregation/Aggregation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
jest-common/src/main/java/io/searchbox/core/search/aggregation/AvgAggregation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
jest-common/src/main/java/io/searchbox/core/search/aggregation/CardinalityAggregation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
51 changes: 51 additions & 0 deletions
51
jest-common/src/main/java/io/searchbox/core/search/aggregation/DateHistogramAggregation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
jest-common/src/main/java/io/searchbox/core/search/aggregation/DateRangeAggregation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
jest-common/src/main/java/io/searchbox/core/search/aggregation/ExtendedStatsAggregation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
jest-common/src/main/java/io/searchbox/core/search/aggregation/FilterAggregation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
jest-common/src/main/java/io/searchbox/core/search/aggregation/FiltersAggregation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
45 changes: 45 additions & 0 deletions
45
jest-common/src/main/java/io/searchbox/core/search/aggregation/GeoDistanceAggregation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
jest-common/src/main/java/io/searchbox/core/search/aggregation/GeoboundsAggregation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.