日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

[轉(zhuǎn)]Lucene 2 教程

系統(tǒng) 2109 0

Lucene是apache組織的一個(gè)用java實(shí)現(xiàn)全文搜索引擎的開(kāi)源項(xiàng)目。 其功能非常的強(qiáng)大,api也很簡(jiǎn)單。總得來(lái)說(shuō)用Lucene來(lái)進(jìn)行建立 和搜索和操作數(shù)據(jù)庫(kù)是差不多的(有點(diǎn)像),Document可以看作是 數(shù)據(jù)庫(kù)的一行記錄,F(xiàn)ield可以看作是數(shù)據(jù)庫(kù)的字段。用lucene實(shí) 現(xiàn)搜索引擎就像用JDBC實(shí)現(xiàn)連接數(shù)據(jù)庫(kù)一樣簡(jiǎn)單。

Lucene2.0,它與以前廣泛應(yīng)用和介紹的Lucene 1.4.3并不兼容。 Lucene2.0的下載地址是 http://apache.justdn.org/lucene/java/


例子一 :

1、在windows系統(tǒng)下的的C盤(pán),建一個(gè)名叫s的文件夾,在該文件夾里面隨便建三個(gè)txt文件,隨便起名啦,就叫"1.txt","2.txt"和"3.txt"啦?
其中1.txt的內(nèi)容如下:

? 中華人民共和國(guó)???
全國(guó)人民???
2006年??

而"2.txt"和"3.txt"的內(nèi)容也可以隨便寫(xiě)幾寫(xiě),這里懶寫(xiě),就復(fù)制一個(gè)和1.txt文件的內(nèi)容一樣吧

2、下載lucene包,放在classpath路徑中?
建立索引:

? package ? ?lighter.iteye.com;???
??
? import ? ?java.io.BufferedReader;???
? import ? ?java.io.File;???
? import ? ?java.io.FileInputStream;???
? import ? ?java.io.IOException;???
? import ? ?java.io.InputStreamReader;???
? import ? ?java.util.Date;???
??
? import ? ?org.apache.lucene.analysis.Analyzer;???
? 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;???
??
? /** ? ??
?*?author?lighter?date?2006-8-7??
?
? */ ?
??
? public ? ? ? class ? ?TextFileIndexer? ? {???
????
? public ? ? ? static ? ? ? void ? ?main(String[]?args)? ? throws ? ?Exception? ? {???
????????
? /* ? ?指明要索引文件夾的位置,這里是C盤(pán)的S文件夾下? ? */ ? ??
????????File?fileDir?
? = ? ? ? new ? ?File( ? " ? c:\\s ? " ? );???
??
????????
? /* ? ?這里放索引文件的位置? ? */ ? ??
????????File?indexDir?
? = ? ? ? new ? ?File( ? " ? c:\\index ? " ? );???
????????Analyzer?luceneAnalyzer?
? = ? ? ? new ? ?StandardAnalyzer();???
????????IndexWriter?indexWriter?
? = ? ? ? new ? ?IndexWriter(indexDir,?luceneAnalyzer,???
????????????????
? true ? );???
????????File[]?textFiles?
? = ? ?fileDir.listFiles();???
????????
? long ? ?startTime? ? = ? ? ? new ? ?Date().getTime();???
???????????
????????
? // ? 增加document到索引去??? ?
?
???????? ? for ? ?( ? int ? ?i? ? = ? ? ? 0 ? ;?i? ? < ? ?textFiles.length;?i ? ++ ? )? ? {???
????????????
? if ? ?(textFiles[i].isFile()???
????????????????????
? && ? ?textFiles[i].getName().endsWith( ? " ? .txt ? " ? ))? ? {???
????????????????System.out.println(
? " ? File? ? " ? ? ? + ? ?textFiles[i].getCanonicalPath()???
????????????????????????
? + ? ? ? " ? 正在被索引 . ? " ? );???
????????????????String?temp?
? = ? ?FileReaderAll(textFiles[i].getCanonicalPath(),???
????????????????????????
? " ? GBK ? " ? );???
????????????????System.out.println(temp);???
????????????????Document?document?
? = ? ? ? new ? ?Document();???
????????????????Field?FieldPath?
? = ? ? ? new ? ?Field( ? " ? path ? " ? ,?textFiles[i].getPath(),???
????????????????????????Field.Store.YES,?Field.Index.NO);???
????????????????Field?FieldBody?
? = ? ? ? new ? ?Field( ? " ? body ? " ? ,?temp,?Field.Store.YES,???
????????????????????????Field.Index.TOKENIZED,???
????????????????????????Field.TermVector.WITH_POSITIONS_OFFSETS);???
????????????????document.add(FieldPath);???
????????????????document.add(FieldBody);???
????????????????indexWriter.addDocument(document);???
????????????}
?
???
????????}
?
???
????????
? // ? optimize()方法是對(duì)索引進(jìn)行優(yōu)化??? ?
?
????????indexWriter.optimize();???
????????indexWriter.close();???
???????????
????????
? // ? 測(cè)試一下索引的時(shí)間??? ?
?
???????? ? long ? ?endTime? ? = ? ? ? new ? ?Date().getTime();???
????????System.out???
????????????????.println(
? " ? 這花費(fèi)了 ? " ? ??
????????????????????????
? + ? ?(endTime? ? - ? ?startTime)???
????????????????????????
? + ? ? ? " ? ?毫秒來(lái)把文檔增加到索引里面去! ? " ? ??
????????????????????????
? + ? ?fileDir.getPath());???
????}
?
???
??
????
? public ? ? ? static ? ?String?FileReaderAll(String?FileName,?String?charset)???
????????????
? throws ? ?IOException? ? {???
????????BufferedReader?reader?
? = ? ? ? new ? ?BufferedReader( ? new ? ?InputStreamReader(???
????????????????
? new ? ?FileInputStream(FileName),?charset));???
????????String?line?
? = ? ? ? new ? ?String();???
????????String?temp?
? = ? ? ? new ? ?String();???
???????????
????????
? while ? ?((line? ? = ? ?reader.readLine())? ? != ? ? ? null ? )? ? {???
????????????temp?
? += ? ?line;???
????????}
?
???
????????reader.close();???
????????
? return ? ?temp;???
????}
?
???
}
?
?

