Search: Reuse Lucene's MultiCollector.

We could reuse Lucene's MultiCollector instead of implementing our own.

Close #9549
This commit is contained in:
Adrien Grand 2015-02-03 17:33:29 +01:00
parent 13b64cc362
commit 6cdde31e64
2 changed files with 4 additions and 87 deletions

View File

@ -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();
}
}
}
}

View File

@ -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<Collector> allCollectors = new ArrayList<>(queryCollectors);
allCollectors.add(collector);
collector = MultiCollector.wrap(allCollectors);
}
// apply the minimum score after multi collector so we filter aggs as well