pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / search / FacetsCollector.java
1 package org.apache.lucene.facet.search;
2
3 import java.io.IOException;
4 import java.util.List;
5
6 import org.apache.lucene.index.IndexReader;
7 import org.apache.lucene.search.Collector;
8 import org.apache.lucene.search.Scorer;
9
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;
14
15 /**
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
22  *
23  *     http://www.apache.org/licenses/LICENSE-2.0
24  *
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.
30  */
31
32 /**
33  * Collector for facet accumulation. *
34  * 
35  * @lucene.experimental
36  */
37 public class FacetsCollector extends Collector {
38
39   protected final FacetsAccumulator facetsAccumulator;
40   private ScoredDocIdCollector scoreDocIdCollector;
41   private List<FacetResult> results;
42   private Object resultsGuard;
43
44   /**
45    * Create a collector for accumulating facets while collecting documents
46    * during search.
47    * 
48    * @param facetSearchParams
49    *          faceted search parameters defining which facets are required and
50    *          how.
51    * @param indexReader
52    *          searched index.
53    * @param taxonomyReader
54    *          taxonomy containing the facets.
55    */
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();
61   }
62
63   /**
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
69    * documents' scores.
70    */
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);
77       }
78     }
79     return ScoredDocIdCollector.create(indexReader.maxDoc(), false);
80   }
81
82   /**
83    * Create the {@link FacetsAccumulator} to be used. Default is 
84    * {@link StandardFacetsAccumulator}. Called once at the constructor of the collector.
85    * 
86    * @param facetSearchParams
87    *            The search params.
88    * @param indexReader
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.
93    */
94   protected FacetsAccumulator initFacetsAccumulator(FacetSearchParams facetSearchParams,
95                                                     IndexReader indexReader,
96                                                     TaxonomyReader taxonomyReader) {
97     return new StandardFacetsAccumulator(facetSearchParams, indexReader, taxonomyReader);
98   }
99
100   /**
101    * Return accumulated facets results (according to faceted search parameters) 
102    * for collected documents.
103    * @throws IOException on error
104    */
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;
111       }
112       return results;
113     }
114   }
115
116   @Override
117   public boolean acceptsDocsOutOfOrder() {
118     return false;
119   }
120
121   @Override
122   public void collect(int doc) throws IOException {
123     scoreDocIdCollector.collect(doc);
124   }
125
126   @Override
127   public void setNextReader(IndexReader reader, int docBase) throws IOException {
128     scoreDocIdCollector.setNextReader(reader, docBase);
129   }
130
131   @Override
132   public void setScorer(Scorer scorer) throws IOException {
133     scoreDocIdCollector.setScorer(scorer);
134   }
135
136 }