1 package org.apache.lucene.facet.search;
3 import java.io.IOException;
5 import java.util.Random;
7 import org.apache.lucene.index.IndexReader;
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;
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
26 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
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()}.
46 * @lucene.experimental
48 public final class AdaptiveFacetsAccumulator extends StandardFacetsAccumulator {
50 private Sampler sampler = new RandomSampler();
53 * Create an {@link AdaptiveFacetsAccumulator}
54 * @see StandardFacetsAccumulator#StandardFacetsAccumulator(FacetSearchParams, IndexReader, TaxonomyReader)
56 public AdaptiveFacetsAccumulator(FacetSearchParams searchParams, IndexReader indexReader,
57 TaxonomyReader taxonomyReader) {
58 super(searchParams, indexReader, taxonomyReader);
62 * Create an {@link AdaptiveFacetsAccumulator}
63 * @see StandardFacetsAccumulator#StandardFacetsAccumulator(FacetSearchParams, IndexReader, TaxonomyReader,
64 * IntArrayAllocator, FloatArrayAllocator)
66 public AdaptiveFacetsAccumulator(FacetSearchParams searchParams, IndexReader indexReader,
67 TaxonomyReader taxonomyReader, IntArrayAllocator intArrayAllocator,
68 FloatArrayAllocator floatArrayAllocator) {
69 super(searchParams, indexReader, taxonomyReader, intArrayAllocator, floatArrayAllocator);
74 * @param sampler sampler to set
76 public void setSampler(Sampler sampler) {
77 this.sampler = sampler;
81 public List<FacetResult> accumulate(ScoredDocIDs docids) throws IOException {
82 FacetsAccumulator delegee = appropriateFacetCountingAccumulator(docids);
84 if (delegee == this) {
85 return super.accumulate(docids);
88 return delegee.accumulate(docids);
92 * Compute the appropriate facet accumulator to use.
93 * If no special/clever adaptation is possible/needed return this (self).
95 private FacetsAccumulator appropriateFacetCountingAccumulator(ScoredDocIDs docids) {
96 // Verify that searchPareams permit sampling/complement/etc... otherwise do default
97 if (!mayComplement()) {
101 // Now we're sure we can use the sampling methods as we're in a counting only mode
103 // Verify that sampling is enabled and required ... otherwise do default
104 if (sampler == null || !sampler.shouldSample(docids)) {
108 SamplingAccumulator samplingAccumulator = new SamplingAccumulator(sampler, searchParams, indexReader, taxonomyReader);
109 samplingAccumulator.setComplementThreshold(getComplementThreshold());
110 return samplingAccumulator;
114 * @return the sampler in effect
116 public final Sampler getSampler() {