pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / search / TestTopKResultsHandlerRandom.java
1 package org.apache.lucene.facet.search;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.List;
6
7 import org.apache.lucene.index.IndexReader;
8 import org.apache.lucene.search.MatchAllDocsQuery;
9 import org.apache.lucene.search.Query;
10 import org.junit.Test;
11
12 import org.apache.lucene.facet.search.params.FacetSearchParams;
13 import org.apache.lucene.facet.search.results.FacetResult;
14 import org.apache.lucene.facet.search.results.FacetResultNode;
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 TestTopKResultsHandlerRandom extends BaseTestTopK {
35   
36   private List<FacetResult> countFacets(int partitionSize, int numResults, final boolean doComplement)
37       throws IOException, IllegalAccessException, InstantiationException {
38     Query q = new MatchAllDocsQuery();
39     FacetSearchParams facetSearchParams = searchParamsWithRequests(numResults, partitionSize);
40     FacetsCollector fc = new FacetsCollector(facetSearchParams, indexReader, taxoReader) {
41       @Override
42       protected FacetsAccumulator initFacetsAccumulator(
43           FacetSearchParams facetSearchParams, IndexReader indexReader,
44           TaxonomyReader taxonomyReader) {
45         FacetsAccumulator accumulator = new StandardFacetsAccumulator(facetSearchParams, indexReader, taxonomyReader);
46         double complement = doComplement ? FacetsAccumulator.FORCE_COMPLEMENT : FacetsAccumulator.DISABLE_COMPLEMENT;
47         accumulator.setComplementThreshold(complement);
48         return accumulator;
49       }
50     };
51     searcher.search(q, fc);
52     List<FacetResult> facetResults = fc.getFacetResults();
53     return facetResults;
54   }
55
56   /**
57    * Test that indeed top results are returned, ordered same as all results 
58    * also when some facets have the same counts.
59    */
60   @Test
61   public void testTopCountsOrder() throws Exception {
62     for (int partitionSize : partitionSizes) {
63       initIndex(partitionSize);
64       
65       /*
66        * Try out faceted search in it's most basic form (no sampling nor complement
67        * that is). In this test lots (and lots..) of randomly generated data is
68        * being indexed, and later on an "over-all" faceted search is performed. The
69        * results are checked against the DF of each facet by itself
70        */
71       List<FacetResult> facetResults = countFacets(partitionSize, 100000, false);
72       assertCountsAndCardinality(facetCountsTruth(), facetResults);
73       
74       /*
75        * Try out faceted search with complements. In this test lots (and lots..) of
76        * randomly generated data is being indexed, and later on, a "beta" faceted
77        * search is performed - retrieving ~90% of the documents so complements takes
78        * place in here. The results are checked against the a regular (a.k.a
79        * no-complement, no-sampling) faceted search with the same parameters.
80        */
81       facetResults = countFacets(partitionSize, 100000, true);
82       assertCountsAndCardinality(facetCountsTruth(), facetResults);
83       
84       List<FacetResult> allFacetResults = countFacets(partitionSize, 100000, false);
85       
86       HashMap<String,Integer> all = new HashMap<String,Integer>();
87       int maxNumNodes = 0;
88       int k = 0;
89       for (FacetResult fr : allFacetResults) {
90         FacetResultNode topResNode = fr.getFacetResultNode();
91         maxNumNodes = Math.max(maxNumNodes, topResNode.getNumSubResults());
92         int prevCount = Integer.MAX_VALUE;
93         int pos = 0;
94         for (FacetResultNode frn: topResNode.getSubResults()) {
95           assertTrue("wrong counts order: prev="+prevCount+" curr="+frn.getValue(), prevCount>=frn.getValue());
96           prevCount = (int) frn.getValue();
97           String key = k+"--"+frn.getLabel()+"=="+frn.getValue();
98           if (VERBOSE) {
99             System.out.println(frn.getLabel() + " - " + frn.getValue() + "  "+key+"  "+pos);
100           }
101           all.put(key, pos++); // will use this later to verify order of sub-results
102         }
103         k++;
104       }
105       
106       // verify that when asking for less results, they are always of highest counts
107       // also verify that the order is stable
108       for (int n=1; n<maxNumNodes; n++) {
109         if (VERBOSE) {
110           System.out.println("-------  verify for "+n+" top results");
111         }
112         List<FacetResult> someResults = countFacets(partitionSize, n, false);
113         k = 0;
114         for (FacetResult fr : someResults) {
115           FacetResultNode topResNode = fr.getFacetResultNode();
116           assertTrue("too many results: n="+n+" but got "+topResNode.getNumSubResults(), n>=topResNode.getNumSubResults());
117           int pos = 0;
118           for (FacetResultNode frn: topResNode.getSubResults()) {
119             String key = k+"--"+frn.getLabel()+"=="+frn.getValue();
120             if (VERBOSE) {
121               System.out.println(frn.getLabel() + " - " + frn.getValue() + "  "+key+"  "+pos);
122             }
123             Integer origPos = all.get(key);
124             assertNotNull("missing in all results: "+frn,origPos);
125             assertEquals("wrong order of sub-results!",pos++, origPos.intValue()); // verify order of sub-results
126           }
127           k++;
128         }
129       }
130       
131       closeAll(); // done with this partition
132     }
133   }
134
135 }