索引的結(jié)果:

? File?C:\s\ ? 1 ? .txt正在被索引 .???
中華人民共和國(guó)全國(guó)人民2006年???
File?C:\s\
? 2 ? .txt正在被索引 .???
中華人民共和國(guó)全國(guó)人民2006年???
File?C:\s\
? 3 ? .txt正在被索引 .???
中華人民共和國(guó)全國(guó)人民2006年???
這花費(fèi)了297?毫秒來(lái)把文檔增加到索引里面去
? ! ? c:\s??


3、建立了索引之后,查詢啦....

? package ? ?lighter.iteye.com;???
??
? import ? ?java.io.IOException;???
??
? import ? ?org.apache.lucene.analysis.Analyzer;???
? import ? ?org.apache.lucene.analysis.standard.StandardAnalyzer;???
? import ? ?org.apache.lucene.queryParser.ParseException;???
? import ? ?org.apache.lucene.queryParser.QueryParser;???
? import ? ?org.apache.lucene.search.Hits;???
? import ? ?org.apache.lucene.search.IndexSearcher;???
? import ? ?org.apache.lucene.search.Query;???
??
? public ? ? ? class ? ?TestQuery? ? {???
????
? public ? ? ? static ? ? ? void ? ?main(String[]?args)? ? throws ? ?IOException,?ParseException? ? {???
????????Hits?hits?
? = ? ? ? null ? ;???
????????String?queryString?
? = ? ? ? " ? 中華 ? " ? ;???
????????Query?query?
? = ? ? ? null ? ;???
????????IndexSearcher?searcher?
? = ? ? ? new ? ?IndexSearcher( ? " ? c:\\index ? " ? );???
??
????????Analyzer?analyzer?
? = ? ? ? new ? ?StandardAnalyzer();???
????????
? try ? ? ? {???
????????????QueryParser?qp?
? = ? ? ? new ? ?QueryParser( ? " ? body ? " ? ,?analyzer);???
????????????query?
? = ? ?qp.parse(queryString);???
????????}
?
? ? catch ? ?(ParseException?e)? ? {???
????????}
?
???
????????
? if ? ?(searcher? ? != ? ? ? null ? )? ? {???
????????????hits?
? = ? ?searcher.search(query);???
????????????
? if ? ?(hits.length()? ? > ? ? ? 0 ? )? ? {???
????????????????System.out.println(
? " ? 找到: ? " ? ? ? + ? ?hits.length()? ? + ? ? ? " ? ?個(gè)結(jié)果! ? " ? );???
????????????}
?
???
????????}
?
???
????}
?
?
??
}
?
??

其運(yùn)行結(jié)果:

? 找到: ? 3 ? ?個(gè)結(jié)果 ? !

?

