add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / search / TestScoredDocIdCollector.java
1 package org.apache.lucene.facet.search;
2
3 import java.io.IOException;
4 import java.util.Arrays;
5 import java.util.List;
6
7 import org.apache.lucene.index.Term;
8 import org.apache.lucene.search.Query;
9 import org.apache.lucene.search.TermQuery;
10 import org.junit.Before;
11 import org.junit.Test;
12
13 import org.apache.lucene.facet.FacetTestBase;
14 import org.apache.lucene.facet.search.FacetsAccumulator;
15 import org.apache.lucene.facet.search.ScoredDocIDs;
16 import org.apache.lucene.facet.search.ScoredDocIDsIterator;
17 import org.apache.lucene.facet.search.ScoredDocIdCollector;
18 import org.apache.lucene.facet.search.StandardFacetsAccumulator;
19 import org.apache.lucene.facet.search.params.CountFacetRequest;
20 import org.apache.lucene.facet.search.params.FacetSearchParams;
21 import org.apache.lucene.facet.search.params.ScoreFacetRequest;
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 /** Test ScoredDocIdCollector. */
44 public class TestScoredDocIdCollector extends FacetTestBase {
45
46   @Override
47   @Before
48   public void setUp() throws Exception {
49     super.setUp();
50     initIndex();
51   }
52
53   @Override
54   public void tearDown() throws Exception {
55     closeAll();
56     super.tearDown();
57   }
58
59   @Test
60   public void testConstantScore() throws Exception {
61     // test that constant score works well
62
63     Query q = new TermQuery(new Term(CONTENT_FIELD, "white"));
64     if (VERBOSE) {
65       System.out.println("Query: " + q);
66     }
67     float constScore = 17.0f;
68     ScoredDocIdCollector dCollector = ScoredDocIdCollector.create(indexReader
69         .maxDoc(), false); // scoring is disabled
70     dCollector.setDefaultScore(constScore);
71     searcher.search(q, dCollector);
72
73     // verify by doc scores at the level of doc-id-iterator
74     ScoredDocIDs scoredDocIDs = dCollector.getScoredDocIDs();
75     assertEquals("Wrong number of matching documents!", 2, scoredDocIDs.size());
76     ScoredDocIDsIterator docItr = scoredDocIDs.iterator();
77     while (docItr.next()) {
78       assertEquals("Wrong score for doc " + docItr.getDocID(), constScore,
79           docItr.getScore(), Double.MIN_VALUE);
80     }
81
82     // verify by facet values
83     List<FacetResult> countRes = findFacets(scoredDocIDs, getFacetedSearchParams());
84     List<FacetResult> scoreRes = findFacets(scoredDocIDs, sumScoreSearchParams());
85
86     assertEquals("Wrong number of facet count results!", 1, countRes.size());
87     assertEquals("Wrong number of facet score results!", 1, scoreRes.size());
88
89     FacetResultNode parentCountRes = countRes.get(0).getFacetResultNode();
90     FacetResultNode parentScoreRes = scoreRes.get(0).getFacetResultNode();
91
92     assertEquals("Wrong number of top count aggregated categories!", 3,
93         parentCountRes.getNumSubResults());
94     assertEquals("Wrong number of top score aggregated categories!", 3,
95         parentScoreRes.getNumSubResults());
96
97     // rely on that facet value is computed as doc-score, and
98     // accordingly compare values of the two top-category results.
99
100     FacetResultNode[] countResNodes = resultNodesAsArray(parentCountRes);
101     FacetResultNode[] scoreResNodes = resultNodesAsArray(parentScoreRes);
102
103     for (int i = 0; i < scoreResNodes.length; i++) {
104       assertEquals("Ordinals differ!", 
105           countResNodes[i].getOrdinal(), scoreResNodes[i].getOrdinal());
106       assertEquals("Wrong scores!", 
107           constScore * countResNodes[i].getValue(),
108           scoreResNodes[i].getValue(), 
109           Double.MIN_VALUE);
110     }
111   }
112
113   // compute facets with certain facet requests and docs
114   private List<FacetResult> findFacets(ScoredDocIDs sDocids,
115       FacetSearchParams facetSearchParams) throws IOException {
116     FacetsAccumulator fAccumulator = new StandardFacetsAccumulator(
117         facetSearchParams, indexReader, taxoReader);
118     List<FacetResult> res = fAccumulator.accumulate(sDocids);
119
120     // Results are ready, printing them...
121     int i = 0;
122     for (FacetResult facetResult : res) {
123       if (VERBOSE) {
124         System.out.println("Res " + (i++) + ": " + facetResult);
125       }
126     }
127
128     return res;
129   }
130
131   @Test
132   public void testOutOfOrderCollectionScoringEnabled() throws Exception {
133     assertFalse(
134         "when scoring enabled, out-of-order collection should not be supported",
135         ScoredDocIdCollector.create(1, true).acceptsDocsOutOfOrder());
136   }
137
138   @Test
139   public void testOutOfOrderCollectionScoringDisabled() throws Exception {
140     // This used to fail, because ScoredDocIdCollector.acceptDocsOutOfOrder
141     // returned true, even when scoring was enabled.
142     final int[] docs = new int[] { 1, 0, 2 }; // out of order on purpose
143
144     ScoredDocIdCollector sdic = ScoredDocIdCollector.create(docs.length, false);
145     assertTrue(
146         "when scoring disabled, out-of-order collection should be supported",
147         sdic.acceptsDocsOutOfOrder());
148     for (int i = 0; i < docs.length; i++) {
149       sdic.collect(docs[i]);
150     }
151
152     assertEquals("expected 3 documents but got " + sdic.getScoredDocIDs().size(), 3, sdic.getScoredDocIDs().size());
153     ScoredDocIDsIterator iter = sdic.getScoredDocIDs().iterator();
154     Arrays.sort(docs);
155     for (int i = 0; iter.next(); i++) {
156       assertEquals("expected doc " + docs[i], docs[i], iter.getDocID());
157     }
158   }
159
160   /* use a scoring aggregator */
161   private FacetSearchParams sumScoreSearchParams() {
162     // this will use default faceted indexing params, not altering anything about indexing
163     FacetSearchParams res = super.getFacetedSearchParams();
164     res.addFacetRequest(new ScoreFacetRequest(new CategoryPath("root", "a"), 10));
165     return res;
166   }
167
168   @Override
169   protected FacetSearchParams getFacetedSearchParams() {
170     FacetSearchParams res = super.getFacetedSearchParams();
171     res.addFacetRequest(new CountFacetRequest(new CategoryPath("root","a"), 10));
172     return res;
173   }
174
175 }