欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Lucene--FuzzyQuery与WildCardQuery(通配符)

发布时间:2025/3/19 编程问答 30 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Lucene--FuzzyQuery与WildCardQuery(通配符) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

2019独角兽企业重金招聘Python工程师标准>>>

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

与50位技术专家面对面20年技术见证,附赠技术全景图

总结

以上是生活随笔为你收集整理的Lucene--FuzzyQuery与WildCardQuery(通配符)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。