Skip to content

Commit 7bb0924

Browse files
committed
facet refactoring, default collector base post implementation
automatically implement post based on collector
1 parent ce6f0e2 commit 7bb0924

29 files changed

+40
-199
lines changed

src/main/java/org/elasticsearch/common/lucene/docset/ContextDocIdSet.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.lucene.search.DocIdSet;
2424

2525
/**
26+
* A holder for a {@link DocIdSet} and the {@link AtomicReaderContext} it is associated with.
2627
*/
2728
public class ContextDocIdSet {
2829

src/main/java/org/elasticsearch/search/facet/FacetExecutor.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.search.facet;
2121

2222
import org.apache.lucene.search.DocIdSet;
23+
import org.apache.lucene.search.DocIdSetIterator;
2324
import org.apache.lucene.search.Filter;
2425
import org.apache.lucene.search.Scorer;
2526
import org.elasticsearch.common.lucene.docset.AndDocIdSet;
@@ -34,8 +35,8 @@
3435
* A facet processor ends up actually executing the relevant facet for a specific
3536
* search request.
3637
* <p/>
37-
* The facet executor requires at least one of the {@link #collector()} or {@link #post()} methods to be
38-
* implemented.
38+
* The facet executor requires at least the {@link #collector()} method to be implemented,
39+
* with an optional {@link #post()} implementation if specific optimizations can be done.
3940
*/
4041
public abstract class FacetExecutor {
4142

@@ -46,6 +47,9 @@ public static abstract class Post {
4647

4748
public abstract void executePost(List<ContextDocIdSet> docSets) throws IOException;
4849

50+
/**
51+
* A filtered post execution.
52+
*/
4953
public static class Filtered extends Post {
5054

5155
private final Post post;
@@ -71,6 +75,32 @@ public void executePost(List<ContextDocIdSet> docSets) throws IOException {
7175
post.executePost(filteredEntries);
7276
}
7377
}
78+
79+
/**
80+
* A {@link FacetExecutor.Collector} based post.
81+
*/
82+
public static class Collector extends Post {
83+
84+
private final FacetExecutor.Collector collector;
85+
86+
public Collector(FacetExecutor.Collector collector) {
87+
this.collector = collector;
88+
}
89+
90+
@Override
91+
public void executePost(List<ContextDocIdSet> docSets) throws IOException {
92+
for (int i = 0; i < docSets.size(); i++) {
93+
ContextDocIdSet docSet = docSets.get(i);
94+
collector.setNextReader(docSet.context);
95+
DocIdSetIterator it = docSet.docSet.iterator();
96+
int doc;
97+
while ((doc = it.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
98+
collector.collect(doc);
99+
}
100+
}
101+
collector.postCollection();
102+
}
103+
}
74104
}
75105

76106
/**
@@ -117,7 +147,12 @@ public static enum Mode {
117147
public abstract Collector collector();
118148

119149
/**
120-
* A post based facet that executes the facet using the aggregated docs relevant.
150+
* A post based facet that executes the facet using the aggregated docs. By default
151+
* uses the {@link Post.Collector} based implementation.
152+
* <p/>
153+
* Can be overridden if a more optimized non collector based implementation can be implemented.
121154
*/
122-
public abstract Post post();
155+
public Post post() {
156+
return new Post.Collector(collector());
157+
}
123158
}

src/main/java/org/elasticsearch/search/facet/datehistogram/CountDateHistogramFacetExecutor.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,14 @@
2121

2222
import gnu.trove.map.hash.TLongLongHashMap;
2323
import org.apache.lucene.index.AtomicReaderContext;
24-
import org.apache.lucene.search.DocIdSetIterator;
2524
import org.elasticsearch.common.CacheRecycler;
2625
import org.elasticsearch.common.joda.TimeZoneRounding;
27-
import org.elasticsearch.common.lucene.docset.ContextDocIdSet;
2826
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
2927
import org.elasticsearch.index.fielddata.LongValues;
3028
import org.elasticsearch.search.facet.FacetExecutor;
3129
import org.elasticsearch.search.facet.InternalFacet;
3230

3331
import java.io.IOException;
34-
import java.util.List;
3532

3633
/**
3734
* A date histogram facet collector that uses the same field as the key as well as the
@@ -58,32 +55,11 @@ public Collector collector() {
5855
return new Collector();
5956
}
6057

61-
@Override
62-
public Post post() {
63-
return new Post();
64-
}
65-
6658
@Override
6759
public InternalFacet buildFacet(String facetName) {
6860
return new InternalCountDateHistogramFacet(facetName, comparatorType, counts, true);
6961
}
7062

71-
class Post extends FacetExecutor.Post {
72-
73-
@Override
74-
public void executePost(List<ContextDocIdSet> docSets) throws IOException {
75-
DateHistogramProc histoProc = new DateHistogramProc(counts, tzRounding);
76-
for (ContextDocIdSet docSet : docSets) {
77-
LongValues values = indexFieldData.load(docSet.context).getLongValues();
78-
DocIdSetIterator it = docSet.docSet.iterator();
79-
int doc;
80-
while ((doc = it.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
81-
values.forEachValueInDoc(doc, histoProc);
82-
}
83-
}
84-
}
85-
}
86-
8763
class Collector extends FacetExecutor.Collector {
8864

8965
private LongValues values;

src/main/java/org/elasticsearch/search/facet/datehistogram/ValueDateHistogramFacetExecutor.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
package org.elasticsearch.search.facet.datehistogram;
2121

2222
import org.apache.lucene.index.AtomicReaderContext;
23-
import org.apache.lucene.search.DocIdSetIterator;
2423
import org.elasticsearch.common.CacheRecycler;
2524
import org.elasticsearch.common.joda.TimeZoneRounding;
26-
import org.elasticsearch.common.lucene.docset.ContextDocIdSet;
2725
import org.elasticsearch.common.trove.ExtTLongObjectHashMap;
2826
import org.elasticsearch.index.fielddata.DoubleValues;
2927
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
@@ -32,7 +30,6 @@
3230
import org.elasticsearch.search.facet.InternalFacet;
3331

3432
import java.io.IOException;
35-
import java.util.List;
3633

3734
/**
3835
* A histogram facet collector that uses different fields for the key and the value.
@@ -60,34 +57,11 @@ public Collector collector() {
6057
return new Collector();
6158
}
6259

63-
@Override
64-
public Post post() {
65-
return new Post();
66-
}
67-
6860
@Override
6961
public InternalFacet buildFacet(String facetName) {
7062
return new InternalFullDateHistogramFacet(facetName, comparatorType, entries, true);
7163
}
7264

73-
class Post extends FacetExecutor.Post {
74-
75-
@Override
76-
public void executePost(List<ContextDocIdSet> docSets) throws IOException {
77-
DateHistogramProc histoProc = new DateHistogramProc(tzRounding, ValueDateHistogramFacetExecutor.this.entries);
78-
for (ContextDocIdSet docSet : docSets) {
79-
LongValues keyValues = keyIndexFieldData.load(docSet.context).getLongValues();
80-
histoProc.valueValues = valueIndexFieldData.load(docSet.context).getDoubleValues();
81-
82-
DocIdSetIterator it = docSet.docSet.iterator();
83-
int doc;
84-
while ((doc = it.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
85-
keyValues.forEachValueInDoc(doc, histoProc);
86-
}
87-
}
88-
}
89-
}
90-
9165
class Collector extends FacetExecutor.Collector {
9266

9367
private final DateHistogramProc histoProc;

src/main/java/org/elasticsearch/search/facet/datehistogram/ValueScriptDateHistogramFacetExecutor.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
package org.elasticsearch.search.facet.datehistogram;
2121

2222
import org.apache.lucene.index.AtomicReaderContext;
23-
import org.apache.lucene.search.DocIdSetIterator;
2423
import org.apache.lucene.search.Scorer;
2524
import org.elasticsearch.common.CacheRecycler;
2625
import org.elasticsearch.common.joda.TimeZoneRounding;
27-
import org.elasticsearch.common.lucene.docset.ContextDocIdSet;
2826
import org.elasticsearch.common.trove.ExtTLongObjectHashMap;
2927
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
3028
import org.elasticsearch.index.fielddata.LongValues;
@@ -33,7 +31,6 @@
3331
import org.elasticsearch.search.facet.InternalFacet;
3432

3533
import java.io.IOException;
36-
import java.util.List;
3734

3835
/**
3936
* A histogram facet collector that uses the same field as the key as well as the
@@ -62,33 +59,11 @@ public Collector collector() {
6259
return new Collector();
6360
}
6461

65-
@Override
66-
public Post post() {
67-
return new Post();
68-
}
69-
7062
@Override
7163
public InternalFacet buildFacet(String facetName) {
7264
return new InternalFullDateHistogramFacet(facetName, comparatorType, entries, true);
7365
}
7466

75-
class Post extends FacetExecutor.Post {
76-
77-
@Override
78-
public void executePost(List<ContextDocIdSet> docSets) throws IOException {
79-
DateHistogramProc histoProc = new DateHistogramProc(tzRounding, valueScript, ValueScriptDateHistogramFacetExecutor.this.entries);
80-
for (ContextDocIdSet entry : docSets) {
81-
LongValues keyValues = keyIndexFieldData.load(entry.context).getLongValues();
82-
valueScript.setNextReader(entry.context);
83-
DocIdSetIterator it = entry.docSet.iterator();
84-
int doc;
85-
while ((doc = it.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
86-
keyValues.forEachValueInDoc(doc, histoProc);
87-
}
88-
}
89-
}
90-
}
91-
9267
class Collector extends FacetExecutor.Collector {
9368

9469
private final DateHistogramProc histoProc;

src/main/java/org/elasticsearch/search/facet/geodistance/GeoDistanceFacetExecutor.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ public Collector collector() {
6060
return new Collector();
6161
}
6262

63-
@Override
64-
public Post post() {
65-
return null;
66-
}
67-
6863
@Override
6964
public InternalFacet buildFacet(String facetName) {
7065
return new InternalGeoDistanceFacet(facetName, entries);

src/main/java/org/elasticsearch/search/facet/geodistance/ScriptGeoDistanceFacetExecutor.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ public Collector collector() {
5050
return new Collector();
5151
}
5252

53-
@Override
54-
public Post post() {
55-
return null;
56-
}
57-
5853
class Collector extends GeoDistanceFacetExecutor.Collector {
5954

6055
private Aggregator scriptAggregator;

src/main/java/org/elasticsearch/search/facet/geodistance/ValueGeoDistanceFacetExecutor.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ public Collector collector() {
4848
return new Collector();
4949
}
5050

51-
@Override
52-
public Post post() {
53-
return null;
54-
}
55-
5651
class Collector extends GeoDistanceFacetExecutor.Collector {
5752

5853
Collector() {

src/main/java/org/elasticsearch/search/facet/histogram/CountHistogramFacetExecutor.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ public Collector collector() {
5555
return new Collector();
5656
}
5757

58-
@Override
59-
public Post post() {
60-
return null;
61-
}
62-
6358
@Override
6459
public InternalFacet buildFacet(String facetName) {
6560
return new InternalCountHistogramFacet(facetName, comparatorType, counts, true);

src/main/java/org/elasticsearch/search/facet/histogram/FullHistogramFacetExecutor.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ public Collector collector() {
5555
return new Collector();
5656
}
5757

58-
@Override
59-
public Post post() {
60-
return null;
61-
}
62-
6358
@Override
6459
public InternalFacet buildFacet(String facetName) {
6560
return new InternalFullHistogramFacet(facetName, comparatorType, entries, true);

0 commit comments

Comments
 (0)