Lucene全文檢索索引之HelloWorld
1.下載Lucene4.4 然後解壓
2.建立一個Java項目,名稱為HelloLucene
3.建立一個lib檔案夾,將需要的jar檔案複製到lib中,本項目所需要的jar檔案如下:
[圖]
然後將這些jar檔案添加到buildPath中
3.建立一個包com.njupt.zhb,建立一個類:HelloLucene.java,代碼如下
[java code]
package com.njupt.zhb;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;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.document.LongField;import org.apache.lucene.document.StringField;import org.apache.lucene.document.TextField;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.index.IndexWriterConfig.OpenMode;import org.apache.lucene.index.Term;import org.apache.lucene.queryparser.classic.ParseException;import org.apache.lucene.queryparser.classic.QueryParser;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TopDocs;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;/* *@author: ZhengHaibo *web: http://blog.csdn.net/nuptboyzhb *mail: zhb931706659@126.com *2013-7-05 Nanjing,njupt,China */public class HelloLucene {/** * Index all text files under a directory. * String indexPath = "index";//索引儲存的路徑 * String docsPath = "";//文檔儲存的路徑(待索引) */public void index(String indexPath,String docsPath) {try {// 1.建立DirectoryDirectory dir = FSDirectory.open(new File(indexPath));//儲存在硬碟上// 2.建立IndexWriterAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_44,analyzer);iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);// 設定建立或追加模式IndexWriter writer = new IndexWriter(dir, iwc);final File docDir = new File(docsPath);indexDocs(writer, docDir);writer.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} public void indexDocs(IndexWriter writer, File file) throws IOException {if (file.canRead()) {if (file.isDirectory()) {//如果是檔案夾,則遍曆檔案夾內的所有檔案String[] files = file.list();// an IO error could occurif (files != null) {for (int i = 0; i < files.length; i++) {indexDocs(writer, new File(file, files[i]));}}} else {//如果是檔案FileInputStream fis;try {fis = new FileInputStream(file);} catch (FileNotFoundException fnfe) {return;}try {// 3.建立Document對象Document doc = new Document();// 4.為Document添加Field// Add the path of the file as a field named "path". Use a// field that is indexed (i.e. searchable), but don't// tokenize// the field into separate words and don't index term// frequency// or positional information://以檔案的檔案路徑建立FieldField pathField = new StringField("path", file.getPath(),Field.Store.YES);doc.add(pathField);//添加到文檔中//以檔案的名稱建立索引域doc.add( new StringField("filename", file.getName(),Field.Store.YES));//添加到文檔中// Add the last modified date of the file a field named// "modified".// Use a LongField that is indexed (i.e. efficiently// filterable with// NumericRangeFilter). This indexes to milli-second// resolution, which// is often too fine. You could instead create a number// based on// year/month/day/hour/minutes/seconds, down the resolution// you require.// For example the long value 2011021714 would mean// February 17, 2011, 2-3 PM.doc.add(new LongField("modified", file.lastModified(),Field.Store.YES));// Add the contents of the file to a field named "contents".// Specify a Reader,// so that the text of the file is tokenized and indexed,// but not stored.// Note that FileReader expects the file to be in UTF-8// encoding.// If that's not the case searching for special characters// will fail.//以檔案的內容建立索引域(Field)doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(fis, "UTF-8"))));if (writer.getConfig().getOpenMode() == OpenMode.CREATE) {// New index, so we just add the document (no old// document can be there):System.out.println("adding " + file);writer.addDocument(doc);//將文檔寫入到索引中(以建立的方式)} else {// Existing index (an old copy of this document may have// been indexed) so// we use updateDocument instead to replace the old one// matching the exact// path, if present:System.out.println("updating " + file);writer.updateDocument(new Term("path", file.getPath()),doc);//以追加方式寫入到索引中}} finally {fis.close();}}}}/** * 搜尋 * http://blog.csdn.net/nuptboyzhb */public void searcher(String indexPath){try {IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexPath)));IndexSearcher searcher = new IndexSearcher(reader);Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);String field = "contents";//搜尋域是:文檔的內容QueryParser parser = new QueryParser(Version.LUCENE_44, field, analyzer); Query query= parser.parse("南京");//搜尋內容中含有“南京”的文檔 TopDocs tds=searcher.search(query, 10);//搜尋前十個 ScoreDoc[] sds= tds.scoreDocs; for (ScoreDoc sd:sds) {//將內容中含有“南京”關鍵字的文檔遍曆一遍Document document=searcher.doc(sd.doc);System.out.println("score:"+sd.score+"--filename:"+document.get("filename")+"--path:"+document.get("path")+"--time"+document.get("modified"));//列印檢索結果中文檔的路徑} reader.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
4.為了實驗,我在D盤下建立了一個檔案夾lucene,裡面有三個檔案,它們的內容如下:
lucene1.txt
Nanjing University Of Posts & Telec
lucene2.txt
南京郵電大學北京市海澱區上海市南京路
lucene3. txt
2014南京青奧會
5.建立一個Junit測試類別,對index函數和searcher函數進行測試,代碼如下:
[java code]
package com.njupt.zhb;import org.junit.Test;/* *@author: ZhengHaibo *web: http://blog.csdn.net/nuptboyzhb *mail: zhb931706659@126.com *2013-7-05 Nanjing,njupt,China */public class TestJunit { @Test public void TestIndex(){ HelloLucene hLucene=new HelloLucene(); hLucene.index("index", "D:\\lucene"); } @Test public void TestSearcher(){ HelloLucene hLucene=new HelloLucene(); hLucene.searcher("index"); }}
以Junit方式運行TestIndex函數,運行結果如下:
updating D:\lucene\lucene1.txt
updating D:\lucene\lucene2.txt
updating D:\lucene\lucene3.txt
索引建立完成!
在項目目錄的index目錄下,就產生了如下索引檔案:
[圖]
6.搜尋,測試TestSearcher函數,運行結果如下:
score:0.53033006--filename:lucene3.txt--path:D:\lucene\lucene3.txt--time1376828819375score:0.48666292--filename:lucene2.txt--path:D:\lucene\lucene2.txt--time1376828783791
可以看出,我們這裡只是把分數列印出來了,並沒有排名,分數越小,說明越相似!
原始碼下載:http://download.csdn.net/detail/nuptboyzhb/5971331未經允許不得用於商業目的