add --shared
[pylucene.git] / lucene-java-3.4.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
6 import org.apache.lucene.index.IndexReader;
7
8 import org.apache.lucene.facet.search.params.FacetSearchParams;
9 import org.apache.lucene.facet.search.results.FacetResult;
10 import org.apache.lucene.facet.search.results.FacetResultNode;
11 import org.apache.lucene.facet.search.sampling.Sampler;
12 import org.apache.lucene.facet.search.sampling.SamplingAccumulator;
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  * {@link FacetsAccumulator} whose behavior regarding complements, sampling,
34  * etc. is not set up front but rather is determined at accumulation time
35  * according to the statistics of the accumulated set of documents and the
36  * index.
37  * <p>
38  * Note: Sampling accumulation (Accumulation over a sampled-set of the results),
39  * does not guarantee accurate values for
40  * {@link FacetResult#getNumValidDescendants()} &
41  * {@link FacetResultNode#getResidue()}.
42  * 
43  * @lucene.experimental
44  */
45 public final class AdaptiveFacetsAccumulator extends StandardFacetsAccumulator {
46   
47   private Sampler sampler = new Sampler();
48
49   /**
50    * Create an {@link AdaptiveFacetsAccumulator} 
51    * @see StandardFacetsAccumulator#StandardFacetsAccumulator(FacetSearchParams, IndexReader, TaxonomyReader)
52    */
53   public AdaptiveFacetsAccumulator(FacetSearchParams searchParams, IndexReader indexReader,
54       TaxonomyReader taxonomyReader) {
55     super(searchParams, indexReader, taxonomyReader);
56   }
57
58   /**
59    * Create an {@link AdaptiveFacetsAccumulator} 
60    * @see StandardFacetsAccumulator#StandardFacetsAccumulator(FacetSearchParams, IndexReader, TaxonomyReader, 
61    *                               IntArrayAllocator, FloatArrayAllocator)
62    */
63   public AdaptiveFacetsAccumulator(FacetSearchParams searchParams, IndexReader indexReader,
64       TaxonomyReader taxonomyReader, IntArrayAllocator intArrayAllocator,
65       FloatArrayAllocator floatArrayAllocator) {
66     super(searchParams, indexReader, taxonomyReader, intArrayAllocator, floatArrayAllocator);
67   }
68
69   /**
70    * Set the sampler.
71    * @param sampler sampler to set
72    */
73   public void setSampler(Sampler sampler) {
74     this.sampler = sampler;
75   }
76   
77   @Override
78   public List<FacetResult> accumulate(ScoredDocIDs docids) throws IOException {
79     FacetsAccumulator delegee = appropriateFacetCountingAccumulator(docids);
80
81     if (delegee == this) {
82       return super.accumulate(docids);
83     }
84
85     return delegee.accumulate(docids);
86   }
87
88   /**
89    * Compute the appropriate facet accumulator to use.
90    * If no special/clever adaptation is possible/needed return this (self).
91    */
92   private FacetsAccumulator appropriateFacetCountingAccumulator(ScoredDocIDs docids) {
93     // Verify that searchPareams permit sampling/complement/etc... otherwise do default
94     if (!mayComplement()) {
95       return this;
96     }
97     
98     // Now we're sure we can use the sampling methods as we're in a counting only mode
99     
100     // Verify that sampling is enabled and required ... otherwise do default
101     if (sampler == null || !sampler.shouldSample(docids)) {
102       return this;
103     }
104     
105     SamplingAccumulator samplingAccumulator = new SamplingAccumulator(sampler, searchParams, indexReader, taxonomyReader);
106     samplingAccumulator.setComplementThreshold(getComplementThreshold());
107     return samplingAccumulator;
108   }
109
110   /**
111    * @return the sampler in effect
112    */
113   public final Sampler getSampler() {
114     return sampler;
115   }
116 }