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;
22
import org.apache.lucene.analysis.MockAnalyzer;
23
import org.apache.lucene.analysis.MockTokenizer;
24
import org.apache.lucene.document.Document;
25
import org.apache.lucene.document.Field;
26
import org.apache.lucene.index.IndexReader;
27
import org.apache.lucene.index.Term;
28
import org.apache.lucene.index.RandomIndexWriter;
29
import org.apache.lucene.store.Directory;
30
import org.apache.lucene.util.LuceneTestCase;
31
import org.apache.lucene.util._TestUtil;
34
* Create an index with random unicode terms
35
* Generates random prefix queries, and validates against a simple impl.
37
public class TestPrefixRandom extends LuceneTestCase {
38
private IndexSearcher searcher;
39
private IndexReader reader;
40
private Directory dir;
43
public void setUp() throws Exception {
46
RandomIndexWriter writer = new RandomIndexWriter(random, dir,
47
newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.KEYWORD, false))
48
.setMaxBufferedDocs(_TestUtil.nextInt(random, 50, 1000)));
50
Document doc = new Document();
51
Field bogus1 = newField("bogus", "", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS);
52
Field field = newField("field", "", Field.Store.NO, Field.Index.NOT_ANALYZED);
53
Field bogus2 = newField("zbogus", "", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS);
58
int num = atLeast(1000);
60
for (int i = 0; i < num; i++) {
61
field.setValue(_TestUtil.randomUnicodeString(random, 10));
62
bogus1.setValue(_TestUtil.randomUnicodeString(random, 10));
63
bogus2.setValue(_TestUtil.randomUnicodeString(random, 10));
64
writer.addDocument(doc);
66
reader = writer.getReader();
67
searcher = newSearcher(reader);
72
public void tearDown() throws Exception {
79
/** a stupid prefix query that just blasts thru the terms */
80
private class DumbPrefixQuery extends MultiTermQuery {
81
private final Term prefix;
83
DumbPrefixQuery(Term term) {
89
protected FilteredTermEnum getEnum(IndexReader reader) throws IOException {
90
return new SimplePrefixTermEnum(reader, prefix);
93
private class SimplePrefixTermEnum extends FilteredTermEnum {
94
private final Term prefix;
95
private boolean endEnum;
97
private SimplePrefixTermEnum(IndexReader reader, Term prefix) throws IOException {
99
setEnum(reader.terms(new Term(prefix.field(), "")));
103
protected boolean termCompare(Term term) {
104
if (term.field() == prefix.field()) {
105
return term.text().startsWith(prefix.text());
113
public float difference() {
118
protected boolean endEnum() {
124
public String toString(String field) {
125
return field.toString() + ":" + prefix.toString();
129
/** test a bunch of random prefixes */
130
public void testPrefixes() throws Exception {
131
int num = atLeast(100);
132
for (int i = 0; i < num; i++)
133
assertSame(_TestUtil.randomUnicodeString(random, 5));
136
/** check that the # of hits is the same as from a very
137
* simple prefixquery implementation.
139
private void assertSame(String prefix) throws IOException {
140
PrefixQuery smart = new PrefixQuery(new Term("field", prefix));
141
DumbPrefixQuery dumb = new DumbPrefixQuery(new Term("field", prefix));
143
TopDocs smartDocs = searcher.search(smart, 25);
144
TopDocs dumbDocs = searcher.search(dumb, 25);
145
CheckHits.checkEqual(smart, smartDocs.scoreDocs, dumbDocs.scoreDocs);