From 91796ade6735720453b7916e4a97ffe6c8de5dd2 Mon Sep 17 00:00:00 2001 From: ronger Date: Tue, 4 Jan 2022 22:04:55 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E6=96=B0=E7=94=A8=E6=88=B7=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E6=8F=90=E7=A4=BA=E6=9C=8D=E5=8A=A1=E5=99=A8=E5=BC=80?= =?UTF-8?q?=E5=B0=8F=E5=B7=AE=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../forest/lucene/util/UserIndexUtil.java | 124 ++++++++++-------- 1 file changed, 67 insertions(+), 57 deletions(-) diff --git a/src/main/java/com/rymcu/forest/lucene/util/UserIndexUtil.java b/src/main/java/com/rymcu/forest/lucene/util/UserIndexUtil.java index 884aac6..9386324 100644 --- a/src/main/java/com/rymcu/forest/lucene/util/UserIndexUtil.java +++ b/src/main/java/com/rymcu/forest/lucene/util/UserIndexUtil.java @@ -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(); + } + } + }); + } }