pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / 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 }