Lucene簡單實現建立索引以及查詢
阿新 • • 發佈:2019-02-10
package com.rdz.test; import java.io.File; import java.io.FileReader; import java.io.IOException; 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.Store; 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.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.store.IOContext; import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.util.Version; public class LuceneTest2 { private String indexPath="G:/lucene/myindex"; private String filePath="G:/lucene/myfile"; /** * 建立索引 */ public void create(){ Directory directory=null; IndexWriter indexWriter=null; try { //建立Directory(在磁碟上建立) directory=FSDirectory.open(new File(indexPath)); //建立分詞器 Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_43); //建立IndexWriteConfig IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_43, analyzer); iwc.setOpenMode(OpenMode.CREATE); //建立模式(覆蓋原來的索引) //建立IndexWrite(通過IndexWrite,來寫我們的索引) indexWriter=new IndexWriter(directory, iwc); //建立Document物件 (相當於資料庫中的表,而Field則相當於其中的欄位) Document doc=null; File files=new File(filePath); for (File file : files.listFiles()) { //將files中的所有檔案迴圈讀取,將內容,檔名,路徑作為Field寫入doc doc=new Document(); doc.add(new TextField(FileNameUtil.CONTENT, new FileReader(file))); doc.add(new StringField(FileNameUtil.TITLE, file.getName(),Store.YES)); //Store表示是否將這個檔名儲存到磁碟中 doc.add(new StringField(FileNameUtil.PATH, file.getPath(),Store.YES)); //通過IndexWriter新增文件到索引中 indexWriter.addDocument(doc); System.out.println("檔案"+file+"索引建立成功"); } } catch (Exception e) { e.printStackTrace(); }finally{ try { //關閉資源 if(indexWriter!=null){ indexWriter.close(); } if(directory!=null){ directory.close(); } } catch (Exception e) { e.printStackTrace(); } } } /** * 根據關鍵字查詢 * @param keyWord */ public void SearcherIndex(String keyWord){ Directory d=null; Directory directory=null; IndexReader iReader=null; try { //建立Directory d=FSDirectory.open(new File(indexPath)); directory=new RAMDirectory(d,new IOContext()); //讀取這個索引檔案 iReader=DirectoryReader.open(directory); //建立一個IndexSearch物件,用來索引需要找的關鍵字 IndexSearcher searcher=new IndexSearcher(iReader); //建立分詞器 Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_43); //建立查詢的Query QueryParser parser=new QueryParser(Version.LUCENE_43, FileNameUtil.CONTENT,analyzer); Query query=parser.parse(keyWord); //需要搜尋的內容 //開始搜尋 TopDocs tds=searcher.search(query, 10); //10表示取前10條記錄 ScoreDoc[] sds=tds.scoreDocs; Document doc; for (ScoreDoc scoreDoc : sds) { doc=searcher.doc(scoreDoc.doc); System.out.println("檔案的名稱:"+doc.get(FileNameUtil.TITLE)); System.out.println("檔案的路徑:"+doc.get(FileNameUtil.PATH)); } } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); }finally{ try { //關閉資源 if(d!=null){ d.close(); } if(directory!=null){ directory.close(); } if(iReader!=null){ iReader.close(); } } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { LuceneTest2 lt=new LuceneTest2(); lt.create(); lt.SearcherIndex("oracle"); } }