KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > spans > TestBasics


1 package org.apache.lucene.search.spans;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import junit.framework.TestCase;
20
21 import java.io.IOException JavaDoc;
22
23 import org.apache.lucene.util.English;
24 import org.apache.lucene.analysis.SimpleAnalyzer;
25 import org.apache.lucene.document.Document;
26 import org.apache.lucene.document.Field;
27 import org.apache.lucene.index.IndexWriter;
28 import org.apache.lucene.index.Term;
29 import org.apache.lucene.store.RAMDirectory;
30
31 import org.apache.lucene.search.*;
32
33 /**
34  * Tests basic search capabilities.
35  *
36  * <p>Uses a collection of 1000 documents, each the english rendition of their
37  * document number. For example, the document numbered 333 has text "three
38  * hundred thirty three".
39  *
40  * <p>Tests are each a single query, and its hits are checked to ensure that
41  * all and only the correct documents are returned, thus providing end-to-end
42  * testing of the indexing and search code.
43  *
44  * @author Doug Cutting
45  */

46 public class TestBasics extends TestCase {
47   private IndexSearcher searcher;
48
49   public void setUp() throws Exception JavaDoc {
50     RAMDirectory directory = new RAMDirectory();
51     IndexWriter writer
52       = new IndexWriter(directory, new SimpleAnalyzer(), true);
53     //writer.infoStream = System.out;
54
for (int i = 0; i < 1000; i++) {
55       Document doc = new Document();
56       doc.add(Field.Text("field", English.intToEnglish(i)));
57       writer.addDocument(doc);
58     }
59
60     writer.close();
61
62     searcher = new IndexSearcher(directory);
63   }
64   
65   public void testTerm() throws Exception JavaDoc {
66     Query query = new TermQuery(new Term("field", "seventy"));
67     checkHits(query, new int[]
68       {70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 170, 171, 172, 173, 174, 175,
69        176, 177, 178, 179, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279,
70        370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 470, 471, 472, 473,
71        474, 475, 476, 477, 478, 479, 570, 571, 572, 573, 574, 575, 576, 577,
72        578, 579, 670, 671, 672, 673, 674, 675, 676, 677, 678, 679, 770, 771,
73        772, 773, 774, 775, 776, 777, 778, 779, 870, 871, 872, 873, 874, 875,
74        876, 877, 878, 879, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979});
75   }
76
77   public void testTerm2() throws Exception JavaDoc {
78     Query query = new TermQuery(new Term("field", "seventish"));
79     checkHits(query, new int[] {});
80   }
81
82   public void testPhrase() throws Exception JavaDoc {
83     PhraseQuery query = new PhraseQuery();
84     query.add(new Term("field", "seventy"));
85     query.add(new Term("field", "seven"));
86     checkHits(query, new int[]
87       {77, 177, 277, 377, 477, 577, 677, 777, 877, 977});
88   }
89
90   public void testPhrase2() throws Exception JavaDoc {
91     PhraseQuery query = new PhraseQuery();
92     query.add(new Term("field", "seventish"));
93     query.add(new Term("field", "sevenon"));
94     checkHits(query, new int[] {});
95   }
96
97   public void testBoolean() throws Exception JavaDoc {
98     BooleanQuery query = new BooleanQuery();
99     query.add(new TermQuery(new Term("field", "seventy")), true, false);
100     query.add(new TermQuery(new Term("field", "seven")), true, false);
101     checkHits(query, new int[]
102       {77, 777, 177, 277, 377, 477, 577, 677, 770, 771, 772, 773, 774, 775,
103        776, 778, 779, 877, 977});
104   }
105
106   public void testBoolean2() throws Exception JavaDoc {
107     BooleanQuery query = new BooleanQuery();
108     query.add(new TermQuery(new Term("field", "sevento")), true, false);
109     query.add(new TermQuery(new Term("field", "sevenly")), true, false);
110     checkHits(query, new int[] {});
111   }
112
113   public void testSpanNearExact() throws Exception JavaDoc {
114     SpanTermQuery term1 = new SpanTermQuery(new Term("field", "seventy"));
115     SpanTermQuery term2 = new SpanTermQuery(new Term("field", "seven"));
116     SpanNearQuery query = new SpanNearQuery(new SpanQuery[] {term1, term2},
117                                             0, true);
118     checkHits(query, new int[]
119       {77, 177, 277, 377, 477, 577, 677, 777, 877, 977});
120
121     assertTrue(searcher.explain(query, 77).getValue() > 0.0f);
122     assertTrue(searcher.explain(query, 977).getValue() > 0.0f);
123   }
124
125   public void testSpanNearUnordered() throws Exception JavaDoc {
126     SpanTermQuery term1 = new SpanTermQuery(new Term("field", "nine"));
127     SpanTermQuery term2 = new SpanTermQuery(new Term("field", "six"));
128     SpanNearQuery query = new SpanNearQuery(new SpanQuery[] {term1, term2},
129                                             4, false);
130
131     checkHits(query, new int[]
132       {609, 629, 639, 649, 659, 669, 679, 689, 699,
133        906, 926, 936, 946, 956, 966, 976, 986, 996});
134   }
135
136   public void testSpanNearOrdered() throws Exception JavaDoc {
137     SpanTermQuery term1 = new SpanTermQuery(new Term("field", "nine"));
138     SpanTermQuery term2 = new SpanTermQuery(new Term("field", "six"));
139     SpanNearQuery query = new SpanNearQuery(new SpanQuery[] {term1, term2},
140                                             4, true);
141     checkHits(query, new int[]
142       {906, 926, 936, 946, 956, 966, 976, 986, 996});
143   }
144
145   public void testSpanNot() throws Exception JavaDoc {
146     SpanTermQuery term1 = new SpanTermQuery(new Term("field", "eight"));
147     SpanTermQuery term2 = new SpanTermQuery(new Term("field", "one"));
148     SpanNearQuery near = new SpanNearQuery(new SpanQuery[] {term1, term2},
149                                            4, true);
150     SpanTermQuery term3 = new SpanTermQuery(new Term("field", "forty"));
151     SpanNotQuery query = new SpanNotQuery(near, term3);
152
153     checkHits(query, new int[]
154       {801, 821, 831, 851, 861, 871, 881, 891});
155
156     assertTrue(searcher.explain(query, 801).getValue() > 0.0f);
157     assertTrue(searcher.explain(query, 891).getValue() > 0.0f);
158   }
159
160   public void testSpanFirst() throws Exception JavaDoc {
161     SpanTermQuery term1 = new SpanTermQuery(new Term("field", "five"));
162     SpanFirstQuery query = new SpanFirstQuery(term1, 1);
163
164     checkHits(query, new int[]
165       {5, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513,
166        514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527,
167        528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541,
168        542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555,
169        556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569,
170        570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 580, 581, 582, 583,
171        584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597,
172        598, 599});
173
174     assertTrue(searcher.explain(query, 5).getValue() > 0.0f);
175     assertTrue(searcher.explain(query, 599).getValue() > 0.0f);
176
177   }
178
179   public void testSpanOr() throws Exception JavaDoc {
180     SpanTermQuery term1 = new SpanTermQuery(new Term("field", "thirty"));
181     SpanTermQuery term2 = new SpanTermQuery(new Term("field", "three"));
182     SpanNearQuery near1 = new SpanNearQuery(new SpanQuery[] {term1, term2},
183                                             0, true);
184     SpanTermQuery term3 = new SpanTermQuery(new Term("field", "forty"));
185     SpanTermQuery term4 = new SpanTermQuery(new Term("field", "seven"));
186     SpanNearQuery near2 = new SpanNearQuery(new SpanQuery[] {term3, term4},
187                                             0, true);
188
189     SpanOrQuery query = new SpanOrQuery(new SpanQuery[] {near1, near2});
190
191     checkHits(query, new int[]
192       {33, 47, 133, 147, 233, 247, 333, 347, 433, 447, 533, 547, 633, 647, 733,
193        747, 833, 847, 933, 947});
194
195     assertTrue(searcher.explain(query, 33).getValue() > 0.0f);
196     assertTrue(searcher.explain(query, 947).getValue() > 0.0f);
197   }
198
199   public void testSpanExactNested() throws Exception JavaDoc {
200     SpanTermQuery term1 = new SpanTermQuery(new Term("field", "three"));
201     SpanTermQuery term2 = new SpanTermQuery(new Term("field", "hundred"));
202     SpanNearQuery near1 = new SpanNearQuery(new SpanQuery[] {term1, term2},
203                                             0, true);
204     SpanTermQuery term3 = new SpanTermQuery(new Term("field", "thirty"));
205     SpanTermQuery term4 = new SpanTermQuery(new Term("field", "three"));
206     SpanNearQuery near2 = new SpanNearQuery(new SpanQuery[] {term3, term4},
207                                             0, true);
208
209     SpanNearQuery query = new SpanNearQuery(new SpanQuery[] {near1, near2},
210                                             0, true);
211
212     checkHits(query, new int[] {333});
213
214     assertTrue(searcher.explain(query, 333).getValue() > 0.0f);
215   }
216
217   public void testSpanNearOr() throws Exception JavaDoc {
218
219     SpanTermQuery t1 = new SpanTermQuery(new Term("field","six"));
220     SpanTermQuery t3 = new SpanTermQuery(new Term("field","seven"));
221     
222     SpanTermQuery t5 = new SpanTermQuery(new Term("field","seven"));
223     SpanTermQuery t6 = new SpanTermQuery(new Term("field","six"));
224
225     SpanOrQuery to1 = new SpanOrQuery(new SpanQuery[] {t1, t3});
226     SpanOrQuery to2 = new SpanOrQuery(new SpanQuery[] {t5, t6});
227     
228     SpanNearQuery query = new SpanNearQuery(new SpanQuery[] {to1, to2},
229                                             10, true);
230
231     checkHits(query, new int[]
232       {606, 607, 626, 627, 636, 637, 646, 647,
233        656, 657, 666, 667, 676, 677, 686, 687, 696, 697,
234        706, 707, 726, 727, 736, 737, 746, 747,
235        756, 757, 766, 767, 776, 777, 786, 787, 796, 797});
236   }
237
238   public void testSpanComplex1() throws Exception JavaDoc {
239       
240     SpanTermQuery t1 = new SpanTermQuery(new Term("field","six"));
241     SpanTermQuery t2 = new SpanTermQuery(new Term("field","hundred"));
242     SpanNearQuery tt1 = new SpanNearQuery(new SpanQuery[] {t1, t2}, 0,true);
243
244     SpanTermQuery t3 = new SpanTermQuery(new Term("field","seven"));
245     SpanTermQuery t4 = new SpanTermQuery(new Term("field","hundred"));
246     SpanNearQuery tt2 = new SpanNearQuery(new SpanQuery[] {t3, t4}, 0,true);
247     
248     SpanTermQuery t5 = new SpanTermQuery(new Term("field","seven"));
249     SpanTermQuery t6 = new SpanTermQuery(new Term("field","six"));
250
251     SpanOrQuery to1 = new SpanOrQuery(new SpanQuery[] {tt1, tt2});
252     SpanOrQuery to2 = new SpanOrQuery(new SpanQuery[] {t5, t6});
253     
254     SpanNearQuery query = new SpanNearQuery(new SpanQuery[] {to1, to2},
255                                             100, true);
256     
257     checkHits(query, new int[]
258       {606, 607, 626, 627, 636, 637, 646, 647,
259        656, 657, 666, 667, 676, 677, 686, 687, 696, 697,
260        706, 707, 726, 727, 736, 737, 746, 747,
261        756, 757, 766, 767, 776, 777, 786, 787, 796, 797});
262   }
263
264
265   private void checkHits(Query query, int[] results) throws IOException JavaDoc {
266     CheckHits.checkHits(query, "field", searcher, results, this);
267   }
268 }
269
Popular Tags