add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / search / TestFacetsAccumulatorWithComplement.java
1 package org.apache.lucene.facet.search;
2
3 import java.io.IOException;
4 import java.util.List;
5
6 import org.apache.lucene.index.IndexReader;
7 import org.apache.lucene.index.MultiReader;
8 import org.apache.lucene.index.ParallelReader;
9 import org.apache.lucene.search.MatchAllDocsQuery;
10 import org.apache.lucene.search.Query;
11 import org.junit.After;
12 import org.junit.Before;
13 import org.junit.Test;
14
15 import org.apache.lucene.facet.FacetTestBase;
16 import org.apache.lucene.facet.search.FacetsAccumulator;
17 import org.apache.lucene.facet.search.ScoredDocIDs;
18 import org.apache.lucene.facet.search.ScoredDocIdCollector;
19 import org.apache.lucene.facet.search.StandardFacetsAccumulator;
20 import org.apache.lucene.facet.search.params.CountFacetRequest;
21 import org.apache.lucene.facet.search.params.FacetSearchParams;
22 import org.apache.lucene.facet.search.results.FacetResult;
23 import org.apache.lucene.facet.search.results.FacetResultNode;
24 import org.apache.lucene.facet.taxonomy.CategoryPath;
25
26 /**
27  * Licensed to the Apache Software Foundation (ASF) under one or more
28  * contributor license agreements.  See the NOTICE file distributed with
29  * this work for additional information regarding copyright ownership.
30  * The ASF licenses this file to You under the Apache License, Version 2.0
31  * (the "License"); you may not use this file except in compliance with
32  * the License.  You may obtain a copy of the License at
33  *
34  *     http://www.apache.org/licenses/LICENSE-2.0
35  *
36  * Unless required by applicable law or agreed to in writing, software
37  * distributed under the License is distributed on an "AS IS" BASIS,
38  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39  * See the License for the specific language governing permissions and
40  * limitations under the License.
41  */
42
43 /**
44  * Test that complementsworks as expected.
45  * We place this test under *.facet.search rather than *.search
46  * because the test actually does faceted search.
47  */
48 public class TestFacetsAccumulatorWithComplement extends FacetTestBase {
49   
50   @Override
51   @Before
52   public void setUp() throws Exception {
53     super.setUp();
54     initIndex();
55   }
56   
57   @Override
58   @After
59   public void tearDown() throws Exception {
60     closeAll();
61     super.tearDown();
62   }
63   
64   /**
65    * Test that complements does not cause a failure when using a parallel reader
66    */
67   @Test
68   public void testComplementsWithParallerReader() throws Exception {
69     IndexReader origReader = indexReader; 
70     ParallelReader pr = new ParallelReader(true);
71     pr.add(origReader);
72     indexReader = pr;
73     try {
74       doTestComplements();
75     } finally {
76       indexReader = origReader;
77     }
78   }
79
80   /**
81    * Test that complements works with MultiReader
82    */
83   @Test
84   public void testComplementsWithMultiReader() throws Exception {
85     final IndexReader origReader = indexReader; 
86     indexReader = new MultiReader(origReader);
87     try {
88       doTestComplements();
89     } finally {
90       indexReader = origReader;
91     }
92   }
93   
94   /**
95    * Test that score is indeed constant when using a constant score
96    */
97   @Test
98   public void testComplements() throws Exception {
99     doTestComplements();
100   }
101   
102   private void doTestComplements() throws Exception {
103     Query q = new MatchAllDocsQuery(); //new TermQuery(new Term(TEXT,"white"));
104     if (VERBOSE) {
105       System.out.println("Query: "+q);
106     }
107     ScoredDocIdCollector dCollector = 
108       ScoredDocIdCollector.create(indexReader.maxDoc(),false); // scoring is disabled
109     searcher.search(q, dCollector);
110     
111     // verify by facet values
112     List<FacetResult> countResWithComplement = findFacets(dCollector.getScoredDocIDs(), true);
113     List<FacetResult> countResNoComplement = findFacets(dCollector.getScoredDocIDs(), false);
114     
115     assertEquals("Wrong number of facet count results with complement!",1,countResWithComplement.size());
116     assertEquals("Wrong number of facet count results no complement!",1,countResNoComplement.size());
117     
118     FacetResultNode parentResWithComp = countResWithComplement.get(0).getFacetResultNode();
119     FacetResultNode parentResNoComp = countResWithComplement.get(0).getFacetResultNode();
120     
121     assertEquals("Wrong number of top count aggregated categories with complement!",3,parentResWithComp.getNumSubResults());
122     assertEquals("Wrong number of top count aggregated categories no complement!",3,parentResNoComp.getNumSubResults());
123     
124   }
125   
126   @Override
127   protected FacetSearchParams getFacetedSearchParams() {
128     FacetSearchParams res = super.getFacetedSearchParams();
129     res.addFacetRequest(new CountFacetRequest(new CategoryPath("root","a"), 10));
130     return res;
131   }
132   
133   /** compute facets with certain facet requests and docs */
134   private List<FacetResult> findFacets(ScoredDocIDs sDocids, boolean withComplement) throws IOException {
135     
136     FacetsAccumulator fAccumulator = 
137       new StandardFacetsAccumulator(getFacetedSearchParams(), indexReader, taxoReader);
138     
139     fAccumulator.setComplementThreshold(
140         withComplement ? 
141             FacetsAccumulator.FORCE_COMPLEMENT: 
142               FacetsAccumulator.DISABLE_COMPLEMENT);
143     
144     List<FacetResult> res = fAccumulator.accumulate(sDocids);
145     
146     // Results are ready, printing them...
147     int i = 0;
148     for (FacetResult facetResult : res) {
149       if (VERBOSE) {
150         System.out.println("Res "+(i++)+": "+facetResult);
151       }
152     }
153     
154     assertEquals(withComplement, ((StandardFacetsAccumulator) fAccumulator).isUsingComplements);
155     
156     return res;
157   }
158   
159 }