⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 indexstore.java

📁 开源项目simpleoa-0.3.zip是最新版本
💻 JAVA
字号:
package com.ejsun.entapps.core.search.lucene;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import com.ejsun.entapps.core.search.SearchException;

/**
 * @author	Quake Wang
 * @since	2004-5-23
 * @version $Revision: 1.1 $
 * 
 **/
public class IndexStore {
    private static final Log log = LogFactory.getLog(IndexStore.class);

	public IndexReader createReader() throws IOException {
		createIndexDirectory(false);
		return IndexReader.open(getDirectory(false));
	}
	
	public IndexWriter createWriter(Analyzer analyzer) throws IOException {
		createIndexDirectory(false);
		return new IndexWriter(getDirectory(false), analyzer, false);
	}
	
	public IndexSearcher createSearcher() throws IOException {
		createIndexDirectory(false);
		return new IndexSearcher(getDirectory(false));
	}
	
    private void createIndexDirectory(boolean forceCreate) {
        if (forceCreate || !isIndexCreated()) {
            try {
                new IndexWriter(getDirectory(true), null, true).close();
            } catch (IOException ioe) {
                log.error(ioe);
                throw new SearchException("Cannot create index directory");
            }
        }
    }

    private Directory getDirectory(boolean createIt) throws IOException {
        return FSDirectory.getDirectory(getIndexDir(), createIt);
    }

    private String getIndexDir() {
        //TODO read the index dir setting from configuration file
        return "index";
    }

    private boolean isIndexCreated() {
        boolean created = false;
        try {
            created = IndexReader.indexExists(getDirectory(false));
        } catch (IOException e) {
            log.error("Error creating reader: " + e, e);
        }
        return created;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -