說明一下,這一篇文章的用到的lucene,是用2.0版本的,主要在查詢的時候2.0版本的lucene與以前的版本有了一些區別.
其實這一些代碼都是早幾個月寫的,自己很懶,所以到今天才寫到自己的博客上,高深的文章自己寫不了,只能記錄下一些簡單的記錄與點滴,其中的代碼算是自娛自樂的,希望高手不要把重構之類的砸下來...
1、在windows系統下的的C盤,建一個名叫s的文件夾,在該文件夾里面隨便建三個txt文件,隨便起名啦,就叫"1.txt","2.txt"和"3.txt"啦
其中1.txt的內容如下:
代碼
-
中華人民共和國 ??
-
全國人民 ??
-
2006
年??
而"2.txt"和"3.txt"的內容也可以隨便寫幾寫,這里懶寫,就復制一個和1.txt文件的內容一樣吧
2、下載lucene包,放在classpath路徑中
建立索引:
代碼
- package?lighter.javaeye.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; ??
- ??
- ?
- ?
- ??
- public?class?TextFileIndexer?{ ??
- ????public?static?void?main(String[]?args)?throws?Exception?{ ??
- ??????????
- ????????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(); ??
- ???????? ??
- ??????????
- ????????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); ??
- ????????????} ??
- ????????} ??
- ??????????
- ????????indexWriter.optimize(); ??
- ????????indexWriter.close(); ??
- ???????? ??
- ??????????
- ????????long?endTime?=?new?Date().getTime(); ??
- ????????System.out ??
- ????????????????.println("這花費了"??
- ????????????????????????+?(endTime?-?startTime) ??
- ????????????????????????+?"?毫秒來把文檔增加到索引里面去!"??
- ????????????????????????+?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; ??
- ????} ??
- }??
索引的結果:
代碼
- File?C:\s\1.txt正在被索引.... ??
- 中華人民共和國全國人民2006年 ??
- File?C:\s\2.txt正在被索引.... ??
- 中華人民共和國全國人民2006年 ??
- File?C:\s\3.txt正在被索引.... ??
- 中華人民共和國全國人民2006年 ??
- 這花費了297?毫秒來把文檔增加到索引里面去!c:\s??
3、建立了索引之后,查詢啦....
代碼
- package?lighter.javaeye.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()?+?"?個結果!"); ??
- ????????????} ??
- ????????} ??
- ????} ??
- ??
- }??
其運行結果:
引用
找到:3 個結果!
具體的API的用法,這里就不說了,具體的做法參考lucene的官方文檔吧...
下一篇文章:
搜索篇:lucene的簡單實例<二>
http://www.javaeye.com/post/190576