1 package org.apache.lucene.facet.search;
3 import java.io.IOException;
6 import org.apache.lucene.index.IndexReader;
7 import org.apache.lucene.search.Collector;
8 import org.apache.lucene.search.Scorer;
10 import org.apache.lucene.facet.search.params.FacetRequest;
11 import org.apache.lucene.facet.search.params.FacetSearchParams;
12 import org.apache.lucene.facet.search.results.FacetResult;
13 import org.apache.lucene.facet.taxonomy.TaxonomyReader;
16 * Licensed to the Apache Software Foundation (ASF) under one or more
17 * contributor license agreements. See the NOTICE file distributed with
18 * this work for additional information regarding copyright ownership.
19 * The ASF licenses this file to You under the Apache License, Version 2.0
20 * (the "License"); you may not use this file except in compliance with
21 * the License. You may obtain a copy of the License at
23 * http://www.apache.org/licenses/LICENSE-2.0
25 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS,
27 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
33 * Collector for facet accumulation. *
35 * @lucene.experimental
37 public class FacetsCollector extends Collector {
39 protected final FacetsAccumulator facetsAccumulator;
40 private ScoredDocIdCollector scoreDocIdCollector;
41 private List<FacetResult> results;
42 private Object resultsGuard;
45 * Create a collector for accumulating facets while collecting documents
48 * @param facetSearchParams
49 * faceted search parameters defining which facets are required and
53 * @param taxonomyReader
54 * taxonomy containing the facets.
56 public FacetsCollector(FacetSearchParams facetSearchParams,
57 IndexReader indexReader, TaxonomyReader taxonomyReader) {
58 facetsAccumulator = initFacetsAccumulator(facetSearchParams, indexReader, taxonomyReader);
59 scoreDocIdCollector = initScoredDocCollector(facetSearchParams, indexReader, taxonomyReader);
60 resultsGuard = new Object();
64 * Create a {@link ScoredDocIdCollector} to be used as the first phase of
65 * the facet collection. If all facetRequests are do not require the
66 * document score, a ScoredDocIdCollector which does not store the document
67 * scores would be returned. Otherwise a SDIC which does store the documents
68 * will be returned, having an initial allocated space for 1000 such
71 protected ScoredDocIdCollector initScoredDocCollector(
72 FacetSearchParams facetSearchParams, IndexReader indexReader,
73 TaxonomyReader taxonomyReader) {
74 for (FacetRequest frq : facetSearchParams.getFacetRequests()) {
75 if (frq.requireDocumentScore()) {
76 return ScoredDocIdCollector.create(1000, true);
79 return ScoredDocIdCollector.create(indexReader.maxDoc(), false);
83 * Create the {@link FacetsAccumulator} to be used. Default is
84 * {@link StandardFacetsAccumulator}. Called once at the constructor of the collector.
86 * @param facetSearchParams
89 * A reader to the index to search in.
90 * @param taxonomyReader
91 * A reader to the active taxonomy.
92 * @return The {@link FacetsAccumulator} to use.
94 protected FacetsAccumulator initFacetsAccumulator(FacetSearchParams facetSearchParams,
95 IndexReader indexReader,
96 TaxonomyReader taxonomyReader) {
97 return new StandardFacetsAccumulator(facetSearchParams, indexReader, taxonomyReader);
101 * Return accumulated facets results (according to faceted search parameters)
102 * for collected documents.
103 * @throws IOException on error
105 public List<FacetResult> getFacetResults() throws IOException {
106 synchronized (resultsGuard) { // over protection
107 if (results == null) {
108 // lazy creation but just once
109 results = facetsAccumulator.accumulate(scoreDocIdCollector.getScoredDocIDs());
110 scoreDocIdCollector = null;
117 public boolean acceptsDocsOutOfOrder() {
122 public void collect(int doc) throws IOException {
123 scoreDocIdCollector.collect(doc);
127 public void setNextReader(IndexReader reader, int docBase) throws IOException {
128 scoreDocIdCollector.setNextReader(reader, docBase);
132 public void setScorer(Scorer scorer) throws IOException {
133 scoreDocIdCollector.setScorer(scorer);