A simple example about full-text search based Java:Lucene

來源:互聯網
上載者:User
索引原始碼:
package lucene;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author Shirley
* @version 1.0
*/

import org.apache.lucene.index.*;
import org.apache.lucene.analysis.*;
import java.io.*;
import org.apache.lucene.document.*;

public class IndexFiles {
  //使用方法:: IndexFiles [索引輸出目錄] [索引的檔案清單] ...
  public static void main(String[] arg) throws Exception {
    String[] args = new String[2];
    //索引後存放索引資訊的路徑
    args[0] = System.getProperty("java.io.tmpdir", "tmp") + System.getProperty("file.separator") + "index-1";
    //待索引檔案
    args[1] = "E:\\AppWork\\lucene\\rfc2047.txt";
    args[2] = "E:\\AppWork\\cyberoffice\\CO\\Sheldon Java Mail.htm";
    args[3] = "E:\\AppWork\\lucene\\englishtest.doc";
    args[4] = "E:\\AppWork\\cyberoffice\\CO\\xls1.xls";
    args[5] = "E:\\AppWork\\cyberoffice\\CO\\ppt1.ppt";

    String indexPath = args[0];
    IndexWriter writer;
    //用指定的語言分析器構造一個新的寫索引器(第3個參數表示是否為追加索引)
    writer = new IndexWriter(indexPath, new SimpleAnalyzer(), false);

    for (int i=1; i<args.length; i++) {
      System.out.println("Indexing file " + args[i]);
      InputStream is = new FileInputStream(args[i]);

      //構造包含2個欄位Field的Document對象
      //一個是路徑path欄位,不索引,只儲存
      //一個是內容body欄位,進行全文索引,並儲存
      Document doc = new Document();
      doc.add(Field.UnIndexed("path", args[i]));
      doc.add(Field.Text("body", (Reader) new InputStreamReader(is)));
      //將文檔寫入索引
      writer.addDocument(doc);
      is.close();
    };
    //關閉寫索引器
    writer.close();
  }
}




搜尋原始碼:
package lucene;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author Shirley
* @version 1.0
*/

import org.apache.lucene.search.*;
import org.apache.lucene.queryParser.*;
import org.apache.lucene.analysis.*;

public class Search {
  public static void main(String[] arg) throws Exception {
    String[] args = new String[2];
    //索引後存放索引資訊的路徑
    args[0] = System.getProperty("java.io.tmpdir", "tmp") + System.getProperty("file.separator") + "index-1";
    //搜尋關鍵字
    args[1] = "sending";

    String indexPath = args[0];
    String queryString = args[1];

    //指向索引目錄的搜尋器
    Searcher searcher = new IndexSearcher(indexPath);
    //查詢解析器:使用和索引同樣的語言分析器
    Query query = QueryParser.parse(queryString, "body", new SimpleAnalyzer());
    //搜尋結果使用Hits儲存
    Hits hits = searcher.search(query);
    //通過hits可以訪問到相應欄位的資料和查詢的匹配度
    for (int i=0; i<hits.length(); i++) {
      System.out.println(hits.doc(i).get("path") + "; Score: " +  hits.score(i));
    };
  }
}


註:目前程式只支援英文索引,可以過濾檔案類型為.txt  .doc  .htm  .xls  .ppt

中文索引及其它類型檔案的索引正在研究中......

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.