lucene-PhraseQuery通过短语查询

通过查找域中的项都相隔一定的距离。两个项的位置最大相隔距离为slop。距离是指项要按顺序组成给定的短语,所需要移动位置的次数,不是指间距。

public class PhraseQueryTest extends TestCase{

privateIndexSearcher searcher;

protectedvoid setUp() throws IOException{

RAMDirectory directory=new RAMDirectory();

IndexWriter writer=new IndexWriter(directory,newWhitespaceAnalyzer(),true);

Document doc=new Document();

doc.add(Field.Text("field","the quick brown fox jumped over thelazy dog"));

writer.addDocument(doc);

writer.close();

searcher=new IndexSearcher(directory);

}

privateboolean matched(String[] phrase,int slop) throwsIOException{

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);

}

}

String[] phrase=nw String[]{"fox","quick"}

matched(phrase,3) 可以成功匹配,将fox向右移动按顺序移动3位

项之间距离越小的匹配具有的权重越大。

你可能感兴趣的:(Lucene)