1 package org.apache.lucene.facet.search;
3 import java.io.IOException;
4 import java.util.HashMap;
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;
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;
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 TestTopKResultsHandlerRandom extends BaseTestTopK {
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) {
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);
51 searcher.search(q, fc);
52 List<FacetResult> facetResults = fc.getFacetResults();
57 * Test that indeed top results are returned, ordered same as all results
58 * also when some facets have the same counts.
61 public void testTopCountsOrder() throws Exception {
62 for (int partitionSize : partitionSizes) {
63 initIndex(partitionSize);
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
71 List<FacetResult> facetResults = countFacets(partitionSize, 100000, false);
72 assertCountsAndCardinality(facetCountsTruth(), facetResults);
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.
81 facetResults = countFacets(partitionSize, 100000, true);
82 assertCountsAndCardinality(facetCountsTruth(), facetResults);
84 List<FacetResult> allFacetResults = countFacets(partitionSize, 100000, false);
86 HashMap<String,Integer> all = new HashMap<String,Integer>();
89 for (FacetResult fr : allFacetResults) {
90 FacetResultNode topResNode = fr.getFacetResultNode();
91 maxNumNodes = Math.max(maxNumNodes, topResNode.getNumSubResults());
92 int prevCount = Integer.MAX_VALUE;
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();
99 System.out.println(frn.getLabel() + " - " + frn.getValue() + " "+key+" "+pos);
101 all.put(key, pos++); // will use this later to verify order of sub-results
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++) {
110 System.out.println("------- verify for "+n+" top results");
112 List<FacetResult> someResults = countFacets(partitionSize, n, false);
114 for (FacetResult fr : someResults) {
115 FacetResultNode topResNode = fr.getFacetResultNode();
116 assertTrue("too many results: n="+n+" but got "+topResNode.getNumSubResults(), n>=topResNode.getNumSubResults());
118 for (FacetResultNode frn: topResNode.getSubResults()) {
119 String key = k+"--"+frn.getLabel()+"=="+frn.getValue();
121 System.out.println(frn.getLabel() + " - " + frn.getValue() + " "+key+" "+pos);
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
131 closeAll(); // done with this partition