Skip to content

Commit 73a447d

Browse files
committed
initial facet refactoring
the main goal of the facet refactoring is to allow for two modes of facet execution, collector based, that get callbacks as hist match, and post based, which iterates over all the relevant hits it also includes a some simplification of the facet implementation
1 parent 06b82a4 commit 73a447d

File tree

126 files changed

+4293
-4995
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+4293
-4995
lines changed

src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import org.elasticsearch.index.query.QueryBuilder;
3434
import org.elasticsearch.search.Scroll;
3535
import org.elasticsearch.search.builder.SearchSourceBuilder;
36-
import org.elasticsearch.search.facet.AbstractFacetBuilder;
36+
import org.elasticsearch.search.facet.FacetBuilder;
3737
import org.elasticsearch.search.highlight.HighlightBuilder;
3838
import org.elasticsearch.search.rescore.RescoreBuilder;
3939
import org.elasticsearch.search.sort.SortBuilder;
@@ -485,7 +485,7 @@ public SearchRequestBuilder addFields(String... fields) {
485485
/**
486486
* Adds a facet to the search operation.
487487
*/
488-
public SearchRequestBuilder addFacet(AbstractFacetBuilder facet) {
488+
public SearchRequestBuilder addFacet(FacetBuilder facet) {
489489
sourceBuilder().facet(facet);
490490
return this;
491491
}
@@ -663,17 +663,17 @@ public SearchRequestBuilder addSuggestion(SuggestBuilder.Suggestion suggestion)
663663
suggestBuilder().addSuggestion(suggestion);
664664
return this;
665665
}
666-
666+
667667
public SearchRequestBuilder setRescorer(RescoreBuilder.Rescorer rescorer) {
668668
rescoreBuilder().setRescorer(rescorer);
669669
return this;
670670
}
671-
671+
672672
public SearchRequestBuilder setRescoreWindow(int window) {
673673
rescoreBuilder().setWindowSize(window);
674674
return this;
675675
}
676-
676+
677677
/**
678678
* Sets the source of the request as a json string. Note, settings anything other
679679
* than the search type will cause this source to be overridden, consider using
@@ -850,7 +850,7 @@ private HighlightBuilder highlightBuilder() {
850850
private SuggestBuilder suggestBuilder() {
851851
return sourceBuilder().suggest();
852852
}
853-
853+
854854
private RescoreBuilder rescoreBuilder() {
855855
return sourceBuilder().rescore();
856856
}

src/main/java/org/elasticsearch/common/lucene/MultiCollector.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
import org.apache.lucene.search.Collector;
2424
import org.apache.lucene.search.ScoreCachingWrappingScorer;
2525
import org.apache.lucene.search.Scorer;
26+
import org.elasticsearch.common.lucene.search.XCollector;
2627

2728
import java.io.IOException;
2829

2930
/**
3031
*
3132
*/
32-
public class MultiCollector extends Collector {
33+
public class MultiCollector extends XCollector {
3334

3435
private final Collector collector;
3536

@@ -80,4 +81,16 @@ public boolean acceptsDocsOutOfOrder() {
8081
}
8182
return true;
8283
}
84+
85+
@Override
86+
public void postCollection() {
87+
if (collector instanceof XCollector) {
88+
((XCollector) collector).postCollection();
89+
}
90+
for (Collector collector : collectors) {
91+
if (collector instanceof XCollector) {
92+
((XCollector) collector).postCollection();
93+
}
94+
}
95+
}
8396
}

src/main/java/org/elasticsearch/search/facet/FacetCollector.java renamed to src/main/java/org/elasticsearch/common/lucene/docset/ContextDocIdSet.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,20 @@
1717
* under the License.
1818
*/
1919

20-
package org.elasticsearch.search.facet;
20+
package org.elasticsearch.common.lucene.docset;
2121

22-
import org.apache.lucene.search.Collector;
23-
import org.apache.lucene.search.Filter;
22+
import org.apache.lucene.index.AtomicReaderContext;
23+
import org.apache.lucene.search.DocIdSet;
2424

2525
/**
26-
*
2726
*/
28-
public abstract class FacetCollector extends Collector {
27+
public class ContextDocIdSet {
2928

30-
public abstract Facet facet();
29+
public final AtomicReaderContext context;
30+
public final DocIdSet docSet;
3131

32-
public abstract void setFilter(Filter filter);
32+
public ContextDocIdSet(AtomicReaderContext context, DocIdSet docSet) {
33+
this.context = context;
34+
this.docSet = docSet;
35+
}
3336
}

src/main/java/org/elasticsearch/common/lucene/search/FilteredCollector.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
/**
3232
*
3333
*/
34-
public class FilteredCollector extends Collector {
34+
public class FilteredCollector extends XCollector {
3535

3636
private final Collector collector;
3737

@@ -44,6 +44,13 @@ public FilteredCollector(Collector collector, Filter filter) {
4444
this.filter = filter;
4545
}
4646

47+
@Override
48+
public void postCollection() {
49+
if (collector instanceof XCollector) {
50+
((XCollector) collector).postCollection();
51+
}
52+
}
53+
4754
@Override
4855
public void setScorer(Scorer scorer) throws IOException {
4956
collector.setScorer(scorer);

src/main/java/org/elasticsearch/search/facet/FacetProcessor.java renamed to src/main/java/org/elasticsearch/common/lucene/search/XCollector.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,17 @@
1717
* under the License.
1818
*/
1919

20-
package org.elasticsearch.search.facet;
20+
package org.elasticsearch.common.lucene.search;
2121

22-
import org.elasticsearch.common.xcontent.XContentParser;
23-
import org.elasticsearch.search.internal.SearchContext;
24-
25-
import java.io.IOException;
22+
import org.apache.lucene.search.Collector;
2623

2724
/**
28-
*
25+
* An extension to {@link Collector} that allows for a callback when
26+
* collection is done.
2927
*/
30-
public interface FacetProcessor {
28+
public abstract class XCollector extends Collector {
3129

32-
String[] types();
30+
public void postCollection() {
3331

34-
FacetCollector parse(String facetName, XContentParser parser, SearchContext context) throws IOException;
32+
}
3533
}

src/main/java/org/elasticsearch/index/cache/IndexCache.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.elasticsearch.common.unit.TimeValue;
3232
import org.elasticsearch.index.AbstractIndexComponent;
3333
import org.elasticsearch.index.Index;
34+
import org.elasticsearch.index.cache.docset.DocSetCache;
3435
import org.elasticsearch.index.cache.filter.FilterCache;
3536
import org.elasticsearch.index.cache.id.IdCache;
3637
import org.elasticsearch.index.cache.query.parser.QueryParserCache;
@@ -42,24 +43,24 @@
4243
public class IndexCache extends AbstractIndexComponent implements CloseableComponent, ClusterStateListener {
4344

4445
private final FilterCache filterCache;
45-
4646
private final QueryParserCache queryParserCache;
47-
4847
private final IdCache idCache;
48+
private final DocSetCache docSetCache;
4949

5050
private final TimeValue refreshInterval;
51-
5251
private ClusterService clusterService;
5352

5453
private long latestCacheStatsTimestamp = -1;
5554
private CacheStats latestCacheStats;
5655

5756
@Inject
58-
public IndexCache(Index index, @IndexSettings Settings indexSettings, FilterCache filterCache, QueryParserCache queryParserCache, IdCache idCache) {
57+
public IndexCache(Index index, @IndexSettings Settings indexSettings, FilterCache filterCache, QueryParserCache queryParserCache, IdCache idCache,
58+
DocSetCache docSetCache) {
5959
super(index, indexSettings);
6060
this.filterCache = filterCache;
6161
this.queryParserCache = queryParserCache;
6262
this.idCache = idCache;
63+
this.docSetCache = docSetCache;
6364

6465
this.refreshInterval = componentSettings.getAsTime("stats.refresh_interval", TimeValue.timeValueSeconds(1));
6566

@@ -94,6 +95,10 @@ public FilterCache filter() {
9495
return filterCache;
9596
}
9697

98+
public DocSetCache docSet() {
99+
return this.docSetCache;
100+
}
101+
97102
public IdCache idCache() {
98103
return this.idCache;
99104
}
@@ -107,6 +112,7 @@ public void close() throws ElasticSearchException {
107112
filterCache.close();
108113
idCache.close();
109114
queryParserCache.close();
115+
docSetCache.clear("close");
110116
if (clusterService != null) {
111117
clusterService.remove(this);
112118
}
@@ -115,12 +121,14 @@ public void close() throws ElasticSearchException {
115121
public void clear(IndexReader reader) {
116122
filterCache.clear(reader);
117123
idCache.clear(reader);
124+
docSetCache.clear(reader);
118125
}
119126

120127
public void clear(String reason) {
121128
filterCache.clear(reason);
122129
idCache.clear();
123130
queryParserCache.clear();
131+
docSetCache.clear(reason);
124132
}
125133

126134
@Override

src/main/java/org/elasticsearch/index/cache/IndexCacheModule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.elasticsearch.common.inject.AbstractModule;
2323
import org.elasticsearch.common.settings.Settings;
24+
import org.elasticsearch.index.cache.docset.DocSetCacheModule;
2425
import org.elasticsearch.index.cache.filter.FilterCacheModule;
2526
import org.elasticsearch.index.cache.id.IdCacheModule;
2627
import org.elasticsearch.index.cache.query.parser.QueryParserCacheModule;
@@ -41,6 +42,7 @@ protected void configure() {
4142
new FilterCacheModule(settings).configure(binder());
4243
new IdCacheModule(settings).configure(binder());
4344
new QueryParserCacheModule(settings).configure(binder());
45+
new DocSetCacheModule(settings).configure(binder());
4446

4547
bind(IndexCache.class).asEagerSingleton();
4648
}
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,22 @@
1717
* under the License.
1818
*/
1919

20-
package org.elasticsearch.search.facet;
20+
package org.elasticsearch.index.cache.docset;
2121

22-
import org.elasticsearch.search.internal.SearchContext;
22+
import org.apache.lucene.index.AtomicReaderContext;
23+
import org.apache.lucene.index.IndexReader;
24+
import org.elasticsearch.common.lucene.docset.ContextDocIdSet;
25+
import org.elasticsearch.index.IndexComponent;
2326

24-
import java.io.IOException;
27+
/**
28+
*/
29+
public interface DocSetCache extends IndexComponent {
30+
31+
void clear(String reason);
32+
33+
void clear(IndexReader reader);
2534

26-
public interface OptimizeGlobalFacetCollector {
35+
ContextDocIdSet obtain(AtomicReaderContext context);
2736

28-
void optimizedGlobalExecution(SearchContext searchContext) throws IOException;
29-
}
37+
void release(ContextDocIdSet docSet);
38+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to ElasticSearch and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. ElasticSearch licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.cache.docset;
21+
22+
import org.elasticsearch.common.inject.AbstractModule;
23+
import org.elasticsearch.common.inject.Scopes;
24+
import org.elasticsearch.common.settings.Settings;
25+
import org.elasticsearch.index.cache.docset.simple.SimpleDocSetCache;
26+
27+
/**
28+
*
29+
*/
30+
public class DocSetCacheModule extends AbstractModule {
31+
32+
private final Settings settings;
33+
34+
public DocSetCacheModule(Settings settings) {
35+
this.settings = settings;
36+
}
37+
38+
@Override
39+
protected void configure() {
40+
bind(DocSetCache.class)
41+
.to(settings.getAsClass("index.cache.docset.type", SimpleDocSetCache.class, "org.elasticsearch.index.cache.docset.", "DocSetCache"))
42+
.in(Scopes.SINGLETON);
43+
}
44+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Licensed to ElasticSearch and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. ElasticSearch licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.cache.docset.none;
21+
22+
import org.apache.lucene.index.AtomicReaderContext;
23+
import org.apache.lucene.index.IndexReader;
24+
import org.apache.lucene.util.FixedBitSet;
25+
import org.elasticsearch.common.inject.Inject;
26+
import org.elasticsearch.common.lucene.docset.ContextDocIdSet;
27+
import org.elasticsearch.common.settings.Settings;
28+
import org.elasticsearch.index.AbstractIndexComponent;
29+
import org.elasticsearch.index.Index;
30+
import org.elasticsearch.index.cache.docset.DocSetCache;
31+
import org.elasticsearch.index.settings.IndexSettings;
32+
33+
/**
34+
*/
35+
public class NoneDocSetCache extends AbstractIndexComponent implements DocSetCache {
36+
37+
@Inject
38+
public NoneDocSetCache(Index index, @IndexSettings Settings indexSettings) {
39+
super(index, indexSettings);
40+
}
41+
42+
@Override
43+
public void clear(String reason) {
44+
}
45+
46+
@Override
47+
public void clear(IndexReader reader) {
48+
}
49+
50+
@Override
51+
public ContextDocIdSet obtain(AtomicReaderContext context) {
52+
return new ContextDocIdSet(context, new FixedBitSet(context.reader().maxDoc()));
53+
}
54+
55+
@Override
56+
public void release(ContextDocIdSet docSet) {
57+
}
58+
}

0 commit comments

Comments
 (0)