Lucene--FuzzyQuery与WildCardQuery(通配符)
生活随笔
收集整理的這篇文章主要介紹了
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(通配符)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JVM性能调优监控工具专题二:Visua
- 下一篇: hg 全局密码配置。