实现词库热更新两种方式
1、远程词库,2、修改源码整合数据库
要求:(前提安装ik词库。下载复制到plugin,重启即可)
1、改http请求需要返回两个头部header(Last-Modified、ETag
),这两个词都是字符串类型,只要一个发生变化,改插件就会去抓取新词
2、换行符\n
一分钟执行一次API文章来源:https://www.toymoban.com/news/detail-604527.html
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
@RestController
@RequestMapping(value = "/extendApi")
public class ExtendController {
@RequestMapping("/getThesaurus")
public void getThesaurus(HttpServletResponse response) throws IOException {
File file = new File("D:\\EdgeDownLoad\\elasticsearch-7.8.0-windows-x86_64\\elasticsearch-7.8.0\\plugins\\analysis-ik\\config\\ext.dic");
FileInputStream inputStream = new FileInputStream(file);
//缓冲
int offset = 0;
byte[] buffer = new byte[(int) file.length()];
response.setHeader("Last-Modified",String.valueOf(buffer.length));
response.setHeader("ETag",String.valueOf(buffer.length));
while(inputStream.read(buffer, offset,(int)file.length()-1)!=-1){
}
OutputStream outputStream = response.getOutputStream();
outputStream.write(buffer);
outputStream.close();
inputStream.close();
}
}
配置远程词库(“D:\EdgeDownLoad\elasticsearch-7.8.0-windows-x86_64\elasticsearch-7.8.0\plugins\analysis-ik\config\IKAnalyzer.cfg.xml”)文章来源地址https://www.toymoban.com/news/detail-604527.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict">ext.dic</entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords"></entry>
<!--用户可以在这里配置远程扩展字典 -->
<entry key="remote_ext_dict">http://localhost:9091/extendApi/getThesaurus</entry>
<!--用户可以在这里配置远程扩展停止词字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
到了这里,关于ES 词库热更新(实现方式一:远程词库)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!