add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / search / TestPrefixInBooleanQuery.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 org.apache.lucene.util.LuceneTestCase;
21
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.document.Field;
24 import org.apache.lucene.index.IndexReader;
25 import org.apache.lucene.index.RandomIndexWriter;
26 import org.apache.lucene.index.Term;
27 import org.apache.lucene.store.Directory;
28 import org.junit.AfterClass;
29 import org.junit.BeforeClass;
30
31 /**
32  * https://issues.apache.org/jira/browse/LUCENE-1974
33  *
34  * represent the bug of 
35  * 
36  *    BooleanScorer.score(Collector collector, int max, int firstDocID)
37  * 
38  * Line 273, end=8192, subScorerDocID=11378, then more got false?
39  * 
40  */
41 public class TestPrefixInBooleanQuery extends LuceneTestCase {
42
43   private static final String FIELD = "name";
44   private static Directory directory;
45   private static IndexReader reader;
46   private static IndexSearcher searcher;
47
48   @BeforeClass
49   public static void beforeClass() throws Exception {
50     directory = newDirectory();
51     RandomIndexWriter writer = new RandomIndexWriter(random, directory);
52
53     Document doc = new Document();
54     Field field = newField(FIELD, "meaninglessnames", Field.Store.NO,
55         Field.Index.NOT_ANALYZED_NO_NORMS);
56     doc.add(field);
57     
58     for (int i = 0; i < 5137; ++i) {
59       writer.addDocument(doc);
60     }
61     
62     field.setValue("tangfulin");
63     writer.addDocument(doc);
64
65     field.setValue("meaninglessnames");
66     for (int i = 5138; i < 11377; ++i) {
67       writer.addDocument(doc);
68     }
69     
70     field.setValue("tangfulin");
71     writer.addDocument(doc);
72     
73     reader = writer.getReader();
74     searcher = newSearcher(reader);
75     writer.close();
76   }
77   
78   @AfterClass
79   public static void afterClass() throws Exception {
80     searcher.close();
81     searcher = null;
82     reader.close();
83     reader = null;
84     directory.close();
85     directory = null;
86   }
87   
88   public void testPrefixQuery() throws Exception {
89     Query query = new PrefixQuery(new Term(FIELD, "tang"));
90     assertEquals("Number of matched documents", 2,
91                  searcher.search(query, null, 1000).totalHits);
92   }
93   public void testTermQuery() throws Exception {
94     Query query = new TermQuery(new Term(FIELD, "tangfulin"));
95     assertEquals("Number of matched documents", 2,
96                  searcher.search(query, null, 1000).totalHits);
97   }
98   public void testTermBooleanQuery() throws Exception {
99     BooleanQuery query = new BooleanQuery();
100     query.add(new TermQuery(new Term(FIELD, "tangfulin")),
101               BooleanClause.Occur.SHOULD);
102     query.add(new TermQuery(new Term(FIELD, "notexistnames")),
103               BooleanClause.Occur.SHOULD);
104     assertEquals("Number of matched documents", 2,
105                  searcher.search(query, null, 1000).totalHits);
106
107   }
108   public void testPrefixBooleanQuery() throws Exception {
109     BooleanQuery query = new BooleanQuery();
110     query.add(new PrefixQuery(new Term(FIELD, "tang")),
111               BooleanClause.Occur.SHOULD);
112     query.add(new TermQuery(new Term(FIELD, "notexistnames")),
113               BooleanClause.Occur.SHOULD);
114     assertEquals("Number of matched documents", 2,
115                  searcher.search(query, null, 1000).totalHits);
116   }
117 }