lucene使用PhraseQuery设置slop进行短语查询

所谓PhraseQuery,就是通过短语来检索。

例如现在有一个字符串,“the quick brown fox jumped over the lazy dog”,我们不知道其中的精确的短语,我们仍然可以通过短语“quick”、"fox"来查找文档。slop就是从一个词到另一个词的距离。

下面是一个模仿lucene in action 第二版的一个例子。

package com.cn;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

public class PharserQueryTest {

	public static void main(String []args)throws Exception {
		RAMDirectory directory = new RAMDirectory();
		IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_34, new StandardAnalyzer(Version.LUCENE_34)));
		Document doc = new Document();
		doc.add(new Field("field","the quick brown fox jumped over the lazy dog",Field.Store.YES,Field.Index.ANALYZED));
		indexWriter.addDocument(doc);
		indexWriter.close();
		
		match(directory,new String[]{"quick","jumped"},1);
		match(directory,new String[]{"quick","jumped"},2);
		
		match(directory,new String[]{"jumped","quick"},3);
		match(directory,new String[]{"jumped","quick"},4);
		
		match(directory,new String[]{"quick","jumped","lazy"},3);
		match(directory,new String[]{"quick","jumped","lazy"},4);
		
		match(directory,new String[]{"lazy","jumped","quick"},7);
		match(directory,new String[]{"lazy","jumped","quick"},8);
		
	}

	public static void match(Directory directory,String []pharser,int slop)throws Exception {
		IndexSearcher indexSearcher = new IndexSearcher(directory);
		PhraseQuery query = new PhraseQuery();
		query.setSlop(slop);
		for(String s:pharser){
			query.add(new Term("field",s));
		}
		TopDocs topDocs = indexSearcher.search(query, 10);
		System.out.println(topDocs.totalHits); 
	}
}

下面是运行结果(0:查不到;1:查到):

0
1
0
1
0
1
0
1



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值