Skip to content

Commit

Permalink
Aggregations: Encapsulate AggregationBuilder name and make getter public
Browse files Browse the repository at this point in the history
Close #7425
  • Loading branch information
Philip Wills authored and areek committed Sep 8, 2014
1 parent ef67e80 commit dc56e9c
Show file tree
Hide file tree
Showing 15 changed files with 19 additions and 16 deletions.
Expand Up @@ -25,12 +25,15 @@
*/
public abstract class AbstractAggregationBuilder implements ToXContent {

protected final String name;
private final String name;
protected final String type;

protected AbstractAggregationBuilder(String name, String type) {
this.name = name;
this.type = type;
}

public String getName() {
return name;
}
}
Expand Up @@ -100,7 +100,7 @@ public B subAggregation(Map<String, Object> aggs) {

@Override
public final XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(name);
builder.startObject(getName());

builder.field(type);
internalXContent(builder, params);
Expand Down
Expand Up @@ -43,7 +43,7 @@ public ChildrenBuilder childType(String childType) {
protected XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
if (childType == null) {
throw new SearchSourceBuilderException("child_type must be set on children aggregation [" + name + "]");
throw new SearchSourceBuilderException("child_type must be set on children aggregation [" + getName() + "]");
}
builder.field("type", childType);
return builder.endObject();
Expand Down
Expand Up @@ -45,7 +45,7 @@ public FilterAggregationBuilder filter(FilterBuilder filter) {
@Override
protected XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException {
if (filter == null) {
throw new SearchSourceBuilderException("filter must be set on filter aggregation [" + name + "]");
throw new SearchSourceBuilderException("filter must be set on filter aggregation [" + getName() + "]");
}
filter.toXContent(builder, params);
return builder;
Expand Down
Expand Up @@ -63,7 +63,7 @@ public FiltersAggregationBuilder filter(FilterBuilder filter) {
protected XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
if (keyedFilters == null && nonKeyedFilters == null) {
throw new SearchSourceBuilderException("At least one filter must be set on filter aggregation [" + name + "]");
throw new SearchSourceBuilderException("At least one filter must be set on filter aggregation [" + getName() + "]");
}
if (keyedFilters != null && nonKeyedFilters != null) {
throw new SearchSourceBuilderException("Cannot add both keyed and non-keyed filters to filters aggregation");
Expand Down
Expand Up @@ -125,7 +125,7 @@ public DateHistogramBuilder extendedBounds(DateTime min, DateTime max) {
@Override
protected XContentBuilder doInternalXContent(XContentBuilder builder, Params params) throws IOException {
if (interval == null) {
throw new SearchSourceBuilderException("[interval] must be defined for histogram aggregation [" + name + "]");
throw new SearchSourceBuilderException("[interval] must be defined for histogram aggregation [" + getName() + "]");
}
if (interval instanceof Number) {
interval = TimeValue.timeValueMillis(((Number) interval).longValue()).toString();
Expand Down
Expand Up @@ -99,7 +99,7 @@ public HistogramBuilder postOffset(long postOffset) {
@Override
protected XContentBuilder doInternalXContent(XContentBuilder builder, Params params) throws IOException {
if (interval == null) {
throw new SearchSourceBuilderException("[interval] must be defined for histogram aggregation [" + name + "]");
throw new SearchSourceBuilderException("[interval] must be defined for histogram aggregation [" + getName() + "]");
}
builder.field("interval", interval);

Expand Down
Expand Up @@ -45,7 +45,7 @@ public NestedBuilder path(String path) {
protected XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
if (path == null) {
throw new SearchSourceBuilderException("nested path must be set on nested aggregation [" + name + "]");
throw new SearchSourceBuilderException("nested path must be set on nested aggregation [" + getName() + "]");
}
builder.field("path", path);
return builder.endObject();
Expand Down
Expand Up @@ -70,7 +70,7 @@ protected AbstractRangeBuilder(String name, String type) {
@Override
protected XContentBuilder doInternalXContent(XContentBuilder builder, Params params) throws IOException {
if (ranges.isEmpty()) {
throw new SearchSourceBuilderException("at least one range must be defined for range aggregation [" + name + "]");
throw new SearchSourceBuilderException("at least one range must be defined for range aggregation [" + getName() + "]");
}
builder.startArray("ranges");
for (Range range : ranges) {
Expand Down
Expand Up @@ -156,10 +156,10 @@ public GeoDistanceBuilder addUnboundedFrom(double from) {
protected XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
if (ranges.isEmpty()) {
throw new SearchSourceBuilderException("at least one range must be defined for geo_distance aggregation [" + name + "]");
throw new SearchSourceBuilderException("at least one range must be defined for geo_distance aggregation [" + getName() + "]");
}
if (point == null) {
throw new SearchSourceBuilderException("center point must be defined for geo_distance aggregation [" + name + "]");
throw new SearchSourceBuilderException("center point must be defined for geo_distance aggregation [" + getName() + "]");
}

if (field != null) {
Expand Down
Expand Up @@ -47,7 +47,7 @@ public IPv4RangeBuilder addMaskRange(String mask) {
public IPv4RangeBuilder addMaskRange(String key, String mask) {
long[] fromTo = cidrMaskToMinMax(mask);
if (fromTo == null) {
throw new SearchSourceBuilderException("invalid CIDR mask [" + mask + "] in ip_range aggregation [" + name + "]");
throw new SearchSourceBuilderException("invalid CIDR mask [" + mask + "] in ip_range aggregation [" + getName() + "]");
}
ranges.add(new Range(key, fromTo[0] < 0 ? null : fromTo[0], fromTo[1] < 0 ? null : fromTo[1]));
return this;
Expand Down
Expand Up @@ -35,7 +35,7 @@ public MetricsAggregationBuilder(String name, String type) {

@Override
public final XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(name).startObject(type);
builder.startObject(getName()).startObject(type);
internalXContent(builder, params);
return builder.endObject().endObject();
}
Expand Down
Expand Up @@ -39,7 +39,7 @@ public PercentilesBuilder percentiles(double... percentiles) {
for (int i = 0; i < percentiles.length; i++) {
if (percentiles[i] < 0 || percentiles[i] > 100) {
throw new IllegalArgumentException("the percents in the percentiles aggregation [" +
name + "] must be in the [0, 100] range");
getName() + "] must be in the [0, 100] range");
}
}
this.percentiles = percentiles;
Expand Down
Expand Up @@ -376,7 +376,7 @@ public TopHitsBuilder setHighlighterOptions(Map<String, Object> options) {

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(name).field(type);
builder.startObject(getName()).field(type);
sourceBuilder().toXContent(builder, params);
return builder.endObject();
}
Expand Down
Expand Up @@ -182,7 +182,7 @@ public void singleValuedField_WithPostTimeZone() throws Exception {
.addAggregation(new AbstractAggregationBuilder("histo", "date_histogram") {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return builder.startObject(name)
return builder.startObject(getName())
.startObject(type)
.field("field", "date")
.field("interval", "1d")
Expand Down

0 comments on commit dc56e9c

Please sign in to comment.