pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / search / TestBooleanScorer.java
1 package org.apache.lucene.search;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24
25 import org.apache.lucene.document.Document;
26 import org.apache.lucene.document.Field;
27 import org.apache.lucene.index.IndexReader;
28 import org.apache.lucene.index.RandomIndexWriter;
29 import org.apache.lucene.index.Term;
30 import org.apache.lucene.store.Directory;
31 import org.apache.lucene.util.LuceneTestCase;
32
33 public class TestBooleanScorer extends LuceneTestCase
34 {
35   private static final String FIELD = "category";
36   
37   public void testMethod() throws Exception {
38     Directory directory = newDirectory();
39
40     String[] values = new String[] { "1", "2", "3", "4" };
41
42     RandomIndexWriter writer = new RandomIndexWriter(random, directory);
43     for (int i = 0; i < values.length; i++) {
44       Document doc = new Document();
45       doc.add(newField(FIELD, values[i], Field.Store.YES, Field.Index.NOT_ANALYZED));
46       writer.addDocument(doc);
47     }
48     IndexReader ir = writer.getReader();
49     writer.close();
50
51     BooleanQuery booleanQuery1 = new BooleanQuery();
52     booleanQuery1.add(new TermQuery(new Term(FIELD, "1")), BooleanClause.Occur.SHOULD);
53     booleanQuery1.add(new TermQuery(new Term(FIELD, "2")), BooleanClause.Occur.SHOULD);
54
55     BooleanQuery query = new BooleanQuery();
56     query.add(booleanQuery1, BooleanClause.Occur.MUST);
57     query.add(new TermQuery(new Term(FIELD, "9")), BooleanClause.Occur.MUST_NOT);
58
59     IndexSearcher indexSearcher = newSearcher(ir);
60     ScoreDoc[] hits = indexSearcher.search(query, null, 1000).scoreDocs;
61     assertEquals("Number of matched documents", 2, hits.length);
62     indexSearcher.close();
63     ir.close();
64     directory.close();
65   }
66   
67   public void testEmptyBucketWithMoreDocs() throws Exception {
68     // This test checks the logic of nextDoc() when all sub scorers have docs
69     // beyond the first bucket (for example). Currently, the code relies on the
70     // 'more' variable to work properly, and this test ensures that if the logic
71     // changes, we have a test to back it up.
72     
73     Similarity sim = Similarity.getDefault();
74     Scorer[] scorers = new Scorer[] {new Scorer(sim) {
75       private int doc = -1;
76       @Override public float score() throws IOException { return 0; }
77       @Override public int docID() { return doc; }
78       
79       @Override public int nextDoc() throws IOException {
80         return doc = doc == -1 ? 3000 : NO_MORE_DOCS;
81       }
82
83       @Override public int advance(int target) throws IOException {
84         return doc = target <= 3000 ? 3000 : NO_MORE_DOCS;
85       }
86       
87     }};
88     BooleanScorer bs = new BooleanScorer(null, false, sim, 1, Arrays.asList(scorers), null, scorers.length);
89
90     final List<Integer> hits = new ArrayList<Integer>();
91     bs.score(new Collector() {
92       int docBase;
93       @Override
94       public void setScorer(Scorer scorer) {
95       }
96       
97       @Override
98       public void collect(int doc) throws IOException {
99         hits.add(docBase+doc);
100       }
101       
102       @Override
103       public void setNextReader(IndexReader reader, int docBase) {
104         this.docBase = docBase;
105       }
106       
107       @Override
108       public boolean acceptsDocsOutOfOrder() {
109         return true;
110       }
111       });
112
113     assertEquals("should have only 1 hit", 1, hits.size());
114     assertEquals("hit should have been docID=3000", 3000, hits.get(0).intValue());
115   }
116
117   public void testMoreThan32ProhibitedClauses() throws Exception {
118     final Directory d = newDirectory();
119     final RandomIndexWriter w = new RandomIndexWriter(random, d);
120     Document doc = new Document();
121     doc.add(new Field("field", "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33", Field.Store.NO, Field.Index.ANALYZED));
122     w.addDocument(doc);
123     doc = new Document();
124     doc.add(new Field("field", "33", Field.Store.NO, Field.Index.ANALYZED));
125     w.addDocument(doc);
126     final IndexReader r = w.getReader();
127     w.close();
128     final IndexSearcher s = newSearcher(r);
129
130     final BooleanQuery q = new BooleanQuery();
131     for(int term=0;term<33;term++) {
132       q.add(new BooleanClause(new TermQuery(new Term("field", ""+term)),
133                               BooleanClause.Occur.MUST_NOT));
134     }
135     q.add(new BooleanClause(new TermQuery(new Term("field", "33")),
136                             BooleanClause.Occur.SHOULD));
137                             
138     final int[] count = new int[1];
139     s.search(q, new Collector() {
140       private Scorer scorer;
141     
142       @Override
143       public void setScorer(Scorer scorer) {
144         // Make sure we got BooleanScorer:
145         this.scorer = scorer;
146         assertEquals("Scorer is implemented by wrong class", BooleanScorer.class.getName() + "$BucketScorer", scorer.getClass().getName());
147       }
148       
149       @Override
150       public void collect(int doc) throws IOException {
151         count[0]++;
152       }
153       
154       @Override
155       public void setNextReader(IndexReader reader, int docBase) {
156       }
157       
158       @Override
159       public boolean acceptsDocsOutOfOrder() {
160         return true;
161       }
162     });
163
164     assertEquals(1, count[0]);
165     
166     s.close();
167     r.close();
168     d.close();
169   }
170 }