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

📄 indexerimpl.java

📁 采用tapestry的简单OA系统
💻 JAVA
字号:
package com.ejsun.entapps.core.search.lucene;

import java.io.IOException;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;

import com.ejsun.entapps.core.search.Indexer;
import com.ejsun.entapps.core.search.SearchException;
import com.ejsun.entapps.core.search.SearchUtil;
import com.ejsun.entapps.core.search.Searchable;
import com.ejsun.entapps.core.search.lucene.builder.DocumentBuilder;
import com.ejsun.entapps.domain.Entity;

/**
 * @author	Quake Wang
 * @since	2004-5-21
 * @version $Revision: 1.3 $
 * 
 **/
public class IndexerImpl implements Indexer {
    private static final Log log = LogFactory.getLog(IndexerImpl.class);
    private Analyzer analyzer;
    private Map builders;
    private IndexStore indexStore;
    
    public IndexerImpl() {
    	indexStore = new IndexStore();
    }

    public void index(Entity entity) {
        if (!(entity instanceof Searchable))
            return;
        unIndex(entity);
        IndexWriter writer = null;
        try {
            writer = indexStore.createWriter(analyzer);
            writer.addDocument(createDocument(entity));
            writer.optimize();
        } catch (IOException ioe) {
            log.error(ioe);
            throw new SearchException("Can index: " + entity); 
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException ioe) {
                    log.warn(ioe);
                }
            }
        }
    }

    public void unIndex(Entity entity) {
		if (!(entity instanceof Searchable))
			return;    	
        IndexReader reader = null;
        try {
            reader = indexStore.createReader();
			Term t = new Term(SearchUtil.IDENTIFIER, SearchUtil.getIdentifier(entity));
			reader.delete(t);            
        } catch (IOException ioe) {
			log.error(ioe);
			throw new SearchException("Can index: " + entity);
        } finally {
        	if (reader != null) {
        		try{
					reader.close();	
        		} catch (IOException ioe) {
        			log.warn(ioe);
        		}
        	}
			
        }
    }

    private Document createDocument(Entity entity) {
        DocumentBuilder builder = (DocumentBuilder) builders.get(entity.getClass().getName());
        if (builder == null) {
            throw new SearchException("Can not get document builder for entity: " + entity.getClass().getName());
        } else {
            return builder.createDocument(entity);
        }
    }

    public void setAnalyzer(Analyzer analyzer) {
        this.analyzer = analyzer;
    }

    public void setBuilders(Map map) {
        builders = map;
    }

}

⌨️ 快捷键说明

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