1 package org.apache.lucene.facet.search;
3 import java.io.IOException;
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;
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;
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
34 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.
48 public class TestFacetsAccumulatorWithComplement extends FacetTestBase {
52 public void setUp() throws Exception {
59 public void tearDown() throws Exception {
65 * Test that complements does not cause a failure when using a parallel reader
68 public void testComplementsWithParallerReader() throws Exception {
69 IndexReader origReader = indexReader;
70 ParallelReader pr = new ParallelReader(true);
76 indexReader = origReader;
81 * Test that complements works with MultiReader
84 public void testComplementsWithMultiReader() throws Exception {
85 final IndexReader origReader = indexReader;
86 indexReader = new MultiReader(origReader);
90 indexReader = origReader;
95 * Test that score is indeed constant when using a constant score
98 public void testComplements() throws Exception {
102 private void doTestComplements() throws Exception {
103 Query q = new MatchAllDocsQuery(); //new TermQuery(new Term(TEXT,"white"));
105 System.out.println("Query: "+q);
107 ScoredDocIdCollector dCollector =
108 ScoredDocIdCollector.create(indexReader.maxDoc(),false); // scoring is disabled
109 searcher.search(q, dCollector);
111 // verify by facet values
112 List<FacetResult> countResWithComplement = findFacets(dCollector.getScoredDocIDs(), true);
113 List<FacetResult> countResNoComplement = findFacets(dCollector.getScoredDocIDs(), false);
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());
118 FacetResultNode parentResWithComp = countResWithComplement.get(0).getFacetResultNode();
119 FacetResultNode parentResNoComp = countResWithComplement.get(0).getFacetResultNode();
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());
127 protected FacetSearchParams getFacetedSearchParams() {
128 FacetSearchParams res = super.getFacetedSearchParams();
129 res.addFacetRequest(new CountFacetRequest(new CategoryPath("root","a"), 10));
133 /** compute facets with certain facet requests and docs */
134 private List<FacetResult> findFacets(ScoredDocIDs sDocids, boolean withComplement) throws IOException {
136 FacetsAccumulator fAccumulator =
137 new StandardFacetsAccumulator(getFacetedSearchParams(), indexReader, taxoReader);
139 fAccumulator.setComplementThreshold(
141 FacetsAccumulator.FORCE_COMPLEMENT:
142 FacetsAccumulator.DISABLE_COMPLEMENT);
144 List<FacetResult> res = fAccumulator.accumulate(sDocids);
146 // Results are ready, printing them...
148 for (FacetResult facetResult : res) {
150 System.out.println("Res "+(i++)+": "+facetResult);
154 assertEquals(withComplement, ((StandardFacetsAccumulator) fAccumulator).isUsingComplements);