修改源码实现连接数据库获取近义词汇
下载elasticsearch-analysis-dynamic-synonym打开项目(https://github.com/bells/elasticsearch-analysis-dynamic-synonym/tree/master)
引入依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!--或者oracle-->
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.3.0.0</version>
</dependency>
将pom文件中的版本修改成跟你的es版本号一样的,比如我的是7.2.0
使用不同的版本对应的lucene包不同,某些代码略有不同,根据编译器提示修改即可,下面的代码照常添加
<version>7.2.0</version>
? 在标签下添加 根据不同环境二选一
<!--添加mysql-connector依赖-->
<dependencySet>
<outputDirectory/>
<useProjectArtifact>true</useProjectArtifact>
<useTransitiveFiltering>true</useTransitiveFiltering>
<includes>
<include>mysql:mysql-connector-java</include>
</includes>
</dependencySet>
<!--将 oracle 的 jar 包打包进 zip 包-->
<dependencySet>
<outputDirectory/>
<useProjectArtifact>true</useProjectArtifact>
<useTransitiveFiltering>true</useTransitiveFiltering>
<includes>
<include>com.oracle.ojdbc:ojdbc8</include>
</includes>
</dependencySet>
在标签下添加(与标签同级)
<fileSets>
<fileSet>
<directory>${project.basedir}/config</directory>
<outputDirectory>config</outputDirectory>
</fileSet>
</fileSets>
在项目根目录下创建config/jdbc.properties文件,写入以下内容
# mysql
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=Asia/Shanghai
jdbc.username=账号
jdbc.password=密码
#近义词sql查询语句。(注意要以words字段展示)
synonym.word.sql=SELECT `keys` AS words FROM es_synonym WHERE ifdel = '0'
#获取近义词最后更新时间,用来判断是否发生了更新。(注意要以maxModitime词汇显示)
synonym.lastModitime.sql=SELECT MAX(moditime) AS maxModitime FROM es_synonym
interval=10
# 配置连接oracle数据库
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
jdbc.username=root
jdbc.password=123456
#近义词sql查询语句。(注意要以words字段展示)
synonym.word.sql=select SYNONYM_KEY as words from es_synonym where IS_DELETED = '0'
#获取近义词最后更新时间,用来判断是否发生了更新。(注意要以maxModitime词汇显示)
synonym.lastModitime.sql=SELECT MAX(UPD_TIME) AS maxModitime FROM es_synonym
interval=60
在com.bellszhu.elasticsearch.plugin.synonym.analysis包下,我们可以看到很多加载近义词汇的类,比如RemoteSynonymFile类就是通过接口来加载近义词词汇的。
? 我们在该包下创建类DynamicSynonymFromDb,同时继承SynonymFile接口,该类是用来读取数据库的近义词汇的,代码如下:
/**
* @author yandi
* @date 2023/06/15
*/
public class DynamicSynonymFromDb implements SynonymFile {
/**
* 配置文件名
*/
private final static String DB_PROPERTIES = "jdbc.properties";
private static Logger logger = LogManager.getLogger("dynamic-synonym");
private String format;
private boolean expand;
private boolean lenient;
private Analyzer analyzer;
private Environment env;
/**
* 动态配置类型
*/
private String location;
/**
* 作用类型
*/
private String group;
private long lastModified;
private Path conf_dir;
private JdbcConfig jdbcConfig;
DynamicSynonymFromDb(Environment env, Analyzer analyzer,
boolean expand, boolean lenient, String format, String location, String group) {
this.analyzer = analyzer;
this.expand = expand;
this.lenient = lenient;
this.format = format;
this.env = env;
this.location = location;
this.group = group;
// 读取配置文件
setJdbcConfig();
// 加载驱动
try {
Class.forName(jdbcConfig.getDriver());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// 判断是否需要加载
isNeedReloadSynonymMap()