🐛 新用户注册提示服务器开小差问题修复
This commit is contained in:
parent
8795621531
commit
91796ade67
@ -3,6 +3,7 @@ package com.rymcu.forest.lucene.util;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.rymcu.forest.lucene.model.UserLucene;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.Field;
|
||||
import org.apache.lucene.document.StringField;
|
||||
@ -21,67 +22,76 @@ import java.util.Arrays;
|
||||
*/
|
||||
public class UserIndexUtil {
|
||||
|
||||
/** lucene索引保存目录 */
|
||||
private static final String PATH = System.getProperty("user.dir") + StrUtil.SLASH + LucenePath.USER_PATH;
|
||||
/**
|
||||
* lucene索引保存目录
|
||||
*/
|
||||
private static final String PATH = System.getProperty("user.dir") + StrUtil.SLASH + LucenePath.USER_PATH;
|
||||
|
||||
/** 系统运行时索引保存目录 */
|
||||
private static final String INDEX_PATH = LucenePath.USER_INCREMENT_INDEX_PATH;
|
||||
/**
|
||||
* 系统运行时索引保存目录
|
||||
*/
|
||||
private static final String INDEX_PATH = LucenePath.USER_INCREMENT_INDEX_PATH;
|
||||
|
||||
/** 删除所有运行中保存的索引 */
|
||||
public static void deleteAllIndex() {
|
||||
if (FileUtil.exist(INDEX_PATH)) {
|
||||
FileUtil.del(INDEX_PATH);
|
||||
/**
|
||||
* 删除所有运行中保存的索引
|
||||
*/
|
||||
public static void deleteAllIndex() {
|
||||
if (FileUtil.exist(INDEX_PATH)) {
|
||||
FileUtil.del(INDEX_PATH);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void addIndex(UserLucene t) {
|
||||
creatIndex(t);
|
||||
}
|
||||
|
||||
public static void updateIndex(UserLucene t) {
|
||||
deleteIndex(t.getIdUser().toString());
|
||||
creatIndex(t);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加或创建单个索引
|
||||
*
|
||||
* @param t
|
||||
* @throws Exception
|
||||
*/
|
||||
private static synchronized void creatIndex(UserLucene t) {
|
||||
System.out.println("创建单个索引");
|
||||
IndexWriter writer;
|
||||
try {
|
||||
writer = IndexUtil.getIndexWriter(INDEX_PATH, false);
|
||||
Document doc = new Document();
|
||||
doc.add(new StringField("id", t.getIdUser() + "", Field.Store.YES));
|
||||
doc.add(new TextField("nickname", t.getNickname(), Field.Store.YES));
|
||||
doc.add(new TextField("signature", t.getSignature(), Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
public static void addIndex(UserLucene t) {
|
||||
creatIndex(t);
|
||||
}
|
||||
}
|
||||
|
||||
/** 删除单个索引 */
|
||||
public static synchronized void deleteIndex(String id) {
|
||||
Arrays.stream(FileUtil.ls(PATH))
|
||||
.forEach(
|
||||
each -> {
|
||||
if (each.isDirectory()) {
|
||||
IndexWriter writer;
|
||||
try {
|
||||
writer = IndexUtil.getIndexWriter(each.getAbsolutePath(), false);
|
||||
writer.deleteDocuments(new Term("id", id));
|
||||
writer.forceMergeDeletes(); // 强制删除
|
||||
writer.commit();
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
public static void updateIndex(UserLucene t) {
|
||||
deleteIndex(t.getIdUser().toString());
|
||||
creatIndex(t);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加或创建单个索引
|
||||
*
|
||||
* @param t
|
||||
* @throws Exception
|
||||
*/
|
||||
private static synchronized void creatIndex(UserLucene t) {
|
||||
System.out.println("创建单个索引");
|
||||
IndexWriter writer;
|
||||
try {
|
||||
writer = IndexUtil.getIndexWriter(INDEX_PATH, false);
|
||||
Document doc = new Document();
|
||||
doc.add(new StringField("id", t.getIdUser() + "", Field.Store.YES));
|
||||
doc.add(new TextField("nickname", t.getNickname(), Field.Store.YES));
|
||||
// 新注册用户无签名
|
||||
doc.add(new TextField("signature", StringUtils.isNotBlank(t.getSignature()) ? t.getSignature() : "", Field.Store.YES));
|
||||
writer.addDocument(doc);
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除单个索引
|
||||
*/
|
||||
public static synchronized void deleteIndex(String id) {
|
||||
Arrays.stream(FileUtil.ls(PATH))
|
||||
.forEach(
|
||||
each -> {
|
||||
if (each.isDirectory()) {
|
||||
IndexWriter writer;
|
||||
try {
|
||||
writer = IndexUtil.getIndexWriter(each.getAbsolutePath(), false);
|
||||
writer.deleteDocuments(new Term("id", id));
|
||||
writer.forceMergeDeletes(); // 强制删除
|
||||
writer.commit();
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user