~ubuntu-branches/ubuntu/trusty/pylucene/trusty

« back to all changes in this revision

Viewing changes to lucene-java-3.5.0/lucene/backwards/src/test/org/apache/lucene/search/TestWildcardRandom.java

  • Committer: Package Import Robot
  • Author(s): Dmitry Nezhevenko
  • Date: 2012-04-23 16:43:55 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120423164355-grqtepnwtecdjfk2
Tags: 3.5.0-1
* New maintainer (closes: 670179)
* New upstream release
* Switch to dpkg-source 3.0 (quilt) format
* Switch to machine-readable debian/copyright
* Bump debian/compat to 8, drop debian/pycompat
* Switch from cdbs to dh
* Add watch file
* Build for all supported versions of python2 (closes: 581198, 632240)
* Rename binary package to python-lucene (closes: 581197)
* Add -dbg package

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.text.DecimalFormat;
 
21
import java.text.DecimalFormatSymbols;
 
22
import java.text.NumberFormat;
 
23
import java.util.Locale;
 
24
 
 
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;
 
34
 
 
35
/**
 
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.
 
39
 */
 
40
public class TestWildcardRandom extends LuceneTestCase {
 
41
  private IndexSearcher searcher;
 
42
  private IndexReader reader;
 
43
  private Directory dir;
 
44
  
 
45
  @Override
 
46
  public void setUp() throws Exception {
 
47
    super.setUp();
 
48
    dir = newDirectory();
 
49
    RandomIndexWriter writer = new RandomIndexWriter(random, dir,
 
50
        newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
 
51
        .setMaxBufferedDocs(_TestUtil.nextInt(random, 50, 1000)));
 
52
    
 
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);
 
57
    doc.add(field);
 
58
    doc.add(bogus1);
 
59
    doc.add(bogus2);
 
60
    
 
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);
 
67
    }
 
68
    
 
69
    reader = writer.getReader();
 
70
    searcher = newSearcher(reader);
 
71
    writer.close();
 
72
  }
 
73
  
 
74
  private char N() {
 
75
    return (char) (0x30 + random.nextInt(10));
 
76
  }
 
77
  
 
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)) {
 
82
        case 'N':
 
83
          sb.append(N());
 
84
          break;
 
85
        default:
 
86
          sb.append(wildcardPattern.charAt(i));
 
87
      }
 
88
    }
 
89
    return sb.toString();
 
90
  }
 
91
  
 
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);
 
97
  }
 
98
 
 
99
  @Override
 
100
  public void tearDown() throws Exception {
 
101
    searcher.close();
 
102
    reader.close();
 
103
    dir.close();
 
104
    super.tearDown();
 
105
  }
 
106
  
 
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);
 
114
    }
 
115
    
 
116
    for (int i = 0; i < num; i++) {
 
117
      assertPatternHits("??N", 100);
 
118
      assertPatternHits("N??", 100);
 
119
      assertPatternHits("???", 1000);
 
120
      
 
121
      assertPatternHits("NN*", 10);
 
122
      assertPatternHits("N*", 100);
 
123
      assertPatternHits("*", 1000);
 
124
      
 
125
      assertPatternHits("*NN", 10);
 
126
      assertPatternHits("*N", 100);
 
127
      
 
128
      assertPatternHits("N*N", 10);
 
129
      
 
130
      // combo of ? and * operators
 
131
      assertPatternHits("?N*", 100);
 
132
      assertPatternHits("N?*", 100);
 
133
      
 
134
      assertPatternHits("*N?", 100);
 
135
      assertPatternHits("*??", 1000);
 
136
      assertPatternHits("*?N", 100);
 
137
    }
 
138
  }
 
139
}