📄 postindexer.java
字号:
//now we have created document with fields so we can store it
try {
writer.addDocument(postDocument);
} catch (IOException e) {
log.error("PostIndexer.doIndexPost failed", e);
//@todo : localize me
throw new SearchException("Error writing new post to index");
}
}
/**
* Add single post to index
* @param post
* @throws SearchException
*/
static void addPostToIndex(PostBean post) throws SearchException, IOException {
Directory directory = null;
IndexWriter writer = null;
try {
directory = MVNForumConfig.getSearchPostIndexDir();
writer = getIndexWriter(directory, false);
if (writer == null) {
log.warn("Cannot get the IndexWriter");
return;
}
doIndexPost(post, writer);
// now check if we should optimize index (each hour)
long now = System.currentTimeMillis();
long timeFromLastOptimize = now - lastOptimizeTime;
if (timeFromLastOptimize > DateUtil.HOUR) {
log.debug("writer.optimize() called in addPostToIndex");
writer.optimize();
lastOptimizeTime = now;
}
} catch (SearchException ex) {
throw ex;
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
log.debug("Error closing Lucene IndexWriter", e);
}
}
if (directory != null) {
try {
directory.close();
} catch (IOException e) {
log.debug("Cannot close directory.", e);
}
}
}
}
/**
* This method is used for deleting post from index.
* @param postID id of the post that should be deleted
* @throws SearchException
*/
static void deletePostFromIndex(int postID) throws SearchException {
Directory directory = null;
IndexReader reader = null;
try {
directory = MVNForumConfig.getSearchPostIndexDir();
reader = IndexReader.open(directory);
if (reader == null) {
log.warn("Cannot get the IndexReader");
return;
}
Term term = new Term(FIELD_POST_ID, String.valueOf(postID));
int deletedCount = reader.delete(term);
log.debug("deletePostFromIndex: deleted posts = " + deletedCount);
} catch (IOException e) {
//@todo : localize me
throw new SearchException("Error trying to delete post with postID = " + postID);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
log.debug("Error closing Lucene IndexReader", e);
}
}
if (directory != null) {
try {
directory.close();
} catch (IOException e) {
log.debug("Cannot close directory.", e);
}
}
}
}
/**
* This method is used for deleting all posts in a thread from index.
* @param threadID id of the thread that should be deleted
* @throws SearchException
*/
static void deleteThreadFromIndex(int threadID) throws SearchException {
Directory directory = null;
IndexReader reader = null;
try {
directory = MVNForumConfig.getSearchPostIndexDir();
reader = IndexReader.open(directory);
if (reader == null) {
log.warn("Cannot get the IndexReader");
return;
}
Term term = new Term(FIELD_THREAD_ID, String.valueOf(threadID));
int deletedCount = reader.delete(term);
log.debug("deleteThreadFromIndex: deleted posts = " + deletedCount);
} catch (IOException e) {
//@todo : localize me
throw new SearchException("Error trying to delete posts in index with threadID = " + threadID);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
log.debug("Error closing Lucene IndexReader", e);
}
}
if (directory != null) {
try {
directory.close();
} catch (IOException e) {
log.debug("Cannot close directory.", e);
}
}
}
}
/**
* This method is used for deleting all posts in a forum from index.
* @param forumID id of the forum that should be deleted
* @throws SearchException
*/
static void deleteForumFromIndex(int forumID) throws SearchException {
Directory directory = null;
IndexReader reader = null;
try {
directory = MVNForumConfig.getSearchPostIndexDir();
reader = IndexReader.open(directory);
if (reader == null) {
log.warn("Cannot get the IndexReader");
return;
}
Term term = new Term(FIELD_FORUM_ID, String.valueOf(forumID));
int deletedCount = reader.delete(term);
log.debug("deleteForumFromIndex: deleted posts = " + deletedCount);
} catch (IOException e) {
//@todo : localize me
throw new SearchException("Error trying to delete posts in index with forumID = " + forumID);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
log.debug("Error closing Lucene IndexReader", e);
}
}
if (directory != null) {
try {
directory.close();
} catch (IOException e) {
log.debug("Cannot close directory.", e);
}
}
}
}
public static int getNumDocs() {
int numDocs = -1;
Directory directory = null;
IndexReader reader = null;
try {
directory = MVNForumConfig.getSearchPostIndexDir();
reader = IndexReader.open(directory);
if (reader == null) {
log.warn("Cannot get the IndexReader");
return -1;
}
numDocs = reader.numDocs();
} catch (IOException ioe) {
//ignore
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
log.debug("Error closing Lucene IndexReader", e);
}
}
if (directory != null) {
try {
directory.close();
} catch (IOException e) {
log.debug("Cannot close directory.", e);
}
}
}
return numDocs;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -