📄 indexerimpl.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 {
//~ Static fields/initializers =============================================
/** DOCUMENT ME! */
private static final Log log = LogFactory.getLog(IndexerImpl.class);
//~ Instance fields ========================================================
/** DOCUMENT ME! */
private Analyzer analyzer;
/** DOCUMENT ME! */
private IndexStore indexStore;
/** DOCUMENT ME! */
private Map builders;
//~ Constructors ===========================================================
/**
* Creates a new IndexerImpl object.
*/
public IndexerImpl() {
indexStore = new IndexStore();
}
//~ Methods ================================================================
/**
* 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);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -