pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / search / spans / TestSpanMultiTermQueryWrapper.java
1 package org.apache.lucene.search.spans;
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.document.Document;
21 import org.apache.lucene.document.Field;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.index.RandomIndexWriter;
24 import org.apache.lucene.index.Term;
25 import org.apache.lucene.search.FuzzyQuery;
26 import org.apache.lucene.search.IndexSearcher;
27 import org.apache.lucene.search.Searcher;
28 import org.apache.lucene.search.WildcardQuery;
29 import org.apache.lucene.store.Directory;
30 import org.apache.lucene.util.LuceneTestCase;
31
32 /**
33  * Tests for {@link SpanMultiTermQueryWrapper}, wrapping a few MultiTermQueries.
34  */
35 public class TestSpanMultiTermQueryWrapper extends LuceneTestCase {
36   private Directory directory;
37   private IndexReader reader;
38   private Searcher searcher;
39   
40   @Override
41   public void setUp() throws Exception {
42     super.setUp();
43     directory = newDirectory();
44     RandomIndexWriter iw = new RandomIndexWriter(random, directory);
45     Document doc = new Document();
46     Field field = newField("field", "", Field.Store.NO, Field.Index.ANALYZED);
47     doc.add(field);
48     
49     field.setValue("quick brown fox");
50     iw.addDocument(doc);
51     field.setValue("jumps over lazy broun dog");
52     iw.addDocument(doc);
53     field.setValue("jumps over extremely very lazy broxn dog");
54     iw.addDocument(doc);
55     reader = iw.getReader();
56     iw.close();
57     searcher = newSearcher(reader);
58   }
59   
60   @Override
61   public void tearDown() throws Exception {
62     searcher.close();
63     reader.close();
64     directory.close();
65     super.tearDown();
66   }
67   
68   public void testWildcard() throws Exception {
69     WildcardQuery wq = new WildcardQuery(new Term("field", "bro?n"));
70     SpanQuery swq = new SpanMultiTermQueryWrapper<WildcardQuery>(wq);
71     // will only match quick brown fox
72     SpanFirstQuery sfq = new SpanFirstQuery(swq, 2);
73     assertEquals(1, searcher.search(sfq, 10).totalHits);
74   }
75   
76   public void testPrefix() throws Exception {
77     WildcardQuery wq = new WildcardQuery(new Term("field", "extrem*"));
78     SpanQuery swq = new SpanMultiTermQueryWrapper<WildcardQuery>(wq);
79     // will only match "jumps over extremely very lazy broxn dog"
80     SpanFirstQuery sfq = new SpanFirstQuery(swq, 3);
81     assertEquals(1, searcher.search(sfq, 10).totalHits);
82   }
83   
84   public void testFuzzy() throws Exception {
85     FuzzyQuery fq = new FuzzyQuery(new Term("field", "broan"));
86     SpanQuery sfq = new SpanMultiTermQueryWrapper<FuzzyQuery>(fq);
87     // will not match quick brown fox
88     SpanPositionRangeQuery sprq = new SpanPositionRangeQuery(sfq, 3, 6);
89     assertEquals(2, searcher.search(sprq, 10).totalHits);
90   }
91   
92   public void testFuzzy2() throws Exception {
93     // maximum of 1 term expansion
94     FuzzyQuery fq = new FuzzyQuery(new Term("field", "broan"), 0.5f, 0, 1);
95     SpanQuery sfq = new SpanMultiTermQueryWrapper<FuzzyQuery>(fq);
96     // will only match jumps over lazy broun dog
97     SpanPositionRangeQuery sprq = new SpanPositionRangeQuery(sfq, 0, 100);
98     assertEquals(1, searcher.search(sprq, 10).totalHits);
99   }
100 }