Lucene的一個簡單的標準測試(Lucene包基於3.5版本的),lucene3.5

來源:互聯網
上載者:User

Lucene的一個簡單的標準測試(Lucene包基於3.5版本的),lucene3.5

Lucene編程一般分為:索引、分詞、搜尋

索引原始碼:

package lucene的一個標準測試;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;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.index.IndexWriterConfig.OpenMode;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;public class TextFileIndexer {public static void main(String[] args) throws Exception {// 需要索引的源檔案位置File fileDir = new File("E:\\Lucene\\source");// 存放索引的檔案位置File indexDir = new File("E:\\Lucene\\index");// 把索引建立在硬碟中Directory dir = FSDirectory.open(indexDir);// 建立一個標準分詞器Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_35);//IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,luceneAnalyzer);// 每次執行都是建立新的索引而不是追加索引iwc.setOpenMode(OpenMode.CREATE);// 建立一個索引器IndexWriter indexWriter = new IndexWriter(dir, iwc);// 儲存源檔案的多個檔案到數組File[]中File[] textFiles = fileDir.listFiles();// 索引的開始時間long startTime = new Date().getTime();// for迴圈遍曆來源目錄下的檔案for (int i = 0; i < textFiles.length; i++) {if (textFiles[i].isFile()&& textFiles[i].getName().endsWith(".txt")) { // 擷取相對路徑???System.out.println("檔案" + textFiles[i].getCanonicalPath()+ "正在被索引...");// 調用自訂讀取檔案內容的方法FileReaderAll()String temp = FileReaderAll(textFiles[i].getCanonicalPath(),"GBK");// 列印讀取到的內容System.out.println(temp);// 為每一個檔案的索引資訊建立一個document對象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.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS);// 添加各域到document文檔對象中document.add(fieldPath);document.add(fieldBody);// 將文檔寫入索引indexWriter.addDocument(document);}}// 關閉indexWriter.close();// 測試一下索引時間long endTime = new Date().getTime();//getPath,getAbsolutePath,getCanonicalPath的區別參考轉載的文章System.out.println("用時:" + (endTime - startTime) + "毫秒來把文檔增加到索引裡面去,"+fileDir.getPath());System.out.println("用時:" + (endTime - startTime) + "毫秒來把文檔增加到索引裡面去,"+fileDir.getAbsolutePath());System.out.println("用時:" + (endTime - startTime) + "毫秒來把文檔增加到索引裡面去,"+fileDir.getCanonicalPath());}// 自訂讀取檔案內容的方法 FileReaderAll()public static String FileReaderAll(String FileName, String charset)throws IOException {BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(FileName), charset));//知制定讀取檔案方式charset:GBKString line = new String();String temp = new String();while ((line = reader.readLine()) != null) {temp += line;}reader.close();return temp;}}

搜尋原始碼:

package lucene的一個標準測試;import java.io.File;import java.io.IOException;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.index.IndexReader;import org.apache.lucene.queryParser.ParseException;import org.apache.lucene.queryParser.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.FSDirectory;import org.apache.lucene.util.Version;public class TestQuery {public static void main(String[] args) throws IOException,ParseException{//搜尋的索引路徑String index = "E:\\index";IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)));//定義在索引庫中進行查詢的searcherIndexSearcher searcher = new IndexSearcher(reader);ScoreDoc[] hits = null;//檢索詞String queryString="絕對秋香";//聲明query對象Query query = null;//分詞器Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);try {QueryParser qp = new QueryParser(Version.LUCENE_35,"body",analyzer);query = qp.parse(queryString);} catch (ParseException e) {e.printStackTrace();}if(searcher!=null){TopDocs results = searcher.search(query, 10);hits = results.scoreDocs;if(hits.length>0){System.out.println("找到hits.length="+hits.length+"個結果\n"+"找到results.totalHits="+results.totalHits);}searcher.close();}}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.