You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2012/05/07 01:08:13 UTC

svn commit: r1334819 [2/2] - in /lucene/dev/trunk/lucene: core/src/java/org/apache/lucene/search/ core/src/test/org/apache/lucene/search/ core/src/test/org/apache/lucene/search/spans/ highlighter/src/test/org/apache/lucene/search/highlight/ queryparser...

Added: lucene/dev/trunk/lucene/sandbox/src/test/org/apache/lucene/sandbox/queries/TestSlowFuzzyQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/sandbox/src/test/org/apache/lucene/sandbox/queries/TestSlowFuzzyQuery.java?rev=1334819&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/sandbox/src/test/org/apache/lucene/sandbox/queries/TestSlowFuzzyQuery.java (added)
+++ lucene/dev/trunk/lucene/sandbox/src/test/org/apache/lucene/sandbox/queries/TestSlowFuzzyQuery.java Sun May  6 23:08:12 2012
@@ -0,0 +1,468 @@
+package org.apache.lucene.sandbox.queries;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.util.List;
+import java.util.Arrays;
+import java.io.IOException;
+
+import org.apache.lucene.analysis.MockAnalyzer;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.TextField;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.MultiReader;
+import org.apache.lucene.index.RandomIndexWriter;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.MultiTermQuery;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.ScoreDoc;
+import org.apache.lucene.search.TopDocs;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.LuceneTestCase;
+
+/**
+ * Tests {@link SlowFuzzyQuery}.
+ *
+ */
+public class TestSlowFuzzyQuery extends LuceneTestCase {
+
+  public void testFuzziness() throws Exception {
+    Directory directory = newDirectory();
+    RandomIndexWriter writer = new RandomIndexWriter(random(), directory);
+    addDoc("aaaaa", writer);
+    addDoc("aaaab", writer);
+    addDoc("aaabb", writer);
+    addDoc("aabbb", writer);
+    addDoc("abbbb", writer);
+    addDoc("bbbbb", writer);
+    addDoc("ddddd", writer);
+
+    IndexReader reader = writer.getReader();
+    IndexSearcher searcher = newSearcher(reader);
+    writer.close();
+
+    SlowFuzzyQuery query = new SlowFuzzyQuery(new Term("field", "aaaaa"), SlowFuzzyQuery.defaultMinSimilarity, 0);   
+    ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(3, hits.length);
+    
+    // same with prefix
+    query = new SlowFuzzyQuery(new Term("field", "aaaaa"), SlowFuzzyQuery.defaultMinSimilarity, 1);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(3, hits.length);
+    query = new SlowFuzzyQuery(new Term("field", "aaaaa"), SlowFuzzyQuery.defaultMinSimilarity, 2);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(3, hits.length);
+    query = new SlowFuzzyQuery(new Term("field", "aaaaa"), SlowFuzzyQuery.defaultMinSimilarity, 3);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(3, hits.length);
+    query = new SlowFuzzyQuery(new Term("field", "aaaaa"), SlowFuzzyQuery.defaultMinSimilarity, 4);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(2, hits.length);
+    query = new SlowFuzzyQuery(new Term("field", "aaaaa"), SlowFuzzyQuery.defaultMinSimilarity, 5);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(1, hits.length);
+    query = new SlowFuzzyQuery(new Term("field", "aaaaa"), SlowFuzzyQuery.defaultMinSimilarity, 6);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(1, hits.length);
+    
+    // test scoring
+    query = new SlowFuzzyQuery(new Term("field", "bbbbb"), SlowFuzzyQuery.defaultMinSimilarity, 0);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals("3 documents should match", 3, hits.length);
+    List<String> order = Arrays.asList("bbbbb","abbbb","aabbb");
+    for (int i = 0; i < hits.length; i++) {
+      final String term = searcher.doc(hits[i].doc).get("field");
+      //System.out.println(hits[i].score);
+      assertEquals(order.get(i), term);
+    }
+
+    // test pq size by supplying maxExpansions=2
+    // This query would normally return 3 documents, because 3 terms match (see above):
+    query = new SlowFuzzyQuery(new Term("field", "bbbbb"), SlowFuzzyQuery.defaultMinSimilarity, 0, 2); 
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals("only 2 documents should match", 2, hits.length);
+    order = Arrays.asList("bbbbb","abbbb");
+    for (int i = 0; i < hits.length; i++) {
+      final String term = searcher.doc(hits[i].doc).get("field");
+      //System.out.println(hits[i].score);
+      assertEquals(order.get(i), term);
+    }
+
+    // not similar enough:
+    query = new SlowFuzzyQuery(new Term("field", "xxxxx"), SlowFuzzyQuery.defaultMinSimilarity, 0);  	
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(0, hits.length);
+    query = new SlowFuzzyQuery(new Term("field", "aaccc"), SlowFuzzyQuery.defaultMinSimilarity, 0);   // edit distance to "aaaaa" = 3
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(0, hits.length);
+
+    // query identical to a word in the index:
+    query = new SlowFuzzyQuery(new Term("field", "aaaaa"), SlowFuzzyQuery.defaultMinSimilarity, 0);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(3, hits.length);
+    assertEquals(searcher.doc(hits[0].doc).get("field"), ("aaaaa"));
+    // default allows for up to two edits:
+    assertEquals(searcher.doc(hits[1].doc).get("field"), ("aaaab"));
+    assertEquals(searcher.doc(hits[2].doc).get("field"), ("aaabb"));
+
+    // query similar to a word in the index:
+    query = new SlowFuzzyQuery(new Term("field", "aaaac"), SlowFuzzyQuery.defaultMinSimilarity, 0);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(3, hits.length);
+    assertEquals(searcher.doc(hits[0].doc).get("field"), ("aaaaa"));
+    assertEquals(searcher.doc(hits[1].doc).get("field"), ("aaaab"));
+    assertEquals(searcher.doc(hits[2].doc).get("field"), ("aaabb"));
+    
+    // now with prefix
+    query = new SlowFuzzyQuery(new Term("field", "aaaac"), SlowFuzzyQuery.defaultMinSimilarity, 1);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(3, hits.length);
+    assertEquals(searcher.doc(hits[0].doc).get("field"), ("aaaaa"));
+    assertEquals(searcher.doc(hits[1].doc).get("field"), ("aaaab"));
+    assertEquals(searcher.doc(hits[2].doc).get("field"), ("aaabb"));
+    query = new SlowFuzzyQuery(new Term("field", "aaaac"), SlowFuzzyQuery.defaultMinSimilarity, 2);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(3, hits.length);
+    assertEquals(searcher.doc(hits[0].doc).get("field"), ("aaaaa"));
+    assertEquals(searcher.doc(hits[1].doc).get("field"), ("aaaab"));
+    assertEquals(searcher.doc(hits[2].doc).get("field"), ("aaabb"));
+    query = new SlowFuzzyQuery(new Term("field", "aaaac"), SlowFuzzyQuery.defaultMinSimilarity, 3);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(3, hits.length);
+    assertEquals(searcher.doc(hits[0].doc).get("field"), ("aaaaa"));
+    assertEquals(searcher.doc(hits[1].doc).get("field"), ("aaaab"));
+    assertEquals(searcher.doc(hits[2].doc).get("field"), ("aaabb"));
+    query = new SlowFuzzyQuery(new Term("field", "aaaac"), SlowFuzzyQuery.defaultMinSimilarity, 4);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(2, hits.length);
+    assertEquals(searcher.doc(hits[0].doc).get("field"), ("aaaaa"));
+    assertEquals(searcher.doc(hits[1].doc).get("field"), ("aaaab"));
+    query = new SlowFuzzyQuery(new Term("field", "aaaac"), SlowFuzzyQuery.defaultMinSimilarity, 5);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(0, hits.length);
+    
+
+    query = new SlowFuzzyQuery(new Term("field", "ddddX"), SlowFuzzyQuery.defaultMinSimilarity, 0);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(1, hits.length);
+    assertEquals(searcher.doc(hits[0].doc).get("field"), ("ddddd"));
+    
+    // now with prefix
+    query = new SlowFuzzyQuery(new Term("field", "ddddX"), SlowFuzzyQuery.defaultMinSimilarity, 1);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(1, hits.length);
+    assertEquals(searcher.doc(hits[0].doc).get("field"), ("ddddd"));
+    query = new SlowFuzzyQuery(new Term("field", "ddddX"), SlowFuzzyQuery.defaultMinSimilarity, 2);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(1, hits.length);
+    assertEquals(searcher.doc(hits[0].doc).get("field"), ("ddddd"));
+    query = new SlowFuzzyQuery(new Term("field", "ddddX"), SlowFuzzyQuery.defaultMinSimilarity, 3);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(1, hits.length);
+    assertEquals(searcher.doc(hits[0].doc).get("field"), ("ddddd"));
+    query = new SlowFuzzyQuery(new Term("field", "ddddX"), SlowFuzzyQuery.defaultMinSimilarity, 4);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(1, hits.length);
+    assertEquals(searcher.doc(hits[0].doc).get("field"), ("ddddd"));
+    query = new SlowFuzzyQuery(new Term("field", "ddddX"), SlowFuzzyQuery.defaultMinSimilarity, 5);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(0, hits.length);
+    
+
+    // different field = no match:
+    query = new SlowFuzzyQuery(new Term("anotherfield", "ddddX"), SlowFuzzyQuery.defaultMinSimilarity, 0);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(0, hits.length);
+
+    reader.close();
+    directory.close();
+  }
+
+  public void testFuzzinessLong() throws Exception {
+    Directory directory = newDirectory();
+    RandomIndexWriter writer = new RandomIndexWriter(random(), directory);
+    addDoc("aaaaaaa", writer);
+    addDoc("segment", writer);
+
+    IndexReader reader = writer.getReader();
+    IndexSearcher searcher = newSearcher(reader);
+    writer.close();
+
+    SlowFuzzyQuery query;
+    // not similar enough:
+    query = new SlowFuzzyQuery(new Term("field", "xxxxx"), 0.5f, 0);   
+    ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(0, hits.length);
+    // edit distance to "aaaaaaa" = 3, this matches because the string is longer than
+    // in testDefaultFuzziness so a bigger difference is allowed:
+    query = new SlowFuzzyQuery(new Term("field", "aaaaccc"), 0.5f, 0);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(1, hits.length);
+    assertEquals(searcher.doc(hits[0].doc).get("field"), ("aaaaaaa"));
+    
+    // now with prefix
+    query = new SlowFuzzyQuery(new Term("field", "aaaaccc"), 0.5f, 1);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(1, hits.length);
+    assertEquals(searcher.doc(hits[0].doc).get("field"), ("aaaaaaa"));
+    query = new SlowFuzzyQuery(new Term("field", "aaaaccc"), 0.5f, 4);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(1, hits.length);
+    assertEquals(searcher.doc(hits[0].doc).get("field"), ("aaaaaaa"));
+    query = new SlowFuzzyQuery(new Term("field", "aaaaccc"), 0.5f, 5);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(0, hits.length);
+
+    // no match, more than half of the characters is wrong:
+    query = new SlowFuzzyQuery(new Term("field", "aaacccc"), 0.5f, 0);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(0, hits.length);
+    
+    // now with prefix
+    query = new SlowFuzzyQuery(new Term("field", "aaacccc"), 0.5f, 2);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(0, hits.length);
+
+    // "student" and "stellent" are indeed similar to "segment" by default:
+    query = new SlowFuzzyQuery(new Term("field", "student"), 0.5f, 0);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(1, hits.length);
+    query = new SlowFuzzyQuery(new Term("field", "stellent"), 0.5f, 0);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(1, hits.length);
+    
+    // now with prefix
+    query = new SlowFuzzyQuery(new Term("field", "student"), 0.5f, 1);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(1, hits.length);
+    query = new SlowFuzzyQuery(new Term("field", "stellent"), 0.5f, 1);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(1, hits.length);
+    query = new SlowFuzzyQuery(new Term("field", "student"), 0.5f, 2);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(0, hits.length);
+    query = new SlowFuzzyQuery(new Term("field", "stellent"), 0.5f, 2);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(0, hits.length);
+    
+    // "student" doesn't match anymore thanks to increased minimum similarity:
+    query = new SlowFuzzyQuery(new Term("field", "student"), 0.6f, 0);   
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(0, hits.length);
+
+    try {
+      query = new SlowFuzzyQuery(new Term("field", "student"), 1.1f);
+      fail("Expected IllegalArgumentException");
+    } catch (IllegalArgumentException e) {
+      // expecting exception
+    }
+    try {
+      query = new SlowFuzzyQuery(new Term("field", "student"), -0.1f);
+      fail("Expected IllegalArgumentException");
+    } catch (IllegalArgumentException e) {
+      // expecting exception
+    }
+
+    reader.close();
+    directory.close();
+  }
+  
+  /** 
+   * MultiTermQuery provides (via attribute) information about which values
+   * must be competitive to enter the priority queue. 
+   * 
+   * SlowFuzzyQuery optimizes itself around this information, if the attribute
+   * is not implemented correctly, there will be problems!
+   */
+  public void testTieBreaker() throws Exception {
+    Directory directory = newDirectory();
+    RandomIndexWriter writer = new RandomIndexWriter(random(), directory);
+    addDoc("a123456", writer);
+    addDoc("c123456", writer);
+    addDoc("d123456", writer);
+    addDoc("e123456", writer);
+    
+    Directory directory2 = newDirectory();
+    RandomIndexWriter writer2 = new RandomIndexWriter(random(), directory2);
+    addDoc("a123456", writer2);
+    addDoc("b123456", writer2);
+    addDoc("b123456", writer2);
+    addDoc("b123456", writer2);
+    addDoc("c123456", writer2);
+    addDoc("f123456", writer2);
+    
+    IndexReader ir1 = writer.getReader();
+    IndexReader ir2 = writer2.getReader();
+    
+    MultiReader mr = new MultiReader(ir1, ir2);
+    IndexSearcher searcher = newSearcher(mr);
+    SlowFuzzyQuery fq = new SlowFuzzyQuery(new Term("field", "z123456"), 1f, 0, 2);
+    TopDocs docs = searcher.search(fq, 2);
+    assertEquals(5, docs.totalHits); // 5 docs, from the a and b's
+    mr.close();
+    ir1.close();
+    ir2.close();
+    writer.close();
+    writer2.close();
+    directory.close();
+    directory2.close(); 
+  }
+  
+  public void testTokenLengthOpt() throws IOException {
+    Directory directory = newDirectory();
+    RandomIndexWriter writer = new RandomIndexWriter(random(), directory);
+    addDoc("12345678911", writer);
+    addDoc("segment", writer);
+
+    IndexReader reader = writer.getReader();
+    IndexSearcher searcher = newSearcher(reader);
+    writer.close();
+
+    Query query;
+    // term not over 10 chars, so optimization shortcuts
+    query = new SlowFuzzyQuery(new Term("field", "1234569"), 0.9f);
+    ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(0, hits.length);
+
+    // 10 chars, so no optimization
+    query = new SlowFuzzyQuery(new Term("field", "1234567891"), 0.9f);
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(0, hits.length);
+    
+    // over 10 chars, so no optimization
+    query = new SlowFuzzyQuery(new Term("field", "12345678911"), 0.9f);
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(1, hits.length);
+
+    // over 10 chars, no match
+    query = new SlowFuzzyQuery(new Term("field", "sdfsdfsdfsdf"), 0.9f);
+    hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(0, hits.length);
+    
+    reader.close();
+    directory.close();
+  }
+  
+  /** Test the TopTermsBoostOnlyBooleanQueryRewrite rewrite method. */
+  public void testBoostOnlyRewrite() throws Exception {
+    Directory directory = newDirectory();
+    RandomIndexWriter writer = new RandomIndexWriter(random(), directory);
+    addDoc("Lucene", writer);
+    addDoc("Lucene", writer);
+    addDoc("Lucenne", writer);
+
+    IndexReader reader = writer.getReader();
+    IndexSearcher searcher = newSearcher(reader);
+    writer.close();
+    
+    SlowFuzzyQuery query = new SlowFuzzyQuery(new Term("field", "lucene"));
+    query.setRewriteMethod(new MultiTermQuery.TopTermsBoostOnlyBooleanQueryRewrite(50));
+    ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
+    assertEquals(3, hits.length);
+    // normally, 'Lucenne' would be the first result as IDF will skew the score.
+    assertEquals("Lucene", reader.document(hits[0].doc).get("field"));
+    assertEquals("Lucene", reader.document(hits[1].doc).get("field"));
+    assertEquals("Lucenne", reader.document(hits[2].doc).get("field"));
+    reader.close();
+    directory.close();
+  }
+  
+  public void testGiga() throws Exception {
+
+    MockAnalyzer analyzer = new MockAnalyzer(random());
+    Directory index = newDirectory();
+    RandomIndexWriter w = new RandomIndexWriter(random(), index);
+
+    addDoc("Lucene in Action", w);
+    addDoc("Lucene for Dummies", w);
+
+    //addDoc("Giga", w);
+    addDoc("Giga byte", w);
+
+    addDoc("ManagingGigabytesManagingGigabyte", w);
+    addDoc("ManagingGigabytesManagingGigabytes", w);
+
+    addDoc("The Art of Computer Science", w);
+    addDoc("J. K. Rowling", w);
+    addDoc("JK Rowling", w);
+    addDoc("Joanne K Roling", w);
+    addDoc("Bruce Willis", w);
+    addDoc("Willis bruce", w);
+    addDoc("Brute willis", w);
+    addDoc("B. willis", w);
+    IndexReader r = w.getReader();
+    w.close();
+
+    Query q = new SlowFuzzyQuery(new Term("field", "giga"), 0.9f);
+
+    // 3. search
+    IndexSearcher searcher = newSearcher(r);
+    ScoreDoc[] hits = searcher.search(q, 10).scoreDocs;
+    assertEquals(1, hits.length);
+    assertEquals("Giga byte", searcher.doc(hits[0].doc).get("field"));
+    r.close();
+    index.close();
+  }
+  
+  public void testDistanceAsEditsSearching() throws Exception {
+    Directory index = newDirectory();
+    RandomIndexWriter w = new RandomIndexWriter(random(), index);
+    addDoc("foobar", w);
+    addDoc("test", w);
+    addDoc("working", w);
+    IndexReader reader = w.getReader();
+    IndexSearcher searcher = newSearcher(reader);
+    w.close();
+    
+    SlowFuzzyQuery q = new SlowFuzzyQuery(new Term("field", "fouba"), 2);
+    ScoreDoc[] hits = searcher.search(q, 10).scoreDocs;
+    assertEquals(1, hits.length);
+    assertEquals("foobar", searcher.doc(hits[0].doc).get("field"));
+    
+    q = new SlowFuzzyQuery(new Term("field", "foubara"), 2);
+    hits = searcher.search(q, 10).scoreDocs;
+    assertEquals(1, hits.length);
+    assertEquals("foobar", searcher.doc(hits[0].doc).get("field"));
+    
+    q = new SlowFuzzyQuery(new Term("field", "t"), 3);
+    hits = searcher.search(q, 10).scoreDocs;
+    assertEquals(1, hits.length);
+    assertEquals("test", searcher.doc(hits[0].doc).get("field"));
+    
+    q = new SlowFuzzyQuery(new Term("field", "a"), 4f, 0, 50);
+    hits = searcher.search(q, 10).scoreDocs;
+    assertEquals(1, hits.length);
+    assertEquals("test", searcher.doc(hits[0].doc).get("field"));
+    
+    q = new SlowFuzzyQuery(new Term("field", "a"), 6f, 0, 50);
+    hits = searcher.search(q, 10).scoreDocs;
+    assertEquals(2, hits.length);
+    assertEquals("test", searcher.doc(hits[0].doc).get("field"));
+    assertEquals("foobar", searcher.doc(hits[1].doc).get("field"));
+    
+    reader.close();
+    index.close();
+  }
+
+  private void addDoc(String text, RandomIndexWriter writer) throws IOException {
+    Document doc = new Document();
+    doc.add(newField("field", text, TextField.TYPE_STORED));
+    writer.addDocument(doc);
+  }
+}

