采用analysis-dynamic-synonym插件,动态接口的方式同步同义词

1、采用analysis-dynamic-synonym插件,动态接口的方式同步同义词

大致原理 -> 动态接口:
1、采用外部URL请求的方式,将同义词配置同步到es。
2、定时轮询,当发现接口返回版本发生变化时,则会触发自动更新;

  • 下载elasticsearch-7.10.0版本,并解压:https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-10-0

  • 下载IK分词器7.10.0版本,并解压到指定文件夹下(elasticsearch-7.10.0/plugins/),文件夹改名为ik:https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.10.0
    采用analysis-dynamic-synonym插件,动态接口的方式同步同义词_第1张图片

  • 下载elasticsearch-analysis-dynamic-synonym 插件代码: https://github.com/bells/elasticsearch-analysis-dynamic-synonym

  • 打开代码中pom文件,将版本号改为和ES一致,然后执行maven命令:clean、compile、package
    采用analysis-dynamic-synonym插件,动态接口的方式同步同义词_第2张图片

  • 将打包好的jar包,复制粘贴到指定文件夹(elasticsearch-7.10.0/plugins/),解压并改名为“dynamic-synonym”

  • 将打包好的jar包,复制粘贴到指定文件夹(elasticsearch-7.10.0/plugins/),解压并改名为“dynamic-synonym”
    采用analysis-dynamic-synonym插件,动态接口的方式同步同义词_第3张图片

  • 新建服务(synonym-update),将动态接口定义好,例如:

package com.synonym.demo.controller;
 
import com.synonym.demo.dao.SynonymMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
 
@RestController
public class TestController {
 
    @Autowired
    private SynonymMapper synonymMapper;
 
    @GetMapping("/synonym")
    public String synonym (HttpServletRequest request, HttpServletResponse response) {
        String result = "";
        String eTag = request.getHeader("If-None-Match") == null ? "" : request.getHeader("If-None-Match");
        long eTagVersion = request.getHeader("If-Modified-Since") == null ? -1L : Long.parseLong(request.getHeader("If-Modified-Since"));
        // 读取数据库,获取数据库中当前同义词版本号 (在数据库中维护一个版本号,当同义词变更时,变更它。示例代码取的是表最后更新的日期)
        Date date = synonymMapper.queryVersion();
        long dbVersion = date.getTime();
        if (dbVersion > eTagVersion){
            List<String> docList = synonymMapper.querySynonymList();
            StringBuilder words = new StringBuilder();
            for (String doc : docList) {
                words.append(doc);
                words.append(System.getProperty("line.separator"));
            }
            eTagVersion = dbVersion;
            result = words.toString();
        }
        //更新时间
        response.setHeader("ETag", eTag);
        response.setHeader("Last-Modified", eTagVersion + "");
        response.setHeader("Content-Type", "text/plain");
        return result;
    }
}
package com.synonym.demo.dao;
 
import org.apache.ibatis.annotations.Mapper;
 
import java.util.Date;
import java.util.List;
 
@Mapper
public interface SynonymMapper {
 
    /**
     * 获取同义词最新版本
     *
     * @return
     */
    Date queryVersion();
 
    /**
     * 获取同义词数据集合
     *
     * @return
     */
    List<String> querySynonymList();
 
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.synonym.demo.dao.SynonymMapper">
    <select id="queryVersion" resultType="java.util.Date">
        select update_time from tb_test order by update_time desc limit 1;
    </select>
 
    <select id="querySynonymList" resultType="java.lang.String">
        select name from tb_test;
    </select>
</mapper>
CREATE TABLE `tb_test` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',
  `name` varchar(500) NOT NULL DEFAULT '0' COMMENT '名字',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COMMENT='测试表';
  • 配置文件内容
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/elasticsearch_test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=GMT%2B8
    username: root
    password: root
 
mybatis:
  # classpath 路径下,
  mapper-locations: classpath:mapper/**/*Mapper.xml
  type-aliases-package: com.synonym.demo
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  • 服务搭建完成后,运行起来,并在数据库里初始化一部分数据
  • 接下来我们启动es,再运行es-head插件,进入可视化页面,执行创建索引操作,并指定同义词文件配置
PUT http://localhost:9200/test04_index/
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0,
    "index": {
      "analysis": {
        "filter": {
          "synonym_graph": {
            "type": "dynamic_synonym_graph",
            "synonyms_path": "http://127.0.0.1:8080/synonym",
            "interval": 30
          }
        },
        "analyzer": {
          "my_doc_syno": {
            "tokenizer": "ik_smart",
            "filter": [
              "synonym_graph"
            ]
          }
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "my_doc_syno"
      },
      "remark": {
        "type": "text",
        "analyzer": "my_doc_syno"
      }
    }
  }
}
  • 此时可以看到正确返回
  • 在创建索引完成后,可看到数据库的同义词已经加载到es中了
  • 我们可以在数据库中新增一条同义词配置,可以发现在下次定时任务触发时,能够把最新的配置同步到es中
    采用analysis-dynamic-synonym插件,动态接口的方式同步同义词_第4张图片
  • 接下来我们进入到验证环节,看是否同义词设置成功
POST http://localhost:9200/test04_index/_analyze/
{
  "field": "name",
  "text": "双十一,iphone,小米"
}

采用analysis-dynamic-synonym插件,动态接口的方式同步同义词_第5张图片
数据验证步骤,同第一种方式(传送门:https://blog.csdn.net/u012888052/article/details/125016668?spm=1001.2014.3001.5502)

你可能感兴趣的:(瞎写,es,elasticsearch,搜索引擎,java)