Lucene實戰-Indexer索引建立

來源:互聯網
上載者:User

標籤:

package com.lin.util;import java.io.File;import java.io.FileFilter;import java.io.FileReader;import java.io.IOException;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.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;import org.wltea.analyzer.lucene.IKAnalyzer;public class Indexer {private IndexWriter writer;/** * 主程式 *  * @param indexDir *            索引位置 * @param dataDir *            資料來源 * @throws Exception */public static void index(String indexDir, String dataDir) throws Exception {if (indexDir == null || dataDir == null) {throw new IllegalArgumentException("請檢查你的參數是否正確");}long start = System.currentTimeMillis();Indexer indexer = new Indexer(indexDir);int numIndexed;try {numIndexed = indexer.index(dataDir, new TextFilesFilter());} finally {indexer.close();}long end = System.currentTimeMillis();System.out.println("Indexing " + numIndexed + " files took "+ (end - start) + " milliseconds");}/** * 初始化writer(用與建立索引) *  * @param indexDir * @throws IOException */private Indexer(String indexDir) throws IOException {Directory dir = FSDirectory.open(new File(indexDir));IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_2,new IKAnalyzer());writer = new IndexWriter(dir, config);}/** *  * @param dataDir * @param filter * @return 索引的檔案數  * @throws IOException */public int index(String dataDir, FileFilter filter) throws IOException {File[] files = new File(dataDir).listFiles();for (File f : files) {if (!f.isDirectory() && !f.isHidden() && f.canRead() && f.exists()&& (filter == null || filter.accept(f))) {indexFile(f);}}return writer.numDocs();}private void indexFile(File f) throws IOException {System.out.println("indexing " + f.getCanonicalPath());Document doc = getDocument(f);writer.addDocument(doc);}@SuppressWarnings("deprecation")protected Document getDocument(File f) throws IOException {Document doc = new Document();doc.add(new Field("contents", new FileReader(f)));doc.add(new Field("filename", f.getName(), Field.Store.YES,Field.Index.NOT_ANALYZED));doc.add(new Field("fullpath", f.getCanonicalPath(), Field.Store.YES,Field.Index.NOT_ANALYZED));return doc;}/** * 檔案過濾器 *  * @author zan *  */private static class TextFilesFilter implements FileFilter {public boolean accept(File f) {return f.getName().toLowerCase().endsWith(".txt");}}public void close() throws IOException {if (writer != null) {writer.close();}}public static void main(String[] args) throws Exception {Indexer.index("d:\\index", "D:\\Program Files\\TortoiseSVN");}}

Lucene實戰-Indexer索引建立

聯繫我們

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