Elasticsearch 使用同义词 二
阅读原文时间:2021年04月22日阅读:1

接着上篇来,上篇其实里面有一些隐藏的问题:

比如说: 水果手机,苹果手,苹果手机,iphone,最好用的手机 => iphone手机

我想要在搜索苹果手机的时候能够得到汇总的结果是:iPhone手机

但是在我们的实际使用中,始终得不到这样的结构。原因如下:

同义词 --->> 获取同义词  :要求的是每个词都必须在分词里面是存在的,分词 是搜索引擎里面很重要的一个前提条件,这也是一样的,假如我们的分词器 IK,或者 ansj 默认的词库没有:水果手机 这个词的话,那么搜索同义词是就匹配不到:iphone手机,需要你将水果手机作为一个完整的词,需要配置进ElasticSearch自定义词库。查看怎么配置

坑也填完了,接下来我们实现兼容本地词库以及远程词库。

第二种方式远程文件配置

首先我们准备一个可访问的本地接口服务,这是JAVA后端,使用springboot 

package com.zdx.word.interfaces.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Class SynonymController
 * @Author 作者姓名:刘兴
 * @Version 1.0
 * @Date 创建时间:2019-01-17 14:29
 * @Direction 类说明
 */
@RequestMapping
@Controller
public class SynonymController {

    public static final String ETag = "ETag" ;
    public static final String lastModified = "Last-Modified" ;
    public static int version = 0 ;

    @GetMapping("/synonym")
    @ResponseBody
    public String wordSynonym(HttpServletRequest request, HttpServletResponse response) {

        //业务操作,比如有新的同义词过来了,我们需要变更版本,然后Elasticsearch 的定时器得到版本变动了,才会将数据视为更新数据
        if( true ){
            version += 1 ;
        }

        //返回必须要带下面的参数
        response.setHeader( ETag ,version + "" );
        response.setHeader( lastModified ,version + "" );
        response.setContentType("text/plain");

        return "管廊,地下管廊,地铁管廊,高铁管廊,隧道管廊 => 通用管廊" ;
    }



}

访问地址:http://localhost:8080/synonym

下面是设置远程调用接口, 本地同义词的配置Elastic端的代码:使用Kibana 

 PUT /megacorp
{
  "mappings": {
    "mysynonym": {
      "properties": {
        "name":{
          "type": "text",
          "analyzer": "ik-index",  //指定索引时候用的解析器
          "search_analyzer": "ik-smart" //指定搜索时候用的解析器
        }
      }
    }
  }
  ,
  "settings": {
    "analysis": {
      "filter": {
        "remote_synonym": {
            "type" : "dynamic_synonym",
            "synonyms_path" : "http://localhost:8080/synonym",
            "interval": 60 // 没60s调取一次接口
        },
        "local_synonym" : {
            "type" : "dynamic_synonym",
            "synonyms_path" : "analysis/synonyms.txt"  
        }
      },
      "analyzer": {
        "ik-index": {
          "type": "custom",
          "tokenizer": "ik_max_word",
          "filter": [ 
              "remote_synonym", //对远程同义词进行了过滤
              "local_synonym"   //对本地同义词进行了过滤
           ]
        },
        "ik-smart": {
          "type": "custom",
          "tokenizer": "ik_smart",
          "filter": [
              "local_synonym"
           ]
        }
      }
    }
  }
}

我们要查一下,我的远程接口返回的数据shif是否会自动加载至同义词的接口中:

GET /megacorp/_analyze
{
  "analyzer": "ik-index",
  "text": "美女"
}

翻车了,远程的这个有问题 ------》》》》》》》

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器