add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / search / TestPrefixRandom.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
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;
32
33 /**
34  * Create an index with random unicode terms
35  * Generates random prefix queries, and validates against a simple impl.
36  */
37 public class TestPrefixRandom extends LuceneTestCase {
38   private IndexSearcher searcher;
39   private IndexReader reader;
40   private Directory dir;
41   
42   @Override
43   public void setUp() throws Exception {
44     super.setUp();
45     dir = newDirectory();
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)));
49     
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);
54     doc.add(field);
55     doc.add(bogus1);
56     doc.add(bogus2);
57     
58     int num = atLeast(2000);
59
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);
65     }
66     reader = writer.getReader();
67     searcher = newSearcher(reader);
68     writer.close();
69   }
70
71   @Override
72   public void tearDown() throws Exception {
73     reader.close();
74     searcher.close();
75     dir.close();
76     super.tearDown();
77   }
78   
79   /** a stupid prefix query that just blasts thru the terms */
80   private class DumbPrefixQuery extends MultiTermQuery {
81     private final Term prefix;
82     
83     DumbPrefixQuery(Term term) {
84       super();
85       prefix = term;
86     }
87     
88     @Override
89     protected FilteredTermEnum getEnum(IndexReader reader) throws IOException {
90       return new SimplePrefixTermEnum(reader, prefix);
91     }
92
93     private class SimplePrefixTermEnum extends FilteredTermEnum {
94       private final Term prefix;
95       private boolean endEnum;
96
97       private SimplePrefixTermEnum(IndexReader reader, Term prefix) throws IOException {
98         this.prefix = prefix;
99         setEnum(reader.terms(new Term(prefix.field(), "")));
100       }
101
102       @Override
103       protected boolean termCompare(Term term) {
104         if (term.field() == prefix.field()) {
105           return term.text().startsWith(prefix.text());
106         } else {
107           endEnum = true;
108           return false;
109         }
110       }
111
112       @Override
113       public float difference() {
114         return 1.0F;
115       }
116
117       @Override
118       protected boolean endEnum() {
119         return endEnum;
120       }
121     }
122
123     @Override
124     public String toString(String field) {
125       return field.toString() + ":" + prefix.toString();
126     }
127   }
128   
129   /** test a bunch of random prefixes */
130   public void testPrefixes() throws Exception {
131       int num = atLeast(1000);
132       for (int i = 0; i < num; i++)
133         assertSame(_TestUtil.randomUnicodeString(random, 5));
134   }
135   
136   /** check that the # of hits is the same as from a very
137    * simple prefixquery implementation.
138    */
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));
142     
143     TopDocs smartDocs = searcher.search(smart, 25);
144     TopDocs dumbDocs = searcher.search(dumb, 25);
145     CheckHits.checkEqual(smart, smartDocs.scoreDocs, dumbDocs.scoreDocs);
146   }
147 }