ElasticSearch7.17.5版本热更新同义词,扩展词停止词

2023-11-05 20:59

本文主要是介绍ElasticSearch7.17.5版本热更新同义词,扩展词停止词,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

ElasticSearch7.17.5热更新同义词及自定义ik分词器,集成SpringData4.3.4使用

  • 从ElasticSearch部署同义词热更新踩坑出来,写个博客记录一下
    • ik分词器
    • 同义词
    • SpringData集成

从ElasticSearch部署同义词热更新踩坑出来,写个博客记录一下

ElasticSearch版本更新得很快,对于同义词热更新官方版本只到5.1.1版本,这里是做了一个适用于7.16.3版本以上的插件,测试发现7.17.5也兼容。
ik分词器源码修改是参考以下博客实现:https://blog.csdn.net/zq199419951001/article/details/89884461

修改后的同义词代码和ik分词器源码如下,ik可从官网下载对应的版本,照着文档改写
ik分词器7.17.5源码下载地址:https://download.csdn.net/download/qq_41927845/87254019
7.17.5同义词源码:https://download.csdn.net/download/qq_41927845/87253959

ik分词器

参考代码的实现比较完善,需要修改的地方是config目录增加一个db.properties文件:
在这里插入图片描述
在这里插入图片描述
然后在Dictionary类下面修改
在这里插入图片描述
在这里插入图片描述

