Skip to content

Commit 392a87a

Browse files
committed
DATAES-180 - added faceted page back powered by aggregation
1 parent 6ffac62 commit 392a87a

File tree

8 files changed

+122
-216
lines changed

8 files changed

+122
-216
lines changed

src/main/java/org/springframework/data/elasticsearch/core/FacetedPageImpl.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@
2424
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
2525
import org.elasticsearch.search.aggregations.bucket.range.Range;
2626
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
27-
import org.elasticsearch.search.aggregations.metrics.stats.Stats;
27+
import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats;
28+
import org.elasticsearch.search.aggregations.metrics.sum.Sum;
29+
import org.joda.time.DateTime;
2830
import org.springframework.data.domain.PageImpl;
2931
import org.springframework.data.domain.Pageable;
3032
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
33+
import org.springframework.data.elasticsearch.core.facet.AbstractFacetRequest;
3134
import org.springframework.data.elasticsearch.core.facet.FacetResult;
35+
import org.springframework.data.elasticsearch.core.facet.request.RangeFacetRequest;
3236
import org.springframework.data.elasticsearch.core.facet.result.*;
3337

3438
/**
@@ -88,24 +92,35 @@ private void processAggregations() {
8892
for (Terms.Bucket t : ((Terms) agg).getBuckets()) {
8993
terms.add(new Term(t.getKeyAsString(), t.getDocCount()));
9094
}
91-
addFacet(new TermResult(agg.getName(), terms, terms.size(), ((Terms) agg).getSumOfOtherDocCounts(), -1));
95+
addFacet(new TermResult(agg.getName(), terms, terms.size(), ((Terms) agg).getSumOfOtherDocCounts(), 0));
9296
}
9397
if (agg instanceof Range) {
9498
List<? extends Range.Bucket> buckets = ((Range) agg).getBuckets();
9599
List<org.springframework.data.elasticsearch.core.facet.result.Range> ranges = new ArrayList<org.springframework.data.elasticsearch.core.facet.result.Range>();
96100
for (Range.Bucket b : buckets) {
97-
ranges.add(new org.springframework.data.elasticsearch.core.facet.result.Range((Double) b.getFrom(), (Double) b.getTo(), b.getDocCount(), 0, 0, 0, 0));
101+
ExtendedStats rStats = (ExtendedStats) b.getAggregations().get(AbstractFacetRequest.INTERNAL_STATS);
102+
if (rStats != null) {
103+
Sum sum = (Sum) b.getAggregations().get(RangeFacetRequest.RANGE_INTERNAL_SUM);
104+
ranges.add(new org.springframework.data.elasticsearch.core.facet.result.Range((Double) b.getFrom(), (Double) b.getTo(), b.getDocCount(), sum != null ? sum.getValue() : rStats.getSum(), rStats.getCount(), rStats.getMin(), rStats.getMax()));
105+
} else {
106+
ranges.add(new org.springframework.data.elasticsearch.core.facet.result.Range((Double) b.getFrom(), (Double) b.getTo(), b.getDocCount(), 0, 0, 0, 0));
107+
}
98108
}
99109
addFacet(new RangeResult(agg.getName(), ranges));
100110
}
101-
if (agg instanceof Stats) {
102-
Stats stats = (Stats) agg;
103-
addFacet(new StatisticalResult(agg.getName(), stats.getCount(), stats.getMax(), stats.getMin(), stats.getAvg(), -1, -1, stats.getSum(), -1));
111+
if (agg instanceof ExtendedStats) {
112+
ExtendedStats stats = (ExtendedStats) agg;
113+
addFacet(new StatisticalResult(agg.getName(), stats.getCount(), stats.getMax(), stats.getMin(), stats.getAvg(), stats.getStdDeviation(), stats.getSumOfSquares(), stats.getSum(), stats.getVariance()));
104114
}
105115
if (agg instanceof Histogram) {
106116
List<IntervalUnit> intervals = new ArrayList<IntervalUnit>();
107117
for (Histogram.Bucket h : ((Histogram) agg).getBuckets()) {
108-
new IntervalUnit((Long) h.getKey(), h.getDocCount(), h.getDocCount(), -1, -1, -1, -1);
118+
ExtendedStats hStats = (ExtendedStats) h.getAggregations().get(AbstractFacetRequest.INTERNAL_STATS);
119+
if (hStats != null) {
120+
intervals.add(new IntervalUnit(((DateTime) h.getKey()).getMillis(), h.getDocCount(), h.getDocCount(), hStats.getSum(), hStats.getAvg(), hStats.getMin(), hStats.getMax()));
121+
} else {
122+
intervals.add(new IntervalUnit(((DateTime) h.getKey()).getMillis(), h.getDocCount(), h.getDocCount(), 0, 0, 0, 0));
123+
}
109124
}
110125
addFacet(new HistogramResult(agg.getName(), intervals));
111126
}

src/main/java/org/springframework/data/elasticsearch/core/facet/AbstractFacetRequest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
@Deprecated
2626
public abstract class AbstractFacetRequest implements FacetRequest {
2727

28+
public static final String INTERNAL_STATS = "internal-stats";
29+
2830
private String name;
2931
private boolean applyQueryFilter;
3032

src/main/java/org/springframework/data/elasticsearch/core/facet/DefaultFacetMapper.java

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/main/java/org/springframework/data/elasticsearch/core/facet/request/HistogramFacetRequest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public AbstractAggregationBuilder getFacet() {
6666
dateHistogramBuilder.interval(interval);
6767
}
6868

69+
dateHistogramBuilder.subAggregation(AggregationBuilders.extendedStats(INTERNAL_STATS));
70+
6971
return dateHistogramBuilder;
7072
}
7173
}

src/main/java/org/springframework/data/elasticsearch/core/facet/request/RangeFacetRequest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import org.apache.commons.lang.StringUtils;
2223
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
2324
import org.elasticsearch.search.aggregations.AggregationBuilders;
2425
import org.elasticsearch.search.aggregations.bucket.range.RangeBuilder;
@@ -35,6 +36,7 @@
3536
@Deprecated
3637
public class RangeFacetRequest extends AbstractFacetRequest {
3738

39+
public static final String RANGE_INTERNAL_SUM = "range-internal-sum";
3840
private String field;
3941
private String keyField;
4042
private String valueField;
@@ -50,7 +52,8 @@ public void setField(String field) {
5052
}
5153

5254
public void setFields(String keyField, String valueField) {
53-
throw new UnsupportedOperationException("Native Facet are not supported in Elasticsearch 2.x - use Aggregation");
55+
this.keyField = keyField;
56+
this.valueField = valueField;
5457
}
5558

5659
public void range(Double from, Double to) {
@@ -74,13 +77,18 @@ public AbstractAggregationBuilder getFacet() {
7477
Assert.notNull(getName(), "Facet name can't be a null !!!");
7578

7679
RangeBuilder rangeBuilder = AggregationBuilders.range(getName());
77-
rangeBuilder.field(field);
80+
rangeBuilder.field(StringUtils.isNotBlank(keyField) ? keyField : field );
7881

7982
for (Entry entry : entries) {
8083
DoubleEntry doubleEntry = (DoubleEntry) entry;
8184
rangeBuilder.addRange(validateValue(doubleEntry.getFrom(), Double.NEGATIVE_INFINITY), validateValue(doubleEntry.getTo(), Double.POSITIVE_INFINITY));
8285
}
8386

87+
rangeBuilder.subAggregation(AggregationBuilders.extendedStats(INTERNAL_STATS));
88+
if(StringUtils.isNotBlank(valueField)){
89+
rangeBuilder.subAggregation(AggregationBuilders.sum(RANGE_INTERNAL_SUM).field(valueField));
90+
}
91+
8492
return rangeBuilder;
8593
}
8694

src/main/java/org/springframework/data/elasticsearch/core/facet/request/StatisticalFacetRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ public void setFields(String... fields) {
4848
public AbstractAggregationBuilder getFacet() {
4949
Assert.notNull(getName(), "Facet name can't be a null !!!");
5050
Assert.isTrue(StringUtils.isNotBlank(field) && fields == null, "Please select field or fields on which to build the facets !!!");
51-
return AggregationBuilders.stats(getName()).field(field);
51+
return AggregationBuilders.extendedStats(getName()).field(field);
5252
}
5353
}

src/main/java/org/springframework/data/elasticsearch/core/facet/request/TermFacetRequest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public TermFacetRequest(String name) {
4646
}
4747

4848
public void setFields(String... fields) {
49-
Assert.isTrue(ArrayUtils.isNotEmpty(fields), "Term agg need one field");
50-
Assert.isTrue(ArrayUtils.getLength(fields) == 1, "Term agg need one field");
49+
Assert.isTrue(ArrayUtils.isNotEmpty(fields), "Term agg need one field only");
50+
Assert.isTrue(ArrayUtils.getLength(fields) == 1, "Term agg need one field only");
5151
this.fields = fields;
5252
}
5353

@@ -84,11 +84,11 @@ public AbstractAggregationBuilder getFacet() {
8484
case ascTerm:
8585
termsBuilder.order(Terms.Order.term(true));
8686
break;
87-
case ascCount:
88-
termsBuilder.order(Terms.Order.count(true));
87+
case descCount:
88+
termsBuilder.order(Terms.Order.count(false));
8989
break;
9090
default:
91-
termsBuilder.order(Terms.Order.count(false));
91+
termsBuilder.order(Terms.Order.count(true));
9292
}
9393
if (ArrayUtils.isNotEmpty(excludeTerms)) {
9494
termsBuilder.exclude(excludeTerms);

0 commit comments

Comments
 (0)