美文网首页Springboot 系列
SpringBoot+ElasticSearch实战系列02 高

SpringBoot+ElasticSearch实战系列02 高

作者: 方才兄 | 来源:发表于2020-10-21 17:14 被阅读0次

    原文链接:中文+拼音混合检索,并高亮显示

    本文导读
    本文仿照QQ的用户搜索,搭建一个中文+拼音的混合检索系统,并高亮显示检索字段。全文共分为以下几部分:

    • 1、项目简介,包括需求描述与分析等;
    • 2、项目开发,通过两个版本的index,验证并完成需求;
    • 3、从分词和高亮原理入手,深度分析高亮显示问题;
    • 4、SpringBoot+RestHighLevelClient 完成项目开发。

    【ps:留言区附完整版项目源码地址 SpringBoot + ElasticSearch】

    01 项目简介

    本项目基于ElasticSearch 7.7.1,analysis-pinyin 7.7.1,参考QQ的用户搜索效果,完成一个中文+拼音的混合检索系统。(ElasticSearch的安装请参考在docker中安装ES

    1.1 检索场景示例

    中文+首字母+全拼检索

    其实QQ的用户检索是有很多限制的,比如说首字母检索时,必须从第一个字开始匹配【输入“gz”,可以检索到“关注我”,但是不能检索到“我关注”】;

    再比如说全拼+首字母检索时,全拼必须在前面【输入“guanz”,可以检索到“关注我”,但是输入“gzhu”,是不能检索到结果的】;

    至于为什么会有如此限制,个人猜测是考虑检索性能(PS:欢迎留言讨论)。

    1.2 检索需求描述

    参考QQ,列出“用户检索系统”的需求如下:

    • 1)支持首字母检索;
    • 2)支持首字母+全拼检索;
    • 3)支持中文+首字母+全拼混合检索;
    • 4)检索词有中文,则必须包含;
    • 5)高亮显示检索命中词。

    1.3 需求分析

    从需求1,可知,需要建立【首字母的倒排索引】;

    从需求2,可知,需要建立【全拼的倒排索引】;

    02 项目开发

    2.1 第一个版本

    根据上面的分析,参考 analysis-pinyin 官网,创建了第一版index:

    ps:关于 analysis-pinyin 各个配置项的含义可参考官网
    PUT /user_index/
    {
      "settings": {
        "index": {
          "number_of_shards": 1,
          "number_of_replicas": 1
        },
        "analysis": {
          "analyzer": {
            "pinyin_analyzer": {
              "tokenizer": "my_pinyin"
            }
          },
          "tokenizer": {
            "my_pinyin": {
              "type": "pinyin",
              "keep_first_letter": true,
              "keep_separate_first_letter": true,
              "keep_full_pinyin": true,
              "keep_original": false,
              "limit_first_letter_length": 16,
              "lowercase": true
            }
          }
        }
      },
      "mappings": {
        "dynamic": false,
        "properties": {
          "nickName": {
            "type": "keyword",
            "fields": {
              "pinyin": {
                "type": "text",
                "store": false,
                "analyzer": "pinyin_analyzer"
              }
            }
          }
        }
      }
    }
    

    使用_analyze接口,看下分词效果:

    GET user_index/_analyze
    {
      "field": "nickName.pinyin",
      "text": [
        "关注我"
      ]
    }
    ​
    # 结果如下:
    {
      "tokens" : [
        {
          "token" : "g",
          "start_offset" : 0,
          "end_offset" : 0,
          "type" : "word",
          "position" : 0
        },
        {
          "token" : "guan",
          "start_offset" : 0,
          "end_offset" : 0,
          "type" : "word",
          "position" : 0
        },
        {
          "token" : "gzw",
          "start_offset" : 0,
          "end_offset" : 0,
          "type" : "word",
          "position" : 0
        },
        {
          "token" : "z",
          "start_offset" : 0,
          "end_offset" : 0,
          "type" : "word",
          "position" : 1
        },
        {
          "token" : "zhu",
          "start_offset" : 0,
          "end_offset" : 0,
          "type" : "word",
          "position" : 1
        },
        {
          "token" : "w",
          "start_offset" : 0,
          "end_offset" : 0,
          "type" : "word",
          "position" : 2
        },
        {
          "token" : "wo",
          "start_offset" : 0,
          "end_offset" : 0,
          "type" : "word",
          "position" : 2
        }
      ]
    }
    

    一切都ok,好像能满足需求,插入几条数据,验证下:

    POST _bulk
    {"index":{"_index":"user_index","_id":"1"}}
    {"nickName":"关注我"}
    {"index":{"_index":"user_index","_id":"2"}}
    {"nickName":"我关注"}
    {"index":{"_index":"user_index","_id":"3"}}
    {"nickName":"系统学ES就关注我"}
    {"index":{"_index":"user_index","_id":"4"}}
    {"nickName":"系统学ES"}
    

    试试检索效果:

    GET /user_index/_search
    {
      "query": {
        "match_phrase": {
          "nickName.pinyin": "guanz我"
        }
      }
    }
    结果如下:
        "hits" : [
          {
            "_index" : "user_index",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 1.9991971,
            "_source" : {
              "nickName" : "关注我"
            }
          },
          {
            "_index" : "user_index",
            "_type" : "_doc",
            "_id" : "3",
            "_score" : 1.4875543,
            "_source" : {
              "nickName" : "系统学ES就关注我"
            }
          }
        ]
    

    经过测试,发现是可以满足需求1、2、3的(有兴趣的小伙伴可以自己试试哟)。

    但别忘了,我们还有需求4和5,关于需求4,可以简单的使用 post_filter 后置过滤完成需求。

    对于高亮显示,ES本身是提供了 highlight 语法的,写个DSL验证一下:

    # 检索语句
    GET /user_index/_search
    {
      "query": {
        "match_phrase": {
          "nickName.pinyin": "guanz我"
        }
      },
      "highlight": {
        "fields": {
          "nickName.pinyin": {}
        }
      }
    }
    # 部分结果     
          {
            "_index" : "user_index",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 1.9991971,
            "_source" : {
              "nickName" : "关注我"
            },
            "highlight" : {
              "nickName.pinyin" : [
                "<em></em><em></em><em></em>关注我"
              ]
            }
          }
    

    发现居然没办法高亮!这可不行呀,这么简单的需求,必须实现了!

    通过阅读 ES官方文档 + 不断尝试,终于找到原因,完美解决。

    解决方案请阅读:

    中文+拼音混合检索,并高亮显示​mp.weixin.qq.com

    相关文章

      网友评论

        本文标题:SpringBoot+ElasticSearch实战系列02 高

        本文链接:https://www.haomeiwen.com/subject/lvsemktx.html