1 package org.apache.lucene.facet.search;
3 import java.util.Arrays;
6 import org.apache.lucene.index.IndexReader;
7 import org.apache.lucene.search.MatchAllDocsQuery;
10 import org.apache.lucene.facet.search.params.CountFacetRequest;
11 import org.apache.lucene.facet.search.params.FacetSearchParams;
12 import org.apache.lucene.facet.search.results.FacetResult;
13 import org.apache.lucene.facet.search.results.FacetResultNode;
14 import org.apache.lucene.facet.taxonomy.CategoryPath;
15 import org.apache.lucene.facet.taxonomy.TaxonomyReader;
18 * Licensed to the Apache Software Foundation (ASF) under one or more
19 * contributor license agreements. See the NOTICE file distributed with
20 * this work for additional information regarding copyright ownership.
21 * The ASF licenses this file to You under the Apache License, Version 2.0
22 * (the "License"); you may not use this file except in compliance with
23 * the License. You may obtain a copy of the License at
25 * http://www.apache.org/licenses/LICENSE-2.0
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS,
29 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 * See the License for the specific language governing permissions and
31 * limitations under the License.
34 public class TestTopKResultsHandler extends BaseTestTopK {
36 private static final CategoryPath[] CATEGORIES = {
37 new CategoryPath( "a", "b"),
38 new CategoryPath( "a", "b", "1"),
39 new CategoryPath( "a", "b", "1"),
40 new CategoryPath( "a", "b", "2"),
41 new CategoryPath( "a", "b", "2"),
42 new CategoryPath( "a", "b", "3"),
43 new CategoryPath( "a", "b", "4"),
44 new CategoryPath( "a", "c"),
45 new CategoryPath( "a", "c"),
46 new CategoryPath( "a", "c"),
47 new CategoryPath( "a", "c"),
48 new CategoryPath( "a", "c"),
49 new CategoryPath( "a", "c", "1"),
53 protected String getContent(int doc) {
58 protected int numDocsToIndex() {
59 return CATEGORIES.length;
63 protected List<CategoryPath> getCategories(int doc) {
64 return Arrays.asList(CATEGORIES[doc]);
68 * Strait forward test: Adding specific documents with specific facets and
69 * counting them in the most basic form.
72 public void testSimple() throws Exception {
73 for (int partitionSize : partitionSizes) {
74 initIndex(partitionSize);
76 // do different facet counts and compare to control
77 FacetSearchParams sParams = getFacetedSearchParams(partitionSize);
79 sParams.addFacetRequest(new CountFacetRequest(new CategoryPath("a"), 100));
80 CountFacetRequest cfra = new CountFacetRequest(new CategoryPath("a"), 100);
82 sParams.addFacetRequest(cfra);
83 sParams.addFacetRequest(new CountFacetRequest(new CategoryPath("a", "b"), 100));
84 sParams.addFacetRequest(new CountFacetRequest(new CategoryPath("a", "b", "1"), 100));
85 sParams.addFacetRequest(new CountFacetRequest(new CategoryPath("a", "c"), 100));
87 FacetsCollector fc = new FacetsCollector(sParams, indexReader, taxoReader) {
89 protected FacetsAccumulator initFacetsAccumulator(FacetSearchParams facetSearchParams, IndexReader indexReader, TaxonomyReader taxonomyReader) {
90 FacetsAccumulator fa = new StandardFacetsAccumulator(facetSearchParams, indexReader, taxonomyReader);
91 fa.setComplementThreshold(FacetsAccumulator.DISABLE_COMPLEMENT);
96 searcher.search(new MatchAllDocsQuery(), fc);
97 long start = System.currentTimeMillis();
98 List<FacetResult> facetResults = fc.getFacetResults();
99 long end = System.currentTimeMillis();
102 System.out.println("Time: " + (end - start));
105 FacetResult fr = facetResults.get(0);
106 FacetResultNode parentRes = fr.getFacetResultNode();
107 assertEquals(13.0, parentRes.getValue(), Double.MIN_VALUE);
108 FacetResultNode[] frn = resultNodesAsArray(parentRes);
109 assertEquals(7.0, frn[0].getValue(), Double.MIN_VALUE);
110 assertEquals(6.0, frn[1].getValue(), Double.MIN_VALUE);
112 fr = facetResults.get(1);
113 parentRes = fr.getFacetResultNode();
114 assertEquals(13.0, parentRes.getValue(), Double.MIN_VALUE);
115 frn = resultNodesAsArray(parentRes);
116 assertEquals(7.0, frn[0].getValue(), Double.MIN_VALUE);
117 assertEquals(6.0, frn[1].getValue(), Double.MIN_VALUE);
118 assertEquals(2.0, frn[2].getValue(), Double.MIN_VALUE);
119 assertEquals(2.0, frn[3].getValue(), Double.MIN_VALUE);
120 assertEquals(1.0, frn[4].getValue(), Double.MIN_VALUE);
121 assertEquals(1.0, frn[5].getValue(), Double.MIN_VALUE);
123 fr = facetResults.get(2);
124 parentRes = fr.getFacetResultNode();
125 assertEquals(7.0, parentRes.getValue(), Double.MIN_VALUE);
126 frn = resultNodesAsArray(parentRes);
127 assertEquals(2.0, frn[0].getValue(), Double.MIN_VALUE);
128 assertEquals(2.0, frn[1].getValue(), Double.MIN_VALUE);
129 assertEquals(1.0, frn[2].getValue(), Double.MIN_VALUE);
130 assertEquals(1.0, frn[3].getValue(), Double.MIN_VALUE);
132 fr = facetResults.get(3);
133 parentRes = fr.getFacetResultNode();
134 assertEquals(2.0, parentRes.getValue(), Double.MIN_VALUE);
135 frn = resultNodesAsArray(parentRes);
136 assertEquals(0, frn.length);
138 fr = facetResults.get(4);
139 parentRes = fr.getFacetResultNode();
140 assertEquals(6.0, parentRes.getValue(), Double.MIN_VALUE);
141 frn = resultNodesAsArray(parentRes);
142 assertEquals(1.0, frn[0].getValue(), Double.MIN_VALUE);
148 * Creating an index, matching the results of an top K = Integer.MAX_VALUE and top-1000 requests
151 public void testGetMaxIntFacets() throws Exception {
152 for (int partitionSize : partitionSizes) {
153 initIndex(partitionSize);
155 // do different facet counts and compare to control
156 CategoryPath path = new CategoryPath("a", "b");
157 FacetSearchParams sParams = getFacetedSearchParams(partitionSize);
158 sParams.addFacetRequest(new CountFacetRequest(path, Integer.MAX_VALUE));
160 FacetsCollector fc = new FacetsCollector(sParams, indexReader, taxoReader) {
162 protected FacetsAccumulator initFacetsAccumulator(FacetSearchParams facetSearchParams, IndexReader indexReader, TaxonomyReader taxonomyReader) {
163 FacetsAccumulator fa = new StandardFacetsAccumulator(facetSearchParams, indexReader, taxonomyReader);
164 fa.setComplementThreshold(FacetsAccumulator.DISABLE_COMPLEMENT);
169 searcher.search(new MatchAllDocsQuery(), fc);
170 long start = System.currentTimeMillis();
171 List<FacetResult> results = fc.getFacetResults();
172 long end = System.currentTimeMillis();
175 System.out.println("Time: " + (end - start));
178 assertEquals("Should only be one result as there's only one request", 1, results.size());
179 FacetResult res = results.get(0);
180 assertEquals(path + " should only have 4 desendants", 4, res.getNumValidDescendants());
182 // As a control base results, ask for top-1000 results
183 FacetSearchParams sParams2 = getFacetedSearchParams(partitionSize);
184 sParams2.addFacetRequest(new CountFacetRequest(path, Integer.MAX_VALUE));
186 FacetsCollector fc2 = new FacetsCollector(sParams2, indexReader, taxoReader) {
188 protected FacetsAccumulator initFacetsAccumulator(FacetSearchParams facetSearchParams, IndexReader indexReader, TaxonomyReader taxonomyReader) {
189 FacetsAccumulator fa = new StandardFacetsAccumulator(facetSearchParams, indexReader, taxonomyReader);
190 fa.setComplementThreshold(FacetsAccumulator.DISABLE_COMPLEMENT);
195 searcher.search(new MatchAllDocsQuery(), fc2);
196 List<FacetResult> baseResults = fc2.getFacetResults();
197 FacetResult baseRes = baseResults.get(0);
199 // Removing the first line which holds the REQUEST and this is surly different between the two
200 String baseResultString = baseRes.toString();
201 baseResultString = baseResultString.substring(baseResultString.indexOf('\n'));
203 // Removing the first line
204 String resultString = res.toString();
205 resultString = resultString.substring(resultString.indexOf('\n'));
207 assertTrue("Results for k=MAX_VALUE do not match the regular results for k=1000!!",
208 baseResultString.equals(resultString));
215 public void testSimpleSearchForNonexistentFacet() throws Exception {
216 for (int partitionSize : partitionSizes) {
217 initIndex(partitionSize);
219 CategoryPath path = new CategoryPath("Miau Hattulla");
220 FacetSearchParams sParams = getFacetedSearchParams(partitionSize);
221 sParams.addFacetRequest(new CountFacetRequest(path, 10));
223 FacetsCollector fc = new FacetsCollector(sParams, indexReader, taxoReader);
225 searcher.search(new MatchAllDocsQuery(), fc);
227 long start = System.currentTimeMillis();
228 List<FacetResult> facetResults = fc.getFacetResults();
229 long end = System.currentTimeMillis();
232 System.out.println("Time: " + (end - start));
235 assertEquals("Shouldn't have found anything for a FacetRequest "
236 + "of a facet that doesn't exist in the index.", 0, facetResults.size());