Added: lucene/dev/trunk/lucene/sandbox/src/test/org/apache/lucene/sandbox/queries/TestSlowFuzzyQuery2.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/sandbox/src/test/org/apache/lucene/sandbox/queries/TestSlowFuzzyQuery2.java?rev=1334819&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/sandbox/src/test/org/apache/lucene/sandbox/queries/TestSlowFuzzyQuery2.java (added)
+++ lucene/dev/trunk/lucene/sandbox/src/test/org/apache/lucene/sandbox/queries/TestSlowFuzzyQuery2.java Sun May  6 23:08:12 2012
@@ -0,0 +1,183 @@
+package org.apache.lucene.sandbox.queries;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import org.apache.lucene.analysis.MockAnalyzer;
+import org.apache.lucene.analysis.MockTokenizer;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.document.TextField;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.RandomIndexWriter;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.MultiTermQuery;
+import org.apache.lucene.search.TopDocs;
+import org.apache.lucene.search.similarities.DefaultSimilarity;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.LuceneTestCase;
+
+/** 
+ * Tests the results of fuzzy against pre-recorded output 
+ * The format of the file is the following:
+ * 
+ * Header Row: # of bits: generate 2^n sequential documents 
+ * with a value of Integer.toBinaryString
+ * 
+ * Entries: an entry is a param spec line, a resultCount line, and
+ * then 'resultCount' results lines. The results lines are in the
+ * expected order.
+ * 
+ * param spec line: a comma-separated list of params to FuzzyQuery
+ *   (query, prefixLen, pqSize, minScore)
+ * query = query text as a number (expand with Integer.toBinaryString)
+ * prefixLen = prefix length
+ * pqSize = priority queue maximum size for TopTermsBoostOnlyBooleanQueryRewrite
+ * minScore = minimum similarity
+ * 
+ * resultCount line: total number of expected hits.
+ * 
+ * results line: comma-separated docID, score pair
+ **/
+public class TestSlowFuzzyQuery2 extends LuceneTestCase {
+  /** epsilon for score comparisons */
+  static final float epsilon = 0.00001f;
+
+  static int[][] mappings = new int[][] {
+    new int[] { 0x40, 0x41 },
+    new int[] { 0x40, 0x0195 },
+    new int[] { 0x40, 0x0906 },
+    new int[] { 0x40, 0x1040F },
+    new int[] { 0x0194, 0x0195 },
+    new int[] { 0x0194, 0x0906 },
+    new int[] { 0x0194, 0x1040F },
+    new int[] { 0x0905, 0x0906 },
+    new int[] { 0x0905, 0x1040F },
+    new int[] { 0x1040E, 0x1040F }
+  };
+  public void testFromTestData() throws Exception {
+    // TODO: randomize!
+    assertFromTestData(mappings[random().nextInt(mappings.length)]);
+  }
+
+  public void assertFromTestData(int codePointTable[]) throws Exception {
+    if (VERBOSE) {
+      System.out.println("TEST: codePointTable=" + codePointTable);
+    }
+    InputStream stream = getClass().getResourceAsStream("fuzzyTestData.txt");
+    BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
+    
+    int bits = Integer.parseInt(reader.readLine());
+    int terms = (int) Math.pow(2, bits);
+    
+    Directory dir = newDirectory();
+    RandomIndexWriter writer = new RandomIndexWriter(random(), dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random(), MockTokenizer.KEYWORD, false)).setMergePolicy(newLogMergePolicy()));
+
+    Document doc = new Document();
+    Field field = newField("field", "", TextField.TYPE_UNSTORED);
+    doc.add(field);
+    
+    for (int i = 0; i < terms; i++) {
+      field.setStringValue(mapInt(codePointTable, i));
+      writer.addDocument(doc);
+    }   
+    
+    IndexReader r = writer.getReader();
+    IndexSearcher searcher = newSearcher(r);
+    if (VERBOSE) {
+      System.out.println("TEST: searcher=" + searcher);
+    }
+    // even though this uses a boost-only rewrite, this test relies upon queryNorm being the default implementation,
+    // otherwise scores are different!
+    searcher.setSimilarity(new DefaultSimilarity());
+    
+    writer.close();
+    String line;
+    while ((line = reader.readLine()) != null) {
+      String params[] = line.split(",");
+      String query = mapInt(codePointTable, Integer.parseInt(params[0]));
+      int prefix = Integer.parseInt(params[1]);
+      int pqSize = Integer.parseInt(params[2]);
+      float minScore = Float.parseFloat(params[3]);
+      SlowFuzzyQuery q = new SlowFuzzyQuery(new Term("field", query), minScore, prefix);
+      q.setRewriteMethod(new MultiTermQuery.TopTermsBoostOnlyBooleanQueryRewrite(pqSize));
+      int expectedResults = Integer.parseInt(reader.readLine());
+      TopDocs docs = searcher.search(q, expectedResults);
+      assertEquals(expectedResults, docs.totalHits);
+      for (int i = 0; i < expectedResults; i++) {
+        String scoreDoc[] = reader.readLine().split(",");
+        assertEquals(Integer.parseInt(scoreDoc[0]), docs.scoreDocs[i].doc);
+        assertEquals(Float.parseFloat(scoreDoc[1]), docs.scoreDocs[i].score, epsilon);
+      }
+    }
+    r.close();
+    dir.close();
+  }
+  
+  /* map bits to unicode codepoints */
+  private static String mapInt(int codePointTable[], int i) {
+    StringBuilder sb = new StringBuilder();
+    String binary = Integer.toBinaryString(i);
+    for (int j = 0; j < binary.length(); j++)
+      sb.appendCodePoint(codePointTable[binary.charAt(j) - '0']);
+    return sb.toString();
+  }
+
+  /* Code to generate test data
+  public static void main(String args[]) throws Exception {
+    int bits = 3;
+    System.out.println(bits);
+    int terms = (int) Math.pow(2, bits);
+    
+    RAMDirectory dir = new RAMDirectory();
+    IndexWriter writer = new IndexWriter(dir, new KeywordAnalyzer(),
+        IndexWriter.MaxFieldLength.UNLIMITED);
+    
+    Document doc = new Document();
+    Field field = newField("field", "", Field.Store.NO, Field.Index.ANALYZED);
+    doc.add(field);
+
+    for (int i = 0; i < terms; i++) {
+      field.setValue(Integer.toBinaryString(i));
+      writer.addDocument(doc);
+    }
+    
+    writer.forceMerge(1);
+    writer.close();
+
+    IndexSearcher searcher = new IndexSearcher(dir);
+    for (int prefix = 0; prefix < bits; prefix++)
+      for (int pqsize = 1; pqsize <= terms; pqsize++)
+        for (float minscore = 0.1F; minscore < 1F; minscore += 0.2F)
+          for (int query = 0; query < terms; query++) {
+            FuzzyQuery q = new FuzzyQuery(
+                new Term("field", Integer.toBinaryString(query)), minscore, prefix);
+            q.setRewriteMethod(new MultiTermQuery.TopTermsBoostOnlyBooleanQueryRewrite(pqsize));
+            System.out.println(query + "," + prefix + "," + pqsize + "," + minscore);
+            TopDocs docs = searcher.search(q, terms);
+            System.out.println(docs.totalHits);
+            for (int i = 0; i < docs.totalHits; i++)
+              System.out.println(docs.scoreDocs[i].doc + "," + docs.scoreDocs[i].score);
+          }
+  }
+  */
+}