📄 bbswriterim.java
字号:
package com.yhbbs.article.search.biz;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.mira.lucene.analysis.IK_CAnalyzer;
import com.yhbbs.article.itface.Article;
import com.yhbbs.bbs.biz.BbsPropBiz;
import com.yhbbs.forum.biz.ForumBiz;
import com.yhbbs.utils.Constants;
/**
* <p>Title:论坛搜索相关操作</p>
* <li> 论坛帖子建立索引的相关操作
* <p>Company: www.yyhweb.com</p>
* <br><b>CopyRight: yyhweb[由由华网]</b>
* @author stephen
* @version YHBBS-2.0
*/
public class BbsWriterIm {
private static Logger bbslog = Logger.getLogger(BbsWriterIm.class);
private static IndexReader reader;
private static String directory = "";
protected static Analyzer ikAnalyzer = new IK_CAnalyzer();
/**
* @return 系统搜索索引路径
*/
protected static String getIndexPath(){
if(directory==null || directory.length()<1){
directory = BbsPropBiz.getParameter().getSearchPath();
if(!new File(directory).exists())
new File(directory).mkdirs();
}
return directory;
}
/**
* @return 检查是否已经存在索引文件
*/
private static boolean isIndexed(){
String directory = getIndexPath();
File indexFile = new File(directory+File.separator+"segments.gen");
return indexFile.exists();
}
/**
* @return 取得IndexWriter
*/
private static IndexWriter getWriter(){
IndexWriter writer = null;
String indexPath = getIndexPath();
if(isIndexed()){
try {
writer = new IndexWriter(indexPath,ikAnalyzer,false);
}catch (Exception e) {
bbslog.error("Throws a Exception when invoke getWriter()!");
e.printStackTrace();
}
}else{
try {
writer = new IndexWriter(indexPath,ikAnalyzer,true);
}catch (Exception e) {
bbslog.error("Throws a Exception when invoke getWriter()!");
e.printStackTrace();
}
}
return writer;
}
/**
* @return 取得IndexReader
*/
protected static IndexReader getReader(){
try {
if(reader==null)
reader = IndexReader.open(getIndexPath());
return reader;
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 开始索引帖子
* @param article 帖子
* @throws IOException
* @throws Exception
* @throws Exception
*/
public synchronized static void writerIndex(IndexWriter writer,Article article) throws Exception {
Document doc = new Document();
int pId = article.getParentid();
int fId = article.getForum();
doc.add(new Field("artId",String.valueOf(article.getId()),Field.Store.YES,Field.Index.NO_NORMS));
doc.add(new Field("parentId",String.valueOf(pId),Field.Store.YES,Field.Index.NO_NORMS));
if(pId==0)
doc.add(new Field("title",article.getTitle(),Field.Store.YES,Field.Index.TOKENIZED));
doc.add(new Field("forumId",String.valueOf(fId),Field.Store.YES,Field.Index.NO_NORMS));
doc.add(new Field("forumName",ForumBiz.getForumName(fId),Field.Store.YES,Field.Index.NO_NORMS));
doc.add(new Field("classId",String.valueOf(article.getClassid()),Field.Store.YES,Field.Index.NO_NORMS));
doc.add(new Field("author",article.getUser(),Field.Store.YES,Field.Index.TOKENIZED));
doc.add(new Field("authorId",String.valueOf(article.getUserid()),Field.Store.YES,Field.Index.NO_NORMS));
doc.add(new Field("posttime",article.getPosttime(),Field.Store.YES,Field.Index.NO_NORMS));
doc.add(new Field("content",article.getContent().getContent(),Field.Store.NO,Field.Index.TOKENIZED));
writer.addDocument(doc);
return;
}
/** 建立帖子索引
* @param article 帖子
*/
public static void indexArticle(Article article){
IndexWriter writer = getWriter();
if(writer!=null){
try {
writerIndex(writer,article);
writer.optimize();
}catch (Exception e) {
bbslog.error("Throws a Exception when invoke indexArticle(Article article)!");
e.printStackTrace();
}finally{
try {
if(writer!=null)
writer.close();
}catch (Exception e) {
writer = null;
bbslog.error("Throws a Exception when close indexwriter!");
e.printStackTrace();
}
}
}
}
/** 建立帖子列表索引
* @param artList 帖子列表
*/
public static void indexList(List artList){
IndexWriter writer = getWriter();
if(writer!=null){
try {
Article curArt = null;
for(int i=0;i<artList.size();i++){
curArt = (Article) artList.get(i);
writerIndex(writer,curArt);
}
writer.optimize();
}catch (Exception e) {
bbslog.error("Throws a Exception when invoke indexList(List artList)!");
e.printStackTrace();
}finally{
try {
if(writer!=null)
writer.close();
}catch (Exception e) {
writer = null;
bbslog.error("Throws a Exception when close indexwriter!");
e.printStackTrace();
}
}
}
}
/** 删除一个索引
* @param term 一个Term
*/
private static void deleteDoc(Term term){
getReader();
if(reader!=null){
try {
reader.deleteDocuments(term);
}catch (Exception e) {
e.printStackTrace();
}finally{
if(reader!=null){
try {
reader.close();
reader = null;
}catch (IOException e) {
reader = null;
e.printStackTrace();
}
}
}
}
}
/** 删除一个帖子的索引
* @param artId 帖子Id
*/
public static void deleteArt(int artId){
Term term = new Term("artId", String.valueOf(artId));
deleteDoc(term);
}
/** 更新索引
* @param article 帖子
* @throws Exception
*/
public static void updateIndex(Article article){
deleteArt(article.getId());
indexArticle(article);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -