phrasequerytest.java

来自「Lucene in Action 中文版代码下载」· Java 代码 · 共 77 行

JAVA
77
字号
package lia.searching;import junit.framework.TestCase;import org.apache.lucene.analysis.WhitespaceAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.search.Hits;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.PhraseQuery;import org.apache.lucene.store.RAMDirectory;import java.io.IOException;public class PhraseQueryTest extends TestCase {  private IndexSearcher searcher;  protected void setUp() throws IOException {    // set up sample document    RAMDirectory directory = new RAMDirectory();    IndexWriter writer = new IndexWriter(directory,        new WhitespaceAnalyzer(), true);    Document doc = new Document();    doc.add(Field.Text("field",              "the quick brown fox jumped over the lazy dog"));    writer.addDocument(doc);    writer.close();    searcher = new IndexSearcher(directory);  }  private boolean matched(String[] phrase, int slop)      throws IOException {    PhraseQuery query = new PhraseQuery();    query.setSlop(slop);    for (int i=0; i < phrase.length; i++) {      query.add(new Term("field", phrase[i]));    }    Hits hits = searcher.search(query);    return hits.length() > 0;  }  public void testSlopComparison() throws Exception {    String[] phrase = new String[] {"quick", "fox"};    assertFalse("exact phrase not found", matched(phrase, 0));    assertTrue("close enough", matched(phrase, 1));  }  public void testReverse() throws Exception {    String[] phrase = new String[] {"fox", "quick"};    assertFalse("hop flop", matched(phrase, 2));    assertTrue("hop hop slop", matched(phrase, 3));  }  public void testMultiple() throws Exception {    assertFalse("not close enough",        matched(new String[] {"quick", "jumped", "lazy"}, 3));    assertTrue("just enough",        matched(new String[] {"quick", "jumped", "lazy"}, 4));    assertFalse("almost but not quite",        matched(new String[] {"lazy", "jumped", "quick"}, 7));    assertTrue("bingo",        matched(new String[] {"lazy", "jumped", "quick"}, 8));  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?