private final static  String FILE_NAME = "IKAnalyzer.cfg.xml";private final static  String EXT_DICT = "ext_dict";private final static  String REMOTE_EXT_DICT = "remote_ext_dict";private final static  String EXT_STOP = "ext_stopwords";private final static  String REMOTE_EXT_STOP = "remote_ext_stopwords";private final static  String DB_PROPERTIES="db.properties";private Path conf_dir;private Properties props;private Properties myProperties;private Dictionary(Configuration cfg) {this.configuration = cfg;this.props = new Properties();this.myProperties=new Properties();this.conf_dir = cfg.getEnvironment().configFile().resolve(AnalysisIkPlugin.PLUGIN_NAME);Path configFile = conf_dir.resolve(FILE_NAME);Path myFile = cfg.getConfigInPluginDir().resolve(DB_PROPERTIES);InputStream input = null;InputStream myInput = null;File file = myFile.toFile();logger.info("file文件:" + file);try {myInput = new FileInputStream(file);} catch (FileNotFoundException e1) {logger.error("db.properties未找到", e1);}try {logger.info("try load config from {}", configFile);input = new FileInputStream(configFile.toFile());} catch (FileNotFoundException e) {conf_dir = cfg.getConfigInPluginDir();configFile = conf_dir.resolve(FILE_NAME);try {logger.info("try load config from {}", configFile);input = new FileInputStream(configFile.toFile());} catch (FileNotFoundException ex) {// We should report origin exceptionlogger.error("ik-analyzer", e);}}if (input != null) {try {props.loadFromXML(input);} catch (IOException e) {logger.error("ik-analyzer", e);}}try {myProperties.load(myInput);} catch (IOException e) {logger.error("加载db.properties文件失败!", e);}}

增加加载数据库代码并自动加载字典

private String getUrl() {String url = myProperties.getProperty("url");return url;}private String getUser() {String user = myProperties.getProperty("user");return user;}private String getPassword() {String password = myProperties.getProperty("password");return password;}private int getInterval() {Integer interval = Integer.valueOf(myProperties.getProperty("interval"));return interval;}private String getExtWordSql() {String extWordSql = myProperties.getProperty("extWordSql");return extWordSql;}private String getStopWordSql() {String stopWordSql = myProperties.getProperty("stopWordSql");return stopWordSql;}private void loadMySQLExtDict() {Connection conn = null;Statement stmt = null;ResultSet rs = null;try {logger.info("query ext dict from mysql, " + getUrl());Class.forName("com.mysql.cj.jdbc.Driver");conn = DriverManager.getConnection(getUrl(), getUser(), getPassword());stmt = conn.createStatement();String extWordSql = getExtWordSql();if(extWordSql!=null && extWordSql!=""){rs = stmt.executeQuery(extWordSql);while (rs.next()) {String theWord = rs.getString("main_keyword");logger.info("main_keyword ext word from mysql: " + theWord);_MainDict.fillSegment(theWord.trim().toCharArray());}}} catch (Exception e) {logger.error("erorr", e);} finally {if (rs != null) {try {rs.close();} catch (SQLException e) {logger.error("error", e);}}if (stmt != null) {try {stmt.close();} catch (SQLException e) {logger.error("error", e);}}if (conn != null) {try {conn.close();} catch (SQLException e) {logger.error("error", e);}}}}private void loadMySQLStopDict() {Connection conn = null;Statement stmt = null;ResultSet rs = null;try {logger.info("query stop dict from mysql, " + getUrl());Class.forName("com.mysql.cj.jdbc.Driver");conn = DriverManager.getConnection(getUrl(), getUser(), getPassword());stmt = conn.createStatement();String stopWordSql = getStopWordSql();if(stopWordSql!=null && stopWordSql!=""){rs = stmt.executeQuery(stopWordSql);while (rs.next()) {String theWord = rs.getString("main_keyword");logger.info("main_keyword stop word from mysql: " + theWord);_StopWords.fillSegment(theWord.trim().toCharArray());}}} catch (Exception e) {logger.error("erorr", e);} finally {if (rs != null) {try {rs.close();} catch (SQLException e) {logger.error("error", e);}}if (stmt != null) {try {stmt.close();} catch (SQLException e) {logger.error("error", e);}}if (conn != null) {try {conn.close();} catch (SQLException e) {logger.error("error", e);}}}}public void reLoadMySqlDict() {logger.info("重新加载远程词典...");// 新开一个实例加载词典,减少加载过程对当前词典使用的影响Dictionary tmpDict = new Dictionary(configuration);tmpDict.configuration = getSingleton().configuration;tmpDict.loadMainDict();tmpDict.loadStopWordDict();tmpDict.loadMySQLExtDict();tmpDict.loadMySQLStopDict();_MainDict = tmpDict._MainDict;_StopWords = tmpDict._StopWords;logger.info("重新加载远程词典完毕...");}

如果保留默认的ik分词器,则给这个ik分词器重新命名
修改pom.xml文件elasticsearch.plugin.name命名,并且es的版本应与服务器一致
需要在pom文件增加mysql驱动文件
在这里插入图片描述
在这里插入图片描述

		<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.13</version></dependency>

修改plugin.xml
引入mysql驱动 在这里插入图片描述

<include>mysql:mysql-connector-java</include>

在AnalysisIkPlugin类修改分析器名称,PLUGIN_NAME需要和pom文件修改的地方保持一致
在这里插入图片描述
至此插件修改完毕,选择mvn clear,再compile package打包
将target/release目录下的压缩包复制,在es的安装目录下的plugin文件夹创建一个文件夹命名为ik-custom,粘贴至此处解压,解压完成后删除压缩包文件,注意看是否引入mysql驱动包
在这里插入图片描述

同义词

由于官网最新的同义词插件版本为5.1.1,这里是改了一个适用于7.16.3及7.17.5版本的数据
下载后使用idea打开项目
首先还是在源码的config文件夹中配置数据库及查询同义词,同义词更新版本的数据,每次同义词版本大于插件记录的版本就会加载同义词,频率是60秒扫描一次,具体使用可查看README.pd文件
在这里插入图片描述
修改pom文件中的es版本
在这里插入图片描述
就可以mvn clear,再compile package打包
将target/release目录下的压缩包复制,在es的安装目录下的plugin文件夹创建一个文件夹命名为ik-custom,粘贴至此处解压,解压完成后删除压缩包文件,注意看是否引入mysql驱动包(同以上一致)

打开es的日志,可看到同义词的加载结果
在这里插入图片描述

SpringData集成

这里要注意的是,springdata 和es有版本限制
在这里插入图片描述
我这边用到的springboot是2.6.8,es是7.17.5,暂时选择的springdata版本是4.3.4,目前还没发现什么问题,最好的是选择4.4.x版本

		<dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-elasticsearch</artifactId><version>4.3.4</version></dependency>

配置es信息
在这里插入图片描述

elasticsearch:rest:uris: http://localhost:9200username: password: jackson:date-format: yyyy-MM-dd HH:mm:ss

加载同义词分析器:在项目resource目录下创建一个文件夹elasticsearch,建一个es.json文件,这里可以设置分析器用自定义的分词器,如ik_smart_custom
在这里插入图片描述

{"index" : {"analysis" : {"analyzer" : {"synonym" : {"tokenizer" : "ik_smart","filter" : ["remote_synonym"]}},"filter" : {"remote_synonym" : {"type" : "dynamic_synonym","synonyms_path" : "fromMySql","interval": 60},"local_synonym" : {"type" : "dynamic_synonym","synonyms_path" : "synonym.txt"},"synonym_graph" : {"type" : "dynamic_synonym_graph","synonyms_path" : "http://host:port/synonym.txt"}}}}
}

创建实体类,指定setting
在这里插入图片描述
这里是指定了索引名为dynamic1,需要注意的是参数设置了synonym同义词分析器才能使用同义词查询

创建dao层文件夹,继承ElasticsearchRepository
在这里插入图片描述
注意框起来的值必须和实体类的id类型一致

接下来创建controller即可测试
在这里插入图片描述
测试结果如下:
新增es文档数据
在这里插入图片描述

同义词查询:我这边配置了同义词组为 我爱,高德,书包,方法
在这里插入图片描述

可以看到查询效果

这篇关于ElasticSearch7.17.5版本热更新同义词,扩展词停止词的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/352482

相关文章

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

科研绘图系列:R语言扩展物种堆积图(Extended Stacked Barplot)

介绍 R语言的扩展物种堆积图是一种数据可视化工具,它不仅展示了物种的堆积结果,还整合了不同样本分组之间的差异性分析结果。这种图形表示方法能够直观地比较不同物种在各个分组中的显著性差异,为研究者提供了一种有效的数据解读方式。 加载R包 knitr::opts_chunk$set(warning = F, message = F)library(tidyverse)library(phyl

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

AI行业应用(不定期更新)

ChatPDF 可以让你上传一个 PDF 文件,然后针对这个 PDF 进行小结和提问。你可以把各种各样你要研究的分析报告交给它,快速获取到想要知道的信息。https://www.chatpdf.com/

GIS图形库更新2024.8.4-9.9

更多精彩内容请访问 dt.sim3d.cn ,关注公众号【sky的数孪技术】,技术交流、源码下载请添加微信:digital_twin123 Cesium 本期发布了1.121 版本。重大新闻,Cesium被Bentley收购。 ✨ 功能和改进 默认启用 MSAA,采样 4 次。若要关闭 MSAA,则可以设置scene.msaaSamples = 1。但是通过比较,发现并没有多大改善。

Spring框架5 - 容器的扩展功能 (ApplicationContext)

private static ApplicationContext applicationContext;static {applicationContext = new ClassPathXmlApplicationContext("bean.xml");} BeanFactory的功能扩展类ApplicationContext进行深度的分析。ApplicationConext与 BeanF