mirror of https://github.com/apache/lucene.git
more phrase query tests
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150055 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
be4dd1b301
commit
8958352e85
|
@ -55,31 +55,130 @@ package org.apache.lucene.search;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.analysis.WhitespaceAnalyzer;
|
||||||
import org.apache.lucene.store.RAMDirectory;
|
|
||||||
import org.apache.lucene.index.IndexWriter;
|
|
||||||
import org.apache.lucene.index.Term;
|
|
||||||
import org.apache.lucene.analysis.*;
|
|
||||||
import org.apache.lucene.document.Document;
|
import org.apache.lucene.document.Document;
|
||||||
import org.apache.lucene.document.Field;
|
import org.apache.lucene.document.Field;
|
||||||
|
import org.apache.lucene.index.IndexWriter;
|
||||||
|
import org.apache.lucene.index.Term;
|
||||||
|
import org.apache.lucene.store.RAMDirectory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests {@link PhraseQuery}.
|
||||||
|
*
|
||||||
|
* @see TestPositionIncrement
|
||||||
|
* @author Erik Hatcher
|
||||||
|
*/
|
||||||
public class TestPhraseQuery extends TestCase {
|
public class TestPhraseQuery extends TestCase {
|
||||||
public void testOrderDoesntMatter() throws Exception {
|
private IndexSearcher searcher;
|
||||||
Directory directory = new RAMDirectory();
|
private PhraseQuery query;
|
||||||
IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
|
|
||||||
Document doc = new Document();
|
|
||||||
doc.add(Field.Text("field", "one two"));
|
|
||||||
writer.addDocument(doc);
|
|
||||||
writer.optimize();
|
|
||||||
writer.close();
|
|
||||||
|
|
||||||
IndexSearcher searcher = new IndexSearcher(directory);
|
public void setUp() throws Exception {
|
||||||
PhraseQuery query = new PhraseQuery();
|
RAMDirectory directory = new RAMDirectory();
|
||||||
query.setSlop(5);
|
IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
|
||||||
query.add(new Term("field", "two"));
|
Document doc = new Document();
|
||||||
query.add(new Term("field", "one"));
|
doc.add(Field.Text("field", "one two three four five"));
|
||||||
Hits hits = searcher.search(query);
|
writer.addDocument(doc);
|
||||||
assertEquals(1, hits.length());
|
writer.close();
|
||||||
searcher.close();
|
|
||||||
}
|
searcher = new IndexSearcher(directory);
|
||||||
|
query = new PhraseQuery();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tearDown() throws Exception {
|
||||||
|
searcher.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testNotCloseEnough() throws Exception {
|
||||||
|
query.setSlop(2);
|
||||||
|
query.add(new Term("field", "one"));
|
||||||
|
query.add(new Term("field", "five"));
|
||||||
|
Hits hits = searcher.search(query);
|
||||||
|
assertEquals(0, hits.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testBarelyCloseEnough() throws Exception {
|
||||||
|
query.setSlop(3);
|
||||||
|
query.add(new Term("field", "one"));
|
||||||
|
query.add(new Term("field", "five"));
|
||||||
|
Hits hits = searcher.search(query);
|
||||||
|
assertEquals(1, hits.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensures slop of 0 works for exact matches, but not reversed
|
||||||
|
*/
|
||||||
|
public void testExact() throws Exception {
|
||||||
|
// slop is zero by default
|
||||||
|
query.add(new Term("field", "four"));
|
||||||
|
query.add(new Term("field", "five"));
|
||||||
|
Hits hits = searcher.search(query);
|
||||||
|
assertEquals("exact match", 1, hits.length());
|
||||||
|
|
||||||
|
query = new PhraseQuery();
|
||||||
|
query.add(new Term("field", "two"));
|
||||||
|
query.add(new Term("field", "one"));
|
||||||
|
hits = searcher.search(query);
|
||||||
|
assertEquals("reverse not exact", 0, hits.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSlop1() throws Exception {
|
||||||
|
// Ensures slop of 1 works with terms in order.
|
||||||
|
query.setSlop(1);
|
||||||
|
query.add(new Term("field", "one"));
|
||||||
|
query.add(new Term("field", "two"));
|
||||||
|
Hits hits = searcher.search(query);
|
||||||
|
assertEquals("in order", 1, hits.length());
|
||||||
|
|
||||||
|
// Ensures slop of 1 does not work for phrases out of order;
|
||||||
|
// must be at least 2.
|
||||||
|
query = new PhraseQuery();
|
||||||
|
query.setSlop(1);
|
||||||
|
query.add(new Term("field", "two"));
|
||||||
|
query.add(new Term("field", "one"));
|
||||||
|
hits = searcher.search(query);
|
||||||
|
assertEquals("reversed, slop not 2 or more", 0, hits.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* As long as slop is at least 2, terms can be reversed
|
||||||
|
*/
|
||||||
|
public void testOrderDoesntMatter() throws Exception {
|
||||||
|
query.setSlop(2); // must be at least two for reverse order match
|
||||||
|
query.add(new Term("field", "two"));
|
||||||
|
query.add(new Term("field", "one"));
|
||||||
|
Hits hits = searcher.search(query);
|
||||||
|
assertEquals("just sloppy enough", 1, hits.length());
|
||||||
|
|
||||||
|
query = new PhraseQuery();
|
||||||
|
query.setSlop(2);
|
||||||
|
query.add(new Term("field", "three"));
|
||||||
|
query.add(new Term("field", "one"));
|
||||||
|
hits = searcher.search(query);
|
||||||
|
assertEquals("not sloppy enough", 0, hits.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* slop is the total number of positional moves allowed
|
||||||
|
* to line up a phrase
|
||||||
|
*/
|
||||||
|
public void testMulipleTerms() throws Exception {
|
||||||
|
query.setSlop(2);
|
||||||
|
query.add(new Term("field", "one"));
|
||||||
|
query.add(new Term("field", "three"));
|
||||||
|
query.add(new Term("field", "five"));
|
||||||
|
Hits hits = searcher.search(query);
|
||||||
|
assertEquals("two total moves", 1, hits.length());
|
||||||
|
|
||||||
|
query = new PhraseQuery();
|
||||||
|
query.setSlop(5); // it takes six moves to match this phrase
|
||||||
|
query.add(new Term("field", "five"));
|
||||||
|
query.add(new Term("field", "three"));
|
||||||
|
query.add(new Term("field", "one"));
|
||||||
|
hits = searcher.search(query);
|
||||||
|
assertEquals("slop of 5 not close enough", 0, hits.length());
|
||||||
|
|
||||||
|
query.setSlop(6);
|
||||||
|
hits = searcher.search(query);
|
||||||
|
assertEquals("slop of 6 just right", 1, hits.length());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue