From 6cdde31e649689e0cb23e635f24ed31dbe90b16b Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Tue, 3 Feb 2015 17:33:29 +0100 Subject: [PATCH] Search: Reuse Lucene's MultiCollector. We could reuse Lucene's MultiCollector instead of implementing our own. Close #9549 --- .../common/lucene/MultiCollector.java | 85 ------------------- .../search/internal/ContextIndexSearcher.java | 6 +- 2 files changed, 4 insertions(+), 87 deletions(-) delete mode 100644 src/main/java/org/elasticsearch/common/lucene/MultiCollector.java diff --git a/src/main/java/org/elasticsearch/common/lucene/MultiCollector.java b/src/main/java/org/elasticsearch/common/lucene/MultiCollector.java deleted file mode 100644 index bb33b78c122..00000000000 --- a/src/main/java/org/elasticsearch/common/lucene/MultiCollector.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.lucene; - -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.search.*; -import org.elasticsearch.common.lucene.search.XCollector; - -import java.io.IOException; - -/** - * - */ -public class MultiCollector extends SimpleCollector implements XCollector { - - private final Collector collector; - private final Collector[] collectors; - - private LeafCollector leafCollector; - private final LeafCollector[] leafCollectors; - - - public MultiCollector(Collector collector, Collector[] collectors) { - this.collector = collector; - this.collectors = collectors; - this.leafCollectors = new LeafCollector[collectors.length]; - } - - @Override - public void setScorer(Scorer scorer) throws IOException { - // always wrap it in a scorer wrapper - if (!(scorer instanceof ScoreCachingWrappingScorer)) { - scorer = new ScoreCachingWrappingScorer(scorer); - } - leafCollector.setScorer(scorer); - for (LeafCollector leafCollector : leafCollectors) { - leafCollector.setScorer(scorer); - } - } - - @Override - public void collect(int doc) throws IOException { - leafCollector.collect(doc); - for (LeafCollector leafCollector : leafCollectors) { - leafCollector.collect(doc); - } - } - - @Override - public void doSetNextReader(LeafReaderContext context) throws IOException { - leafCollector = collector.getLeafCollector(context); - for (int i = 0; i < collectors.length; i++) { - leafCollectors[i] = collectors[i].getLeafCollector(context); - } - } - - @Override - public void postCollection() throws IOException { - if (collector instanceof XCollector) { - ((XCollector) collector).postCollection(); - } - for (Collector collector : collectors) { - if (collector instanceof XCollector) { - ((XCollector) collector).postCollection(); - } - } - } -} diff --git a/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java b/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java index e5f7bc4deda..aa770b3bd85 100644 --- a/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java +++ b/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java @@ -24,13 +24,13 @@ import org.apache.lucene.search.Collector; import org.apache.lucene.search.Explanation; import org.apache.lucene.search.FilteredQuery; import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.MultiCollector; import org.apache.lucene.search.Query; import org.apache.lucene.search.TimeLimitingCollector; import org.apache.lucene.search.Weight; import org.elasticsearch.common.lease.Releasable; import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.lucene.MinimumScoreCollector; -import org.elasticsearch.common.lucene.MultiCollector; import org.elasticsearch.common.lucene.search.FilteredCollector; import org.elasticsearch.common.lucene.search.XCollector; import org.elasticsearch.index.engine.Engine; @@ -151,7 +151,9 @@ public class ContextIndexSearcher extends IndexSearcher implements Releasable { collector = new FilteredCollector(collector, searchContext.parsedPostFilter().filter()); } if (queryCollectors != null && !queryCollectors.isEmpty()) { - collector = new MultiCollector(collector, queryCollectors.toArray(new Collector[queryCollectors.size()])); + ArrayList allCollectors = new ArrayList<>(queryCollectors); + allCollectors.add(collector); + collector = MultiCollector.wrap(allCollectors); } // apply the minimum score after multi collector so we filter aggs as well