feat: 系统启动自动创建索引并加载用户词典

This commit is contained in:
suwen 2021-02-07 08:56:58 +08:00
parent fe700e2dc1
commit 5fee803eec
3 changed files with 32 additions and 5 deletions

View File

@ -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<<<<<<<<<<<");
public void createIndex() {
ExecutorService executor = Executors.newSingleThreadExecutor();
CompletableFuture<String> future =
CompletableFuture.supplyAsync(
() -> {
System.out.println(">>>>>>>>> 开始创建索引 <<<<<<<<<<<");
luceneService.writeArticle(luceneService.getAllArticleLucene());
return GlobalResultGenerator.genSuccessResult("创建索引成功");
System.out.println(">>>>>>>>> 索引创建完毕 <<<<<<<<<<<");
System.out.println("加载用户配置的自定义扩展词典到主词库表");
try {
dicService.writeUserDic();
} catch (FileNotFoundException e) {
System.out.println("加载用户词典失败,未成功创建用户词典");
}
return "索引成功创建";
},
executor);
future.thenAccept(System.out::println);
}
/**

View File

@ -47,4 +47,10 @@ public interface UserDicService {
* @param userDic
*/
void updateDic(UserDic userDic);
/**
* 写入字典至内存
*
*/
void writeUserDic() throws FileNotFoundException;
}

View File

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