1 package org.apache.lucene.facet.search;
3 import java.io.IOException;
5 import org.apache.lucene.facet.search.params.FacetRequest;
6 import org.apache.lucene.facet.search.results.FacetResult;
7 import org.apache.lucene.facet.search.results.FacetResultNode;
8 import org.apache.lucene.facet.search.results.IntermediateFacetResult;
9 import org.apache.lucene.facet.taxonomy.TaxonomyReader;
12 * Licensed to the Apache Software Foundation (ASF) under one or more
13 * contributor license agreements. See the NOTICE file distributed with
14 * this work for additional information regarding copyright ownership.
15 * The ASF licenses this file to You under the Apache License, Version 2.0
16 * (the "License"); you may not use this file except in compliance with
17 * the License. You may obtain a copy of the License at
19 * http://www.apache.org/licenses/LICENSE-2.0
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
29 * Handler for facet results.
31 * The facet results handler provided by the {@link FacetRequest} to
32 * a {@link FacetsAccumulator}.
34 * First it is used by {@link FacetsAccumulator} to obtain a temporary
35 * facet result for each partition and to merge results of several partitions.
37 * Later the accumulator invokes the handler to render the results, creating
38 * {@link FacetResult} objects.
40 * Last the accumulator invokes the handler to label final results.
42 * @lucene.experimental
44 public abstract class FacetResultsHandler {
46 /** Taxonomy for which facets are handled */
47 protected final TaxonomyReader taxonomyReader;
50 * Facet request served by this handler.
52 protected final FacetRequest facetRequest;
55 * Create a faceted search handler.
56 * @param taxonomyReader See {@link #getTaxonomyReader()}.
57 * @param facetRequest See {@link #getFacetRequest()}.
59 public FacetResultsHandler(TaxonomyReader taxonomyReader,
60 FacetRequest facetRequest) {
61 this.taxonomyReader = taxonomyReader;
62 this.facetRequest = facetRequest;
66 * Fetch results of a single partition, given facet arrays for that partition,
67 * and based on the matching documents and faceted search parameters.
70 * facet arrays for the certain partition
72 * offset in input arrays where partition starts
73 * @return temporary facet result, potentially, to be passed back to
74 * <b>this</b> result handler for merging, or <b>null</b> in case that
75 * constructor parameter, <code>facetRequest</code>, requests an
76 * illegal FacetResult, like, e.g., a root node category path that
77 * does not exist in constructor parameter <code>taxonomyReader</code>
82 public abstract IntermediateFacetResult fetchPartitionResult(FacetArrays arrays, int offset) throws IOException;
85 * Merge results of several facet partitions. Logic of the merge is undefined
86 * and open for interpretations. For example, a merge implementation could
87 * keep top K results. Passed {@link IntermediateFacetResult} must be ones
88 * that were created by this handler otherwise a {@link ClassCastException} is
89 * thrown. In addition, all passed {@link IntermediateFacetResult} must have
90 * the same {@link FacetRequest} otherwise an {@link IllegalArgumentException}
93 * @param tmpResults one or more temporary results created by <b>this</b>
95 * @return temporary facet result that represents to union, as specified by
96 * <b>this</b> handler, of the input temporary facet results.
97 * @throws IOException on error.
98 * @throws ClassCastException if the temporary result passed was not created
100 * @throws IllegalArgumentException if passed <code>facetResults</code> do not
101 * have the same {@link FacetRequest}
102 * @see IntermediateFacetResult#getFacetRequest()
104 public abstract IntermediateFacetResult mergeResults(IntermediateFacetResult... tmpResults)
105 throws IOException, ClassCastException, IllegalArgumentException;
108 * Create a facet result from the temporary result.
109 * @param tmpResult temporary result to be rendered as a {@link FacetResult}
110 * @throws IOException on error.
112 public abstract FacetResult renderFacetResult(IntermediateFacetResult tmpResult) throws IOException ;
115 * Perform any rearrangement as required on a facet result that has changed after
118 * Possible use case: a sampling facets accumulator invoked another
119 * other facets accumulator on a sample set of documents, obtained
120 * rendered facet results, fixed their counts, and now it is needed
121 * to sort the results differently according to the fixed counts.
122 * @param facetResult result to be rearranged.
123 * @see FacetResultNode#setValue(double)
125 public abstract FacetResult rearrangeFacetResult(FacetResult facetResult);
128 * Label results according to settings in {@link FacetRequest},
129 * such as {@link FacetRequest#getNumLabel()}.
130 * Usually invoked by {@link FacetsAccumulator#accumulate(ScoredDocIDs)}
131 * @param facetResult facet result to be labeled.
132 * @throws IOException on error
134 public abstract void labelResult (FacetResult facetResult) throws IOException;
136 /** Return taxonomy reader used for current facets accumulation operation. */
137 public final TaxonomyReader getTaxonomyReader() {
138 return this.taxonomyReader;
141 /** Return the facet request served by this handler. */
142 public final FacetRequest getFacetRequest() {
143 return this.facetRequest;
147 * Check if an array contains the partition which contains ordinal
152 * facet arrays for the certain partition
154 * offset in input arrays where partition starts
156 protected boolean isSelfPartition (int ordinal, FacetArrays facetArrays, int offset) {
157 int partitionSize = facetArrays.getArraysLength();
158 return ordinal / partitionSize == offset / partitionSize;