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

📄 indexerimpl.java

📁 Jaoso新闻文章发布系统 0.9.1final 程序架构: Struts+Spring+Hibernate 主要功能:   ·新闻采用在线编辑器,可以象使用word一样编辑新闻,可简繁
💻 JAVA
字号:
package jaoso.framework.core.search.lucene;

import jaoso.framework.core.search.Indexer;
import jaoso.framework.core.search.SearchUtil;
import jaoso.framework.core.search.Searchable;
import jaoso.framework.core.search.lucene.builder.DocumentBuilder;

import jaoso.framework.exception.SearchException;

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 java.io.IOException;

import java.util.Map;

/**
 * @author Quake Wang,Edgeloner
 * @since 2004-5-21
 * @version $Revision: 1.3 $
 *  
 */
public class IndexerImpl implements Indexer {
	/** DOCUMENT ME! */
	private static final Log log = LogFactory.getLog(IndexerImpl.class);

	/** DOCUMENT ME! */
	private Analyzer analyzer;

	/** DOCUMENT ME! */
	private IndexStore indexStore;

	/** DOCUMENT ME! */
	private Map builders;

	/**
	 * DOCUMENT ME!
	 * 
	 * @param analyzer
	 *            DOCUMENT ME!
	 */
	public void setAnalyzer(Analyzer analyzer) {
		this.analyzer = analyzer;
	}

	/**
	 * DOCUMENT ME!
	 * 
	 * @param map
	 *            DOCUMENT ME!
	 */
	public void setBuilders(Map map) {
		builders = map;
	}

	/**
	 * DOCUMENT ME!
	 * 
	 * @param entity
	 *            DOCUMENT ME!
	 */
	public void index(Searchable 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);
				}
			}
		}
	}

	/**
	 * DOCUMENT ME!
	 * 
	 * @param entity
	 *            DOCUMENT ME!
	 */
	public void unIndex(Searchable 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);
				}
			}
		}
	}

	/**
	 * DOCUMENT ME!
	 * 
	 * @param entity
	 *            DOCUMENT ME!
	 * 
	 * @return DOCUMENT ME!
	 */
	private Document createDocument(Searchable 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 setIndexStore(IndexStore indexStore) {
        this.indexStore = indexStore;
    }
}

⌨️ 快捷键说明

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