|
19 | 19 | import java.util.HashMap;
|
20 | 20 | import java.util.List;
|
21 | 21 | import java.util.Map;
|
22 |
| - |
23 | 22 | import org.elasticsearch.search.aggregations.Aggregation;
|
| 23 | +import org.elasticsearch.search.aggregations.Aggregations; |
24 | 24 | import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
|
25 | 25 | import org.elasticsearch.search.aggregations.bucket.range.Range;
|
26 | 26 | import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
|
33 | 33 | import org.springframework.data.elasticsearch.core.facet.AbstractFacetRequest;
|
34 | 34 | import org.springframework.data.elasticsearch.core.facet.FacetResult;
|
35 | 35 | import org.springframework.data.elasticsearch.core.facet.request.RangeFacetRequest;
|
36 |
| -import org.springframework.data.elasticsearch.core.facet.result.*; |
| 36 | +import org.springframework.data.elasticsearch.core.facet.result.HistogramResult; |
| 37 | +import org.springframework.data.elasticsearch.core.facet.result.IntervalUnit; |
| 38 | +import org.springframework.data.elasticsearch.core.facet.result.RangeResult; |
| 39 | +import org.springframework.data.elasticsearch.core.facet.result.StatisticalResult; |
| 40 | +import org.springframework.data.elasticsearch.core.facet.result.Term; |
| 41 | +import org.springframework.data.elasticsearch.core.facet.result.TermResult; |
37 | 42 |
|
38 | 43 | /**
|
39 | 44 | * Container for query result and facet results
|
|
42 | 47 | * @author Mohsin Husen
|
43 | 48 | * @author Artur Konczak
|
44 | 49 | * @author Jonathan Yan
|
| 50 | + * @author Philipp Kräutli |
45 | 51 | */
|
46 | 52 | @Deprecated
|
47 | 53 | public abstract class FacetedPageImpl<T> extends PageImpl<T> implements FacetedPage<T>, AggregatedPage<T> {
|
@@ -84,47 +90,76 @@ private void addFacet(FacetResult facetResult) {
|
84 | 90 | * Lazy conversion from aggregation to old facets
|
85 | 91 | */
|
86 | 92 | private void processAggregations() {
|
87 |
| - if (facets == null) { |
88 |
| - facets = new ArrayList<>(); |
89 |
| - for (Aggregation agg : getAggregations()) { |
90 |
| - if (agg instanceof Terms) { |
91 |
| - List<Term> terms = new ArrayList<>(); |
92 |
| - for (Terms.Bucket t : ((Terms) agg).getBuckets()) { |
93 |
| - terms.add(new Term(t.getKeyAsString(), t.getDocCount())); |
94 |
| - } |
95 |
| - addFacet(new TermResult(agg.getName(), terms, terms.size(), ((Terms) agg).getSumOfOtherDocCounts(), 0)); |
96 |
| - } |
97 |
| - if (agg instanceof Range) { |
98 |
| - List<? extends Range.Bucket> buckets = ((Range) agg).getBuckets(); |
99 |
| - List<org.springframework.data.elasticsearch.core.facet.result.Range> ranges = new ArrayList<>(); |
100 |
| - for (Range.Bucket b : buckets) { |
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 |
| - } |
108 |
| - } |
109 |
| - addFacet(new RangeResult(agg.getName(), ranges)); |
110 |
| - } |
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())); |
114 |
| - } |
115 |
| - if (agg instanceof Histogram) { |
116 |
| - List<IntervalUnit> intervals = new ArrayList<>(); |
117 |
| - for (Histogram.Bucket h : ((Histogram) agg).getBuckets()) { |
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 |
| - } |
124 |
| - } |
125 |
| - addFacet(new HistogramResult(agg.getName(), intervals)); |
126 |
| - } |
| 93 | + if (facets != null) { |
| 94 | + return; |
| 95 | + } |
| 96 | + facets = new ArrayList<>(); |
| 97 | + Aggregations aggregations = getAggregations(); |
| 98 | + if (aggregations == null) { |
| 99 | + return; |
| 100 | + } |
| 101 | + for (Aggregation agg : aggregations) { |
| 102 | + processAggregation(agg); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private void processAggregation(Aggregation agg) |
| 107 | + { |
| 108 | + if (agg instanceof Terms) { |
| 109 | + processTermAggregation((Terms) agg); |
| 110 | + } |
| 111 | + if (agg instanceof Range) { |
| 112 | + processRangeAggregation((Range) agg); |
| 113 | + } |
| 114 | + if (agg instanceof ExtendedStats) { |
| 115 | + processExtendedStatsAggregation((ExtendedStats) agg); |
| 116 | + } |
| 117 | + if (agg instanceof Histogram) { |
| 118 | + processHistogramAggregation((Histogram) agg); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private void processTermAggregation(Terms agg) |
| 123 | + { |
| 124 | + List<Term> terms = new ArrayList<>(); |
| 125 | + for (Terms.Bucket t : agg.getBuckets()) { |
| 126 | + terms.add(new Term(t.getKeyAsString(), t.getDocCount())); |
| 127 | + } |
| 128 | + addFacet(new TermResult(agg.getName(), terms, terms.size(), agg.getSumOfOtherDocCounts(), 0)); |
| 129 | + } |
| 130 | + |
| 131 | + private void processRangeAggregation(Range agg) |
| 132 | + { |
| 133 | + List<? extends Range.Bucket> buckets = ((Range) agg).getBuckets(); |
| 134 | + List<org.springframework.data.elasticsearch.core.facet.result.Range> ranges = new ArrayList<>(); |
| 135 | + for (Range.Bucket b : buckets) { |
| 136 | + ExtendedStats rStats = b.getAggregations().get(AbstractFacetRequest.INTERNAL_STATS); |
| 137 | + if (rStats != null) { |
| 138 | + Sum sum = b.getAggregations().get(RangeFacetRequest.RANGE_INTERNAL_SUM); |
| 139 | + 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())); |
| 140 | + } else { |
| 141 | + ranges.add(new org.springframework.data.elasticsearch.core.facet.result.Range((Double) b.getFrom(), (Double) b.getTo(), b.getDocCount(), 0, 0, 0, 0)); |
| 142 | + } |
| 143 | + } |
| 144 | + addFacet(new RangeResult(agg.getName(), ranges)); |
| 145 | + } |
| 146 | + |
| 147 | + private void processExtendedStatsAggregation(ExtendedStats agg) |
| 148 | + { |
| 149 | + addFacet(new StatisticalResult(agg.getName(), agg.getCount(), agg.getMax(), agg.getMin(), agg.getAvg(), agg.getStdDeviation(), agg.getSumOfSquares(), agg.getSum(), agg.getVariance())); |
| 150 | + } |
| 151 | + |
| 152 | + private void processHistogramAggregation(Histogram agg) |
| 153 | + { |
| 154 | + List<IntervalUnit> intervals = new ArrayList<>(); |
| 155 | + for (Histogram.Bucket h : agg.getBuckets()) { |
| 156 | + ExtendedStats hStats = h.getAggregations().get(AbstractFacetRequest.INTERNAL_STATS); |
| 157 | + if (hStats != null) { |
| 158 | + intervals.add(new IntervalUnit(((DateTime) h.getKey()).getMillis(), h.getDocCount(), h.getDocCount(), hStats.getSum(), hStats.getAvg(), hStats.getMin(), hStats.getMax())); |
| 159 | + } else { |
| 160 | + intervals.add(new IntervalUnit(((DateTime) h.getKey()).getMillis(), h.getDocCount(), h.getDocCount(), 0, 0, 0, 0)); |
127 | 161 | }
|
128 | 162 | }
|
| 163 | + addFacet(new HistogramResult(agg.getName(), intervals)); |
129 | 164 | }
|
130 | 165 | }
|
0 commit comments