add --shared
[pylucene.git] / lucene-java-3.4.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.Arrays;
22
23 import org.apache.lucene.document.Document;
24 import org.apache.lucene.document.Field;
25 import org.apache.lucene.index.IndexReader;
26 import org.apache.lucene.index.RandomIndexWriter;
27 import org.apache.lucene.index.Term;
28 import org.apache.lucene.store.Directory;
29
30 import org.apache.lucene.util.LuceneTestCase;
31
32 public class TestBooleanScorer extends LuceneTestCase
33 {
34   private static final String FIELD = "category";
35   
36   public void testMethod() throws Exception {
37     Directory directory = newDirectory();
38
39     String[] values = new String[] { "1", "2", "3", "4" };
40
41     RandomIndexWriter writer = new RandomIndexWriter(random, directory);
42     for (int i = 0; i < values.length; i++) {
43       Document doc = new Document();
44       doc.add(newField(FIELD, values[i], Field.Store.YES, Field.Index.NOT_ANALYZED));
45       writer.addDocument(doc);
46     }
47     IndexReader ir = writer.getReader();
48     writer.close();
49
50     BooleanQuery booleanQuery1 = new BooleanQuery();
51     booleanQuery1.add(new TermQuery(new Term(FIELD, "1")), BooleanClause.Occur.SHOULD);
52     booleanQuery1.add(new TermQuery(new Term(FIELD, "2")), BooleanClause.Occur.SHOULD);
53
54     BooleanQuery query = new BooleanQuery();
55     query.add(booleanQuery1, BooleanClause.Occur.MUST);
56     query.add(new TermQuery(new Term(FIELD, "9")), BooleanClause.Occur.MUST_NOT);
57
58     IndexSearcher indexSearcher = newSearcher(ir);
59     ScoreDoc[] hits = indexSearcher.search(query, null, 1000).scoreDocs;
60     assertEquals("Number of matched documents", 2, hits.length);
61     indexSearcher.close();
62     ir.close();
63     directory.close();
64   }
65   
66   public void testEmptyBucketWithMoreDocs() throws Exception {
67     // This test checks the logic of nextDoc() when all sub scorers have docs
68     // beyond the first bucket (for example). Currently, the code relies on the
69     // 'more' variable to work properly, and this test ensures that if the logic
70     // changes, we have a test to back it up.
71     
72     Similarity sim = Similarity.getDefault();
73     Scorer[] scorers = new Scorer[] {new Scorer(sim) {
74       private int doc = -1;
75       @Override public float score() throws IOException { return 0; }
76       @Override public int docID() { return doc; }
77       
78       @Override public int nextDoc() throws IOException {
79         return doc = doc == -1 ? 3000 : NO_MORE_DOCS;
80       }
81
82       @Override public int advance(int target) throws IOException {
83         return doc = target <= 3000 ? 3000 : NO_MORE_DOCS;
84       }
85       
86     }};
87     BooleanScorer bs = new BooleanScorer(null, false, sim, 1, Arrays.asList(scorers), null, scorers.length);
88     
89     assertEquals("should have received 3000", 3000, bs.nextDoc());
90     assertEquals("should have received NO_MORE_DOCS", DocIdSetIterator.NO_MORE_DOCS, bs.nextDoc());
91   }
92
93 }