pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / search / SamplingWrapper.java
1 package org.apache.lucene.facet.search;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.apache.lucene.facet.search.params.FacetSearchParams;
8 import org.apache.lucene.facet.search.results.FacetResult;
9 import org.apache.lucene.facet.search.results.FacetResultNode;
10 import org.apache.lucene.facet.search.sampling.Sampler;
11 import org.apache.lucene.facet.search.sampling.Sampler.SampleResult;
12
13 /**
14  * Licensed to the Apache Software Foundation (ASF) under one or more
15  * contributor license agreements.  See the NOTICE file distributed with
16  * this work for additional information regarding copyright ownership.
17  * The ASF licenses this file to You under the Apache License, Version 2.0
18  * (the "License"); you may not use this file except in compliance with
19  * the License.  You may obtain a copy of the License at
20  *
21  *     http://www.apache.org/licenses/LICENSE-2.0
22  *
23  * Unless required by applicable law or agreed to in writing, software
24  * distributed under the License is distributed on an "AS IS" BASIS,
25  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26  * See the License for the specific language governing permissions and
27  * limitations under the License.
28  */
29
30 /**
31  * Wrap any Facets Accumulator with sampling.
32  * <p>
33  * Note: Sampling accumulation (Accumulation over a sampled-set of the results),
34  * does not guarantee accurate values for
35  * {@link FacetResult#getNumValidDescendants()} &
36  * {@link FacetResultNode#getResidue()}.
37  * 
38  * @lucene.experimental
39  */
40 public class SamplingWrapper extends FacetsAccumulator {
41
42   private FacetsAccumulator delegee;
43   private Sampler sampler;
44
45   public SamplingWrapper(FacetsAccumulator delegee, Sampler sampler) {
46     super(delegee.searchParams, delegee.indexReader, delegee.taxonomyReader);
47     this.delegee = delegee;
48     this.sampler = sampler;
49   }
50
51   @Override
52   public List<FacetResult> accumulate(ScoredDocIDs docids) throws IOException {
53     // first let delegee accumulate without labeling at all (though
54     // currently it doesn't matter because we have to label all returned anyhow)
55     boolean origAllowLabeling = isAllowLabeling();
56     setAllowLabeling(false);
57
58     // Replacing the original searchParams with the over-sampled (and without statistics-compute)
59     FacetSearchParams original = delegee.searchParams;
60     delegee.searchParams = sampler.overSampledSearchParams(original);
61     
62     SampleResult sampleSet = sampler.getSampleSet(docids);
63
64     List<FacetResult> sampleRes = delegee.accumulate(sampleSet.docids);
65     setAllowLabeling(origAllowLabeling);
66
67     List<FacetResult> fixedRes = new ArrayList<FacetResult>();
68     for (FacetResult fres : sampleRes) {
69       // for sure fres is not null because this is guaranteed by the delegee.
70       FacetResultsHandler frh = fres.getFacetRequest().createFacetResultsHandler(taxonomyReader);
71       // fix the result of current request
72       sampler.getSampleFixer(indexReader, taxonomyReader, searchParams)
73           .fixResult(docids, fres); 
74       fres = frh.rearrangeFacetResult(fres); // let delegee's handler do any
75       
76       // Using the sampler to trim the extra (over-sampled) results
77       fres = sampler.trimResult(fres);
78       
79       // final labeling if allowed (because labeling is a costly operation)
80       if (isAllowLabeling()) {
81         frh.labelResult(fres);
82       }
83       fixedRes.add(fres); // add to final results
84     }
85
86     delegee.searchParams = original; // Back to original params
87     
88     return fixedRes; 
89   }
90
91   /**
92    * @see FacetsAccumulator#getComplementThreshold()
93    */
94   @Override
95   public double getComplementThreshold() {
96     return delegee.getComplementThreshold();
97   }
98
99   /**
100    * @param complementThreshold
101    * @see FacetsAccumulator#setComplementThreshold(double)
102    */
103   @Override
104   public void setComplementThreshold(double complementThreshold) {
105     delegee.setComplementThreshold(complementThreshold);
106   }
107
108   @Override
109   protected boolean isAllowLabeling() {
110     return delegee.isAllowLabeling();
111   }
112
113   @Override
114   protected void setAllowLabeling(boolean allowLabeling) {
115     delegee.setAllowLabeling(allowLabeling);
116   }
117
118 }