pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / search / TestTopKResultsHandler.java
1 package org.apache.lucene.facet.search;
2
3 import java.util.Arrays;
4 import java.util.List;
5
6 import org.apache.lucene.index.IndexReader;
7 import org.apache.lucene.search.MatchAllDocsQuery;
8 import org.junit.Test;
9
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;
16
17 /**
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
24  *
25  *     http://www.apache.org/licenses/LICENSE-2.0
26  *
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.
32  */
33
34 public class TestTopKResultsHandler extends BaseTestTopK {
35
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"),
50   };
51
52   @Override
53   protected String getContent(int doc) {
54     return ALPHA;
55   }
56   
57   @Override
58   protected int numDocsToIndex() {
59     return CATEGORIES.length;
60   }
61   
62   @Override
63   protected List<CategoryPath> getCategories(int doc) {
64     return Arrays.asList(CATEGORIES[doc]);
65   }
66   
67   /**
68    * Strait forward test: Adding specific documents with specific facets and
69    * counting them in the most basic form.
70    */
71   @Test
72   public void testSimple() throws Exception {
73     for (int partitionSize : partitionSizes) {
74       initIndex(partitionSize);
75
76       // do different facet counts and compare to control
77       FacetSearchParams sParams = getFacetedSearchParams(partitionSize);
78       
79       sParams.addFacetRequest(new CountFacetRequest(new CategoryPath("a"), 100));
80       CountFacetRequest cfra = new CountFacetRequest(new CategoryPath("a"), 100);
81       cfra.setDepth(3);
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));
86
87       FacetsCollector fc = new FacetsCollector(sParams, indexReader, taxoReader) {
88         @Override
89         protected FacetsAccumulator initFacetsAccumulator(FacetSearchParams facetSearchParams, IndexReader indexReader, TaxonomyReader taxonomyReader) {
90           FacetsAccumulator fa = new StandardFacetsAccumulator(facetSearchParams, indexReader, taxonomyReader);
91           fa.setComplementThreshold(FacetsAccumulator.DISABLE_COMPLEMENT);
92           return fa;
93         }
94       };
95       
96       searcher.search(new MatchAllDocsQuery(), fc);
97       long start = System.currentTimeMillis();
98       List<FacetResult> facetResults = fc.getFacetResults();
99       long end = System.currentTimeMillis();
100
101       if (VERBOSE) {
102         System.out.println("Time: " + (end - start));
103       }
104
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);
111
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);
122
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);
131
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);
137
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);
143       closeAll();
144     }
145   }
146   
147   /**
148    * Creating an index, matching the results of an top K = Integer.MAX_VALUE and top-1000 requests
149    */
150   @Test
151   public void testGetMaxIntFacets() throws Exception {
152     for (int partitionSize : partitionSizes) {
153       initIndex(partitionSize);
154
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));
159
160       FacetsCollector fc = new FacetsCollector(sParams, indexReader, taxoReader) {
161         @Override
162         protected FacetsAccumulator initFacetsAccumulator(FacetSearchParams facetSearchParams, IndexReader indexReader, TaxonomyReader taxonomyReader) {
163           FacetsAccumulator fa = new StandardFacetsAccumulator(facetSearchParams, indexReader, taxonomyReader);
164           fa.setComplementThreshold(FacetsAccumulator.DISABLE_COMPLEMENT);
165           return fa;
166         }
167       };
168       
169       searcher.search(new MatchAllDocsQuery(), fc);
170       long start = System.currentTimeMillis();
171       List<FacetResult> results = fc.getFacetResults();
172       long end = System.currentTimeMillis();
173
174       if (VERBOSE) {
175         System.out.println("Time: " + (end - start));
176       }
177
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());
181
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));
185
186       FacetsCollector fc2 = new FacetsCollector(sParams2, indexReader, taxoReader) {
187         @Override
188         protected FacetsAccumulator initFacetsAccumulator(FacetSearchParams facetSearchParams, IndexReader indexReader, TaxonomyReader taxonomyReader) {
189           FacetsAccumulator fa = new StandardFacetsAccumulator(facetSearchParams, indexReader, taxonomyReader);
190           fa.setComplementThreshold(FacetsAccumulator.DISABLE_COMPLEMENT);
191           return fa;
192         }
193       };
194       
195       searcher.search(new MatchAllDocsQuery(), fc2);
196       List<FacetResult> baseResults = fc2.getFacetResults();
197       FacetResult baseRes = baseResults.get(0);
198
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'));
202       
203       // Removing the first line
204       String resultString = res.toString();
205       resultString = resultString.substring(resultString.indexOf('\n'));
206       
207       assertTrue("Results for k=MAX_VALUE do not match the regular results for k=1000!!",
208           baseResultString.equals(resultString));
209       
210       closeAll();
211     }
212   }
213
214   @Test
215   public void testSimpleSearchForNonexistentFacet() throws Exception {
216     for (int partitionSize : partitionSizes) {
217       initIndex(partitionSize);
218
219       CategoryPath path = new CategoryPath("Miau Hattulla");
220       FacetSearchParams sParams = getFacetedSearchParams(partitionSize);
221       sParams.addFacetRequest(new CountFacetRequest(path, 10));
222
223       FacetsCollector fc = new FacetsCollector(sParams, indexReader, taxoReader);
224       
225       searcher.search(new MatchAllDocsQuery(), fc);
226       
227       long start = System.currentTimeMillis();
228       List<FacetResult> facetResults = fc.getFacetResults();
229       long end = System.currentTimeMillis();
230       
231       if (VERBOSE) {
232         System.out.println("Time: " + (end - start));
233       }
234
235       assertEquals("Shouldn't have found anything for a FacetRequest "
236           + "of a facet that doesn't exist in the index.", 0, facetResults.size());
237
238       closeAll();
239     }
240   }
241 }