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);