1
package org.apache.lucene.search.spans;
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.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;
33
* Tests for {@link SpanMultiTermQueryWrapper}, wrapping a few MultiTermQueries.
35
public class TestSpanMultiTermQueryWrapper extends LuceneTestCase {
36
private Directory directory;
37
private IndexReader reader;
38
private Searcher searcher;
41
public void setUp() throws Exception {
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);
49
field.setValue("quick brown fox");
51
field.setValue("jumps over lazy broun dog");
53
field.setValue("jumps over extremely very lazy broxn dog");
55
reader = iw.getReader();
57
searcher = newSearcher(reader);
61
public void tearDown() throws Exception {
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);
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);
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);
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);