Lucene 搜索方法(模糊搜索)

LUCENE提供了一种模糊搜索的机制。

代码:

public static void main(String[] args) {
  try {
   IndexSearcher search = new IndexSearcher("d://demo");
   
   Term term1 = new Term("bookname","work");
   FuzzyQuery query = new FuzzyQuery(term1,0.1f,1);
   Hits hits = search.search(query);
   for(int i=0;i<hits.length();i++)
   {
    System.out.println(hits.doc(i));
   }
  } catch (Exception e) {
   e.printStackTrace();
  }

 }

 

结果;

Document<stored/uncompressed,indexed,tokenized<bookname:work>>
Document<stored/uncompressed,indexed,tokenized<bookname:word>>
Document<stored/uncompressed,indexed,tokenized<bookname:world>>

 

注释:首先构建了一个TERM,并将其作为FuzzyQuery的构造函数,对于FuzzyQuery有两个构造函数,

public FuzzyQuery(Term term,float minimumSimilarity) throws IllegalArgumentException

public FuzzyQuery(Term term,float minimumSimilarity,int prefixLength) throws IllegalArgumentException

 

minimumSimilarity表示是最小相似度,可以通过指定一个相似度来决定模糊匹配的严格程度。默认为0.5,当这个值越小,通过模糊查找出的文档的匹配程度就越低,文档的数量也就越多;当这个值越大,说明要匹配程度更大,匹配的文档数也就越少,当相似度设置为1,那么就退化为TermQuery查询,所以当这个值>=1或<0会抛出IllegalArgumentException异常。

另外一个参数prefixLength表示在进行模糊匹配的时候,要有多少个前缀字母必须完全匹配。例如当该值设置为“1”,则表示所有此条只有第一个字母与检索关键字相符时,才会被集合选中。

 

以上程序设置相似度为0.1f,并且匹配第一个字母相符的检索结果。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值