1 package org.apache.lucene.facet.search;
3 import java.io.IOException;
6 import org.apache.lucene.index.IndexReader;
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;
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
23 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
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()}.
43 * @lucene.experimental
45 public final class AdaptiveFacetsAccumulator extends StandardFacetsAccumulator {
47 private Sampler sampler = new Sampler();
50 * Create an {@link AdaptiveFacetsAccumulator}
51 * @see StandardFacetsAccumulator#StandardFacetsAccumulator(FacetSearchParams, IndexReader, TaxonomyReader)
53 public AdaptiveFacetsAccumulator(FacetSearchParams searchParams, IndexReader indexReader,
54 TaxonomyReader taxonomyReader) {
55 super(searchParams, indexReader, taxonomyReader);
59 * Create an {@link AdaptiveFacetsAccumulator}
60 * @see StandardFacetsAccumulator#StandardFacetsAccumulator(FacetSearchParams, IndexReader, TaxonomyReader,
61 * IntArrayAllocator, FloatArrayAllocator)
63 public AdaptiveFacetsAccumulator(FacetSearchParams searchParams, IndexReader indexReader,
64 TaxonomyReader taxonomyReader, IntArrayAllocator intArrayAllocator,
65 FloatArrayAllocator floatArrayAllocator) {
66 super(searchParams, indexReader, taxonomyReader, intArrayAllocator, floatArrayAllocator);
71 * @param sampler sampler to set
73 public void setSampler(Sampler sampler) {
74 this.sampler = sampler;
78 public List<FacetResult> accumulate(ScoredDocIDs docids) throws IOException {
79 FacetsAccumulator delegee = appropriateFacetCountingAccumulator(docids);
81 if (delegee == this) {
82 return super.accumulate(docids);
85 return delegee.accumulate(docids);
89 * Compute the appropriate facet accumulator to use.
90 * If no special/clever adaptation is possible/needed return this (self).
92 private FacetsAccumulator appropriateFacetCountingAccumulator(ScoredDocIDs docids) {
93 // Verify that searchPareams permit sampling/complement/etc... otherwise do default
94 if (!mayComplement()) {
98 // Now we're sure we can use the sampling methods as we're in a counting only mode
100 // Verify that sampling is enabled and required ... otherwise do default
101 if (sampler == null || !sampler.shouldSample(docids)) {
105 SamplingAccumulator samplingAccumulator = new SamplingAccumulator(sampler, searchParams, indexReader, taxonomyReader);
106 samplingAccumulator.setComplementThreshold(getComplementThreshold());
107 return samplingAccumulator;
111 * @return the sampler in effect
113 public final Sampler getSampler() {