From 5fee803eec3a3430392f8a510f2d474788005752 Mon Sep 17 00:00:00 2001 From: suwen <577014284@qq.com> Date: Sun, 7 Feb 2021 08:56:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=B3=BB=E7=BB=9F=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=88=9B=E5=BB=BA=E7=B4=A2=E5=BC=95=E5=B9=B6?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E7=94=A8=E6=88=B7=E8=AF=8D=E5=85=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lucene/api/LuceneSearchController.java | 28 ++++++++++++++++--- .../forest/lucene/service/UserDicService.java | 6 ++++ .../service/impl/UserDicServiceImpl.java | 3 +- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/rymcu/forest/lucene/api/LuceneSearchController.java b/src/main/java/com/rymcu/forest/lucene/api/LuceneSearchController.java index 3afa553..76220b9 100755 --- a/src/main/java/com/rymcu/forest/lucene/api/LuceneSearchController.java +++ b/src/main/java/com/rymcu/forest/lucene/api/LuceneSearchController.java @@ -7,12 +7,17 @@ import com.rymcu.forest.core.result.GlobalResultGenerator; import com.rymcu.forest.dto.ArticleDTO; import com.rymcu.forest.lucene.model.ArticleLucene; import com.rymcu.forest.lucene.service.LuceneService; +import com.rymcu.forest.lucene.service.UserDicService; import com.rymcu.forest.util.Utils; import org.springframework.web.bind.annotation.*; import javax.annotation.PostConstruct; import javax.annotation.Resource; +import java.io.FileNotFoundException; import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; /** * LuceneSearchController @@ -25,12 +30,27 @@ import java.util.List; public class LuceneSearchController { @Resource private LuceneService luceneService; + @Resource private UserDicService dicService; @PostConstruct - public GlobalResult createIndex() { - System.out.println(">>>>>>>>>init index<<<<<<<<<<<"); - luceneService.writeArticle(luceneService.getAllArticleLucene()); - return GlobalResultGenerator.genSuccessResult("创建索引成功"); + public void createIndex() { + ExecutorService executor = Executors.newSingleThreadExecutor(); + CompletableFuture future = + CompletableFuture.supplyAsync( + () -> { + System.out.println(">>>>>>>>> 开始创建索引 <<<<<<<<<<<"); + luceneService.writeArticle(luceneService.getAllArticleLucene()); + System.out.println(">>>>>>>>> 索引创建完毕 <<<<<<<<<<<"); + System.out.println("加载用户配置的自定义扩展词典到主词库表"); + try { + dicService.writeUserDic(); + } catch (FileNotFoundException e) { + System.out.println("加载用户词典失败,未成功创建用户词典"); + } + return "索引成功创建"; + }, + executor); + future.thenAccept(System.out::println); } /** diff --git a/src/main/java/com/rymcu/forest/lucene/service/UserDicService.java b/src/main/java/com/rymcu/forest/lucene/service/UserDicService.java index e27c322..beeade2 100644 --- a/src/main/java/com/rymcu/forest/lucene/service/UserDicService.java +++ b/src/main/java/com/rymcu/forest/lucene/service/UserDicService.java @@ -47,4 +47,10 @@ public interface UserDicService { * @param userDic */ void updateDic(UserDic userDic); + + /** + * 写入字典至内存 + * + */ + void writeUserDic() throws FileNotFoundException; } diff --git a/src/main/java/com/rymcu/forest/lucene/service/impl/UserDicServiceImpl.java b/src/main/java/com/rymcu/forest/lucene/service/impl/UserDicServiceImpl.java index 5587560..c59a1e9 100644 --- a/src/main/java/com/rymcu/forest/lucene/service/impl/UserDicServiceImpl.java +++ b/src/main/java/com/rymcu/forest/lucene/service/impl/UserDicServiceImpl.java @@ -51,7 +51,8 @@ public class UserDicServiceImpl implements UserDicService { writeUserDic(); } - private void writeUserDic() { + @Override + public void writeUserDic() { try { String filePath = "lucene/userDic/"; File file = new File(filePath);