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();