pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / search / AdaptiveFacetsAccumulator.java
1 package org.apache.lucene.facet.search;
2
3 import java.io.IOException;
4 import java.util.List;
5 import java.util.Random;
6
7 import org.apache.lucene.index.IndexReader;
8
9 import org.apache.lucene.facet.search.params.FacetSearchParams;
10 import org.apache.lucene.facet.search.results.FacetResult;
11 import org.apache.lucene.facet.search.results.FacetResultNode;
12 import org.apache.lucene.facet.search.sampling.RandomSampler;
13 import org.apache.lucene.facet.search.sampling.RepeatableSampler;
14 import org.apache.lucene.facet.search.sampling.Sampler;
15 import org.apache.lucene.facet.search.sampling.SamplingAccumulator;
16 import org.apache.lucene.facet.taxonomy.TaxonomyReader;
17
18 /**
19  * Licensed to the Apache Software Foundation (ASF) under one or more
20  * contributor license agreements.  See the NOTICE file distributed with
21  * this work for additional information regarding copyright ownership.
22  * The ASF licenses this file to You under the Apache License, Version 2.0
23  * (the "License"); you may not use this file except in compliance with
24  * the License.  You may obtain a copy of the License at
25  *
26  *     http://www.apache.org/licenses/LICENSE-2.0
27  *
28  * Unless required by applicable law or agreed to in writing, software
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  */
34
35 /**
36  * {@link FacetsAccumulator} whose behavior regarding complements, sampling,
37  * etc. is not set up front but rather is determined at accumulation time
38  * according to the statistics of the accumulated set of documents and the
39  * index.
40  * <p>
41  * Note: Sampling accumulation (Accumulation over a sampled-set of the results),
42  * does not guarantee accurate values for
43  * {@link FacetResult#getNumValidDescendants()} &
44  * {@link FacetResultNode#getResidue()}.
45  * 
46  * @lucene.experimental
47  */
48 public final class AdaptiveFacetsAccumulator extends StandardFacetsAccumulator {
49   
50   private Sampler sampler = new RandomSampler();
51
52   /**
53    * Create an {@link AdaptiveFacetsAccumulator} 
54    * @see StandardFacetsAccumulator#StandardFacetsAccumulator(FacetSearchParams, IndexReader, TaxonomyReader)
55    */
56   public AdaptiveFacetsAccumulator(FacetSearchParams searchParams, IndexReader indexReader,
57       TaxonomyReader taxonomyReader) {
58     super(searchParams, indexReader, taxonomyReader);
59   }
60
61   /**
62    * Create an {@link AdaptiveFacetsAccumulator} 
63    * @see StandardFacetsAccumulator#StandardFacetsAccumulator(FacetSearchParams, IndexReader, TaxonomyReader, 
64    *                               IntArrayAllocator, FloatArrayAllocator)
65    */
66   public AdaptiveFacetsAccumulator(FacetSearchParams searchParams, IndexReader indexReader,
67       TaxonomyReader taxonomyReader, IntArrayAllocator intArrayAllocator,
68       FloatArrayAllocator floatArrayAllocator) {
69     super(searchParams, indexReader, taxonomyReader, intArrayAllocator, floatArrayAllocator);
70   }
71
72   /**
73    * Set the sampler.
74    * @param sampler sampler to set
75    */
76   public void setSampler(Sampler sampler) {
77     this.sampler = sampler;
78   }
79   
80   @Override
81   public List<FacetResult> accumulate(ScoredDocIDs docids) throws IOException {
82     FacetsAccumulator delegee = appropriateFacetCountingAccumulator(docids);
83
84     if (delegee == this) {
85       return super.accumulate(docids);
86     }
87
88     return delegee.accumulate(docids);
89   }
90
91   /**
92    * Compute the appropriate facet accumulator to use.
93    * If no special/clever adaptation is possible/needed return this (self).
94    */
95   private FacetsAccumulator appropriateFacetCountingAccumulator(ScoredDocIDs docids) {
96     // Verify that searchPareams permit sampling/complement/etc... otherwise do default
97     if (!mayComplement()) {
98       return this;
99     }
100     
101     // Now we're sure we can use the sampling methods as we're in a counting only mode
102     
103     // Verify that sampling is enabled and required ... otherwise do default
104     if (sampler == null || !sampler.shouldSample(docids)) {
105       return this;
106     }
107     
108     SamplingAccumulator samplingAccumulator = new SamplingAccumulator(sampler, searchParams, indexReader, taxonomyReader);
109     samplingAccumulator.setComplementThreshold(getComplementThreshold());
110     return samplingAccumulator;
111   }
112
113   /**
114    * @return the sampler in effect
115    */
116   public final Sampler getSampler() {
117     return sampler;
118   }
119 }