Lucene 其實(shí)很簡(jiǎn)單的,它最主要就是做兩件事:建立索引和進(jìn)行搜索?
來(lái)看一些在lucene中使用的術(shù)語(yǔ),這里并不打算作詳細(xì)的介紹,只是點(diǎn)一下而已----因?yàn)檫@一個(gè)世界有一種好東西,叫搜索。

IndexWriter :lucene中最重要的的類之一,它主要是用來(lái)將文檔加入索引,同時(shí)控制索引過(guò)程中的一些參數(shù)使用。

Analyzer :分析器,主要用于分析搜索引擎遇到的各種文本。常用的有StandardAnalyzer分析器,StopAnalyzer分析器,WhitespaceAnalyzer分析器等。

Directory :索引存放的位置;lucene提供了兩種索引存放的位置,一種是磁盤(pán),一種是內(nèi)存。一般情況將索引放在磁盤(pán)上;相應(yīng)地lucene提供了FSDirectory和RAMDirectory兩個(gè)類。

Document :文檔;Document相當(dāng)于一個(gè)要進(jìn)行索引的單元,任何可以想要被索引的文件都必須轉(zhuǎn)化為Document對(duì)象才能進(jìn)行索引。

Field :字段。

IndexSearcher :是lucene中最基本的檢索工具,所有的檢索都會(huì)用到IndexSearcher工具;

Query :查詢,lucene中支持模糊查詢,語(yǔ)義查詢,短語(yǔ)查詢,組合查詢等等,如有TermQuery,BooleanQuery,RangeQuery,WildcardQuery等一些類。

QueryParser : 是一個(gè)解析用戶輸入的工具,可以通過(guò)掃描用戶輸入的字符串,生成Query對(duì)象。

Hits :在搜索完成之后,需要把搜索結(jié)果返回并顯示給用戶,只有這樣才算是完成搜索的目的。在lucene中,搜索的結(jié)果的集合是用Hits類的實(shí)例來(lái)表示的。

上面作了一大堆名詞解釋,下面就看幾個(gè)簡(jiǎn)單的實(shí)例吧:?
1、簡(jiǎn)單的的StandardAnalyzer測(cè)試?yán)?

?

? package ? ?lighter.iteye.com;???
??
? import ? ?java.io.IOException;???
? import ? ?java.io.StringReader;???
??
? import ? ?org.apache.lucene.analysis.Analyzer;???
? import ? ?org.apache.lucene.analysis.Token;???
? import ? ?org.apache.lucene.analysis.TokenStream;???
? import ? ?org.apache.lucene.analysis.standard.StandardAnalyzer;???
??
? public ? ? ? class ? ?StandardAnalyzerTest????
? {???
????
? // ? 構(gòu)造函數(shù),??? ?
?
???? ? public ? ?StandardAnalyzerTest()???
????
? {???
????}
?
???
????
? public ? ? ? static ? ? ? void ? ?main(String[]?args)????
????
? {???
????????
? // ? 生成一個(gè)StandardAnalyzer對(duì)象??? ?
?
????????Analyzer?aAnalyzer? ? = ? ? ? new ? ?StandardAnalyzer();???
????????
? // ? 測(cè)試字符串??? ?
?
????????StringReader?sr? ? = ? ? ? new ? ?StringReader( ? " ? lighter?javaeye?com?is?the?are?on ? " ? );???
????????
? // ? 生成TokenStream對(duì)象??? ?
?
????????TokenStream?ts? ? = ? ?aAnalyzer.tokenStream( ? " ? name ? " ? ,?sr);????
????????
? try ? ? ? {???
????????????
? int ? ?i ? = ? 0 ? ;???
????????????Token?t?
? = ? ?ts.next();???
????????????
? while ? (t ? != ? null ? )???
????????????
? {???
????????????????
? // ? 輔助輸出時(shí)顯示行號(hào)??? ?
?
????????????????i ? ++ ? ;???
????????????????
?
<
分享到:
評(píng)論

[轉(zhuǎn)]Lucene 2 教程


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。?!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 达日县| 库伦旗| 兴安县| 莎车县| 益阳市| 湘潭市| 岳阳市| 兴城市| 石城县| 海安县| 新巴尔虎左旗| 保康县| 桦南县| 元谋县| 胶州市| 察隅县| 翁源县| 汽车| 铁岭市| 龙门县| 邹平县| 峨眉山市| 元朗区| 临汾市| 右玉县| 锦州市| 繁峙县| 二手房| 雷波县| 肥东县| 山东省| 牙克石市| 彩票| 普定县| 武宁县| 门头沟区| 习水县| 左云县| 罗源县| 湘西| 宜丰县|