Lucene--FuzzyQuery与WildCardQuery(通配符)

Lucene--FuzzyQuery与WildCardQuery(通配符) 博客分类: java 搜索引擎,爬虫  

FuzzyQuery:

创建索引:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
IndexWriter writer =  new  IndexWriter(path,  new  StandardAnalyzer(),  false ); 
writer.setUseCompoundFile( false );
  
Document doc1 =  new  Document(); 
Document doc2 =  new  Document(); 
Document doc3 =  new  Document(); 
Document doc4 =  new  Document(); 
Document doc5 =  new  Document(); 
Document doc6 =  new  Document();
  
Field f1 =  new  Field( "content" "word" , Field.Store.YES, 
Field.Index.TOKENIZED); 
Field f2 =  new  Field( "content" "work" , Field.Store.YES, 
Field.Index.TOKENIZED); 
Field f3 =  new  Field( "content" "seed" , Field.Store.YES, 
Field.Index.TOKENIZED); 
Field f4 =  new  Field( "content" "sword" , Field.Store.YES, 
Field.Index.TOKENIZED); 
Field f5 =  new  Field( "content" "world" , Field.Store.YES, 
Field.Index.TOKENIZED); 
Field f6 =  new  Field( "content" "ford" , Field.Store.YES, 
Field.Index.TOKENIZED);
  
doc1.add(f1); 
doc2.add(f2); 
doc3.add(f3); 
doc4.add(f4); 
doc5.add(f5); 
doc6.add(f6);
  
writer.addDocument(doc1); 
writer.addDocument(doc2); 
writer.addDocument(doc3); 
writer.addDocument(doc4); 
writer.addDocument(doc5); 
writer.addDocument(doc6);
  
writer.close();

 

 

注:IndexWriter中的create的变量值一般设为true

搜索:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
IndexSearcher searcher =  new  IndexSearcher(path); 
//构建一个Term,然后对其进行模糊查找 
Term t =  new  Term( "content" "work" ); 
FuzzyQuery query =  new  FuzzyQuery(t); 
//FuzzyQuery还有两个构造函数,来限制模糊匹配的程度 
// 在FuzzyQuery中,默认的匹配度是0.5,当这个值越小时,通过模糊查找出的文档的匹配程度就 
// 越低,查出的文档量就越多,反之亦然 
FuzzyQuery query1 =  new  FuzzyQuery(t,  0 .1f); 
FuzzyQuery query2 =  new  FuzzyQuery(t,  0 .1f,  1 ); 
Hits hits = searcher.search(query2); 
for  ( int  i =  0 ; i  < hits.length(); i++) { 
     System.out.println(hits.doc(i)); 
searcher.close();

 

模糊搜索的三种构造函数,具体讲一下参数的用法(以第三个为例);

第一个参数当然是词条对象,第二个参数指的是levenshtein算法的最小相似度,第三个参数指的是要有多少个前缀字母完全匹配:

 

WildCardQuery:

通配符就更简单了,只要知道“*”表示0到多个字符,而使用“?”表示一个字符就行了:

?
1
2
3
4
5
6
7
8
IndexSearcher searcher= new  IndexSearcher(path);
Term t1= new  Term( "content" , "?o*" );
WildcardQuery query= new  WildcardQuery(t1);
Hits hits=searcher.search(query);
for ( int  i= 0 ;i<hits.length();i++)
{
      System.out.println(hits.doc(i));
}

 

 

That“s all!

 

http://my.oschina.net/MrMichael/blog/338925

转载于:https://my.oschina.net/xiaominmin/blog/1597428

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值