This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 728
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Aggregation_support' of https://github.com/cfstout/Jest …
…into aggregation-support
- Loading branch information
Showing
53 changed files
with
5,582 additions
and
0 deletions.
There are no files selected for viewing
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
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
261 changes: 261 additions & 0 deletions
261
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,261 @@ | ||
package io.searchbox.core.search.aggregation; | ||
|
||
import com.google.gson.JsonObject; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author cfstout | ||
*/ | ||
|
||
public class Aggregation <T extends Aggregation> { | ||
|
||
protected String name; | ||
protected JsonObject jsonRoot; | ||
|
||
public Aggregation(String name, JsonObject jsonRoot) { | ||
this.name = name; | ||
this.jsonRoot = jsonRoot; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* @param nameToTypeMap a map of aggNames to their expected type (extension of Aggregation class) | ||
* @return A list of aggregation objects for the provided name:type pairs if the name can be found in the root json object | ||
* @exception java.lang.RuntimeException if no constructor found for an expected type in the map | ||
*/ | ||
public List<T> getAggregations(Map<String, Class<T>> nameToTypeMap) { | ||
List<T> aggregations = new ArrayList<T>(); | ||
for (String nameCandidate : nameToTypeMap.keySet()) { | ||
if (jsonRoot.has(nameCandidate)) { | ||
try { | ||
Class<T> type = nameToTypeMap.get(nameCandidate); | ||
Constructor<T> c = type.getConstructor(String.class, JsonObject.class); | ||
aggregations.add(c.newInstance(nameCandidate, jsonRoot.getAsJsonObject(nameCandidate))); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
return aggregations; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the desired aggregation | ||
* @param aggType Extension of Aggregation class expected as return type | ||
* @return Aggregation of type T if aggName can be found in aggregation json or null otherwise | ||
* @exception java.lang.RuntimeException if no constructor exists for provided aggType | ||
*/ | ||
public T getAggregation(String aggName, Class<T> aggType) { | ||
if(jsonRoot.has(aggName)) { | ||
try { | ||
Constructor<T> c = aggType.getConstructor(String.class, JsonObject.class); | ||
return c.newInstance(aggName, jsonRoot.getAsJsonObject(aggName)); | ||
} catch(Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the AvgAggregation | ||
* @return a new AvgAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public AvgAggregation getAvgAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new AvgAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the CardinalityAggregation | ||
* @return a new CardinalityAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public CardinalityAggregation getCardinalityAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new CardinalityAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the DateHistogramAggregation | ||
* @return a new DateHistogramAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public DateHistogramAggregation getDateHistogramAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new DateHistogramAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the DateRangeAggregation | ||
* @return a new DateRangeAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public DateRangeAggregation getDateRangeAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new DateRangeAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the ExtendedStatsAggregation | ||
* @return a new ExtendedStatsAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public ExtendedStatsAggregation getExtendedStatsAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new ExtendedStatsAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the FilterAggregation | ||
* @return a new FilterAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public FilterAggregation getFilterAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new FilterAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the FiltersAggregation | ||
* @return a new FiltersAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public FiltersAggregation getFiltersAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new FiltersAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the GeoBoundsAggregation | ||
* @return a new GeoBoundsAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public GeoBoundsAggregation getGeoBoundsAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new GeoBoundsAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the GeoDistanceAggregation | ||
* @return a new GeoDistanceAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public GeoDistanceAggregation getGeoDistanceAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new GeoDistanceAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the GeohashGridAggregation | ||
* @return a new GeohashGridAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public GeohashGridAggregation getGeohashGridAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new GeohashGridAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the HistogramAggregation | ||
* @return a new HistogramAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public HistogramAggregation getHistogramAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new HistogramAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the Ipv4RangeAggregation | ||
* @return a new Ipv4RangeAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public Ipv4RangeAggregation getIpv4RangeAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new Ipv4RangeAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the MaxAggregation | ||
* @return a new MaxAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public MaxAggregation getMaxAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new MaxAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the MinAggregation | ||
* @return a new MinAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public MinAggregation getMinAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new MinAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the MissingAggregation | ||
* @return a new MissingAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public MissingAggregation getMissingAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new MissingAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the PercentileRanksAggregation | ||
* @return a new PercentileRanksAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public PercentileRanksAggregation getPercentileRanksAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new PercentileRanksAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the PercentilesAggregation | ||
* @return a new PercentilesAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public PercentilesAggregation getPercentilesAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new PercentilesAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the RangeAggregation | ||
* @return a new RangeAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public RangeAggregation getRangeAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new RangeAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the ScriptedMetricAggregation | ||
* @return a new ScriptedMetricAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public ScriptedMetricAggregation getScriptedMetricAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new ScriptedMetricAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the SignificantTermsAggregation | ||
* @return a new SignificantTermsAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public SignificantTermsAggregation getSignificantTermsAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new SignificantTermsAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the StatsAggregation | ||
* @return a new StatsAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public StatsAggregation getStatsAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new StatsAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the SumAggregation | ||
* @return a new SumAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public SumAggregation getSumAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new SumAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the TermsAggregation | ||
* @return a new TermsAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public TermsAggregation getTermsAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new TermsAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
/** | ||
* @param aggName Name of the ValueCountAggregation | ||
* @return a new ValueCountAggregation object if aggName is found in root json object or null if not found | ||
*/ | ||
public ValueCountAggregation getValueCountAggregation(String aggName) { | ||
return jsonRoot.has(aggName) ? new ValueCountAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; | ||
} | ||
|
||
|
||
|
||
} |
53 changes: 53 additions & 0 deletions
53
jest-common/src/main/java/io/searchbox/core/search/aggregation/AggregationField.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,53 @@ | ||
package io.searchbox.core.search.aggregation; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author cfstout | ||
*/ | ||
public enum AggregationField { | ||
VALUE("value"), | ||
BUCKETS("buckets"), | ||
KEY("key"), //Can be String or Long | ||
KEY_AS_STRING("key_as_string"), | ||
DOC_COUNT("doc_count"), | ||
FROM("from"), | ||
TO("to"), | ||
FROM_AS_STRING("from_as_string"), | ||
TO_AS_STRING("to_as_string"), | ||
SUM_OF_SQUARES("sum_of_squares"), | ||
VARIANCE("variance"), | ||
STD_DEVIATION("std_deviation"), | ||
BOUNDS("bounds"), | ||
TOP_LEFT("top_left"), | ||
BOTTOM_RIGHT("bottom_right"), | ||
LAT("lat"), | ||
LON("lon"), | ||
UNIT("unit"), | ||
VALUES("values"), | ||
SCORE("score"), | ||
BG_COUNT("bg_count"), //Background Count | ||
COUNT("count"), | ||
MIN("min"), | ||
MAX("max"), | ||
AVG("avg"), | ||
SUM("sum"), | ||
DOC_COUNT_ERROR_UPPER_BOUND("doc_count_error_upper_bound"), | ||
SUM_OTHER_DOC_COUNT("sum_other_doc_count"); | ||
|
||
private final String field; | ||
|
||
private AggregationField(String s) { | ||
field = s; | ||
} | ||
|
||
public String toString() { | ||
return field; | ||
} | ||
|
||
public boolean equals(String s) { | ||
return s.equals(toString()); | ||
} | ||
} | ||
|
35 changes: 35 additions & 0 deletions
35
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,35 @@ | ||
package io.searchbox.core.search.aggregation; | ||
|
||
import com.google.gson.JsonObject; | ||
|
||
import static io.searchbox.core.search.aggregation.AggregationField.VALUE; | ||
|
||
/** | ||
* @author cfstout | ||
*/ | ||
public class AvgAggregation extends SingleValueAggregation<AvgAggregation> { | ||
|
||
public static final String TYPE = "avg"; | ||
|
||
public <T extends Aggregation> AvgAggregation(String name, JsonObject avgAggregation) { | ||
super(name, avgAggregation); | ||
} | ||
|
||
/** | ||
* @return Average if it was found and not null, null otherwise | ||
*/ | ||
public Double getAvg() { | ||
return getValue(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof AvgAggregation)) { | ||
return false; | ||
} | ||
return super.equals(o); | ||
} | ||
} |
Oops, something went wrong.