1 package org.apache.lucene.facet.search;
3 import java.io.IOException;
4 import java.util.ArrayList;
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;
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
21 * http://www.apache.org/licenses/LICENSE-2.0
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.
31 * Wrap any Facets Accumulator with sampling.
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()}.
38 * @lucene.experimental
40 public class SamplingWrapper extends FacetsAccumulator {
42 private FacetsAccumulator delegee;
43 private Sampler sampler;
45 public SamplingWrapper(FacetsAccumulator delegee, Sampler sampler) {
46 super(delegee.searchParams, delegee.indexReader, delegee.taxonomyReader);
47 this.delegee = delegee;
48 this.sampler = sampler;
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);
58 // Replacing the original searchParams with the over-sampled (and without statistics-compute)
59 FacetSearchParams original = delegee.searchParams;
60 delegee.searchParams = sampler.overSampledSearchParams(original);
62 SampleResult sampleSet = sampler.getSampleSet(docids);
64 List<FacetResult> sampleRes = delegee.accumulate(sampleSet.docids);
65 setAllowLabeling(origAllowLabeling);
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
76 // Using the sampler to trim the extra (over-sampled) results
77 fres = sampler.trimResult(fres);
79 // final labeling if allowed (because labeling is a costly operation)
80 if (isAllowLabeling()) {
81 frh.labelResult(fres);
83 fixedRes.add(fres); // add to final results
86 delegee.searchParams = original; // Back to original params
92 * @see FacetsAccumulator#getComplementThreshold()
95 public double getComplementThreshold() {
96 return delegee.getComplementThreshold();
100 * @param complementThreshold
101 * @see FacetsAccumulator#setComplementThreshold(double)
104 public void setComplementThreshold(double complementThreshold) {
105 delegee.setComplementThreshold(complementThreshold);
109 protected boolean isAllowLabeling() {
110 return delegee.isAllowLabeling();
114 protected void setAllowLabeling(boolean allowLabeling) {
115 delegee.setAllowLabeling(allowLabeling);