1 package org.apache.lucene.search;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
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;
33 public class TestBooleanScorer extends LuceneTestCase
35 private static final String FIELD = "category";
37 public void testMethod() throws Exception {
38 Directory directory = newDirectory();
40 String[] values = new String[] { "1", "2", "3", "4" };
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);
48 IndexReader ir = writer.getReader();
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);
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);
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();
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.
73 Similarity sim = Similarity.getDefault();
74 Scorer[] scorers = new Scorer[] {new Scorer(sim) {
76 @Override public float score() throws IOException { return 0; }
77 @Override public int docID() { return doc; }
79 @Override public int nextDoc() throws IOException {
80 return doc = doc == -1 ? 3000 : NO_MORE_DOCS;
83 @Override public int advance(int target) throws IOException {
84 return doc = target <= 3000 ? 3000 : NO_MORE_DOCS;
88 BooleanScorer bs = new BooleanScorer(null, false, sim, 1, Arrays.asList(scorers), null, scorers.length);
90 final List<Integer> hits = new ArrayList<Integer>();
91 bs.score(new Collector() {
94 public void setScorer(Scorer scorer) {
98 public void collect(int doc) throws IOException {
99 hits.add(docBase+doc);
103 public void setNextReader(IndexReader reader, int docBase) {
104 this.docBase = docBase;
108 public boolean acceptsDocsOutOfOrder() {
113 assertEquals("should have only 1 hit", 1, hits.size());
114 assertEquals("hit should have been docID=3000", 3000, hits.get(0).intValue());
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));
123 doc = new Document();
124 doc.add(new Field("field", "33", Field.Store.NO, Field.Index.ANALYZED));
126 final IndexReader r = w.getReader();
128 final IndexSearcher s = newSearcher(r);
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));
135 q.add(new BooleanClause(new TermQuery(new Term("field", "33")),
136 BooleanClause.Occur.SHOULD));
138 final int[] count = new int[1];
139 s.search(q, new Collector() {
140 private Scorer scorer;
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());
150 public void collect(int doc) throws IOException {
155 public void setNextReader(IndexReader reader, int docBase) {
159 public boolean acceptsDocsOutOfOrder() {
164 assertEquals(1, count[0]);