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.Arrays;
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;
30 import org.apache.lucene.util.LuceneTestCase;
32 public class TestBooleanScorer extends LuceneTestCase
34 private static final String FIELD = "category";
36 public void testMethod() throws Exception {
37 Directory directory = newDirectory();
39 String[] values = new String[] { "1", "2", "3", "4" };
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);
47 IndexReader ir = writer.getReader();
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);
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);
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();
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.
72 Similarity sim = Similarity.getDefault();
73 Scorer[] scorers = new Scorer[] {new Scorer(sim) {
75 @Override public float score() throws IOException { return 0; }
76 @Override public int docID() { return doc; }
78 @Override public int nextDoc() throws IOException {
79 return doc = doc == -1 ? 3000 : NO_MORE_DOCS;
82 @Override public int advance(int target) throws IOException {
83 return doc = target <= 3000 ? 3000 : NO_MORE_DOCS;
87 BooleanScorer bs = new BooleanScorer(null, false, sim, 1, Arrays.asList(scorers), null, scorers.length);
89 assertEquals("should have received 3000", 3000, bs.nextDoc());
90 assertEquals("should have received NO_MORE_DOCS", DocIdSetIterator.NO_MORE_DOCS, bs.nextDoc());