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 org.apache.lucene.util.LuceneTestCase;
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;
32 * https://issues.apache.org/jira/browse/LUCENE-1974
34 * represent the bug of
36 * BooleanScorer.score(Collector collector, int max, int firstDocID)
38 * Line 273, end=8192, subScorerDocID=11378, then more got false?
41 public class TestPrefixInBooleanQuery extends LuceneTestCase {
43 private static final String FIELD = "name";
44 private static Directory directory;
45 private static IndexReader reader;
46 private static IndexSearcher searcher;
49 public static void beforeClass() throws Exception {
50 directory = newDirectory();
51 RandomIndexWriter writer = new RandomIndexWriter(random, directory);
53 Document doc = new Document();
54 Field field = newField(FIELD, "meaninglessnames", Field.Store.NO,
55 Field.Index.NOT_ANALYZED_NO_NORMS);
58 for (int i = 0; i < 5137; ++i) {
59 writer.addDocument(doc);
62 field.setValue("tangfulin");
63 writer.addDocument(doc);
65 field.setValue("meaninglessnames");
66 for (int i = 5138; i < 11377; ++i) {
67 writer.addDocument(doc);
70 field.setValue("tangfulin");
71 writer.addDocument(doc);
73 reader = writer.getReader();
74 searcher = newSearcher(reader);
79 public static void afterClass() throws Exception {
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);
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);
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);
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);