Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
Merge branch 'Aggregation_support' of https://github.com/cfstout/Jest
Browse files Browse the repository at this point in the history
…into aggregation-support
  • Loading branch information
Cihat Keser committed Feb 26, 2015
2 parents cad1ffc + 440903c commit b8ddb83
Show file tree
Hide file tree
Showing 53 changed files with 5,582 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Thanks to [JetBrains][jetbrains] for providing a license for [IntelliJ IDEA][ide
We also would like to thank the following people for their significant contributions.
* [Andrea Turli](https://github.com/andreaturli)
* [asierdelpozo](https://github.com/asierdelpozo)
* [Clayton Stout](https://github.com/cfstout)
* [Dominic Tootell](https://github.com/tootedom)
* [Erik Schuchmann](https://github.com/eschuchmann)
* [Filippo Rossoni](https://github.com/filippor)
Expand Down
10 changes: 10 additions & 0 deletions jest-common/src/main/java/io/searchbox/core/SearchResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.searchbox.client.JestResult;
import io.searchbox.core.search.aggregation.Aggregation;
import io.searchbox.core.search.facet.Facet;

import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -176,6 +177,15 @@ public <T extends Facet> List<T> getFacets(Class<T> type) {
return facets;
}

public Aggregation getAggregations() {
final String rootAggrgationName = "aggs";
if (jsonObject == null) return new Aggregation(rootAggrgationName, new JsonObject());
if (jsonObject.has("aggregations")) return new Aggregation(rootAggrgationName, jsonObject.getAsJsonObject("aggregations"));
if (jsonObject.has("aggs")) return new Aggregation(rootAggrgationName, jsonObject.getAsJsonObject("aggs"));

return new Aggregation(rootAggrgationName, new JsonObject());
}

/**
* Immutable class representing a search hit.
*
Expand Down
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;
}



}
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());
}
}

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);
}
}

0 comments on commit b8ddb83

Please sign in to comment.