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.text.DecimalFormat;
21
import java.text.DecimalFormatSymbols;
22
import java.text.NumberFormat;
23
import java.util.Locale;
25
import org.apache.lucene.analysis.MockAnalyzer;
26
import org.apache.lucene.document.Document;
27
import org.apache.lucene.document.Field;
28
import org.apache.lucene.index.IndexReader;
29
import org.apache.lucene.index.RandomIndexWriter;
30
import org.apache.lucene.index.Term;
31
import org.apache.lucene.store.Directory;
32
import org.apache.lucene.util.LuceneTestCase;
33
import org.apache.lucene.util._TestUtil;
36
* Create an index with terms from 000-999.
37
* Generates random wildcards according to patterns,
38
* and validates the correct number of hits are returned.
40
public class TestWildcardRandom extends LuceneTestCase {
41
private IndexSearcher searcher;
42
private IndexReader reader;
43
private Directory dir;
46
public void setUp() throws Exception {
49
RandomIndexWriter writer = new RandomIndexWriter(random, dir,
50
newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
51
.setMaxBufferedDocs(_TestUtil.nextInt(random, 50, 1000)));
53
Document doc = new Document();
54
Field bogus1 = newField("bogus1", "", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS);
55
Field field = newField("field", "", Field.Store.NO, Field.Index.ANALYZED_NO_NORMS);
56
Field bogus2 = newField("zbogus2", "", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS);
61
NumberFormat df = new DecimalFormat("000", new DecimalFormatSymbols(Locale.ENGLISH));
62
for (int i = 0; i < 1000; i++) {
63
field.setValue(df.format(i));
64
bogus1.setValue(_TestUtil.randomUnicodeString(random, 10));
65
bogus2.setValue(_TestUtil.randomUnicodeString(random, 10));
66
writer.addDocument(doc);
69
reader = writer.getReader();
70
searcher = newSearcher(reader);
75
return (char) (0x30 + random.nextInt(10));
78
private String fillPattern(String wildcardPattern) {
79
StringBuilder sb = new StringBuilder();
80
for (int i = 0; i < wildcardPattern.length(); i++) {
81
switch(wildcardPattern.charAt(i)) {
86
sb.append(wildcardPattern.charAt(i));
92
private void assertPatternHits(String pattern, int numHits) throws Exception {
93
// TODO: run with different rewrites
94
Query wq = new WildcardQuery(new Term("field", fillPattern(pattern)));
95
TopDocs docs = searcher.search(wq, 25);
96
assertEquals("Incorrect hits for pattern: " + pattern, numHits, docs.totalHits);
100
public void tearDown() throws Exception {
107
public void testWildcards() throws Exception {;
108
int num = atLeast(1);
109
for (int i = 0; i < num; i++) {
110
assertPatternHits("NNN", 1);
111
assertPatternHits("?NN", 10);
112
assertPatternHits("N?N", 10);
113
assertPatternHits("NN?", 10);
116
for (int i = 0; i < num; i++) {
117
assertPatternHits("??N", 100);
118
assertPatternHits("N??", 100);
119
assertPatternHits("???", 1000);
121
assertPatternHits("NN*", 10);
122
assertPatternHits("N*", 100);
123
assertPatternHits("*", 1000);
125
assertPatternHits("*NN", 10);
126
assertPatternHits("*N", 100);
128
assertPatternHits("N*N", 10);
130
// combo of ? and * operators
131
assertPatternHits("?N*", 100);
132
assertPatternHits("N?*", 100);
134
assertPatternHits("*N?", 100);
135
assertPatternHits("*??", 1000);
136
assertPatternHits("*?N", 100);