• ElasticSearch中keyword和text类型区别和模糊查询


    参考文章:
    https://blog.csdn.net/sfh2018/article/details/118083634
    https://blog.csdn.net/w1014074794/article/details/119643883

    text和keyword类型介绍

    • ES5.0及以后的版本取消了string类型,将原先的string类型拆分为textkeyword两种类型。它们的区别在于text会对字段进行分词处理而keyword则不会进行分词
      也就是说如果字段是text类型,存入的数据会先进行分词,然后将分完词的词组存入索引,而keyword则不会进行分词,直接存储。
    • text类型的数据被用来索引长文本,例如电子邮件主体部分或者一款产品的介绍,这些文本会被分析,在建立索引文档之前会被分词器进行分词,转化为词组。经过分词机制之后es允许检索到该文本切分而成的词语,但是text类型的数据不能用来过滤、排序和聚合等操作
    • keyword类型的数据可以满足电子邮箱地址、主机名、状态码、邮政编码和标签等数据的要求,不进行分词,常常被用来过滤、排序和聚合。

    elasticsearch如何对text字段进行精确匹配?

    同字段多type配置

    创建索引,在mapping中通过fields关键字给city字段添加别名raw,类型为keyword,用来做精确匹配以及排序。

    • 创建索引
    PUT test_index03
    {
      "mappings": {
        "properties": {
          "name": {
            "type": "keyword"
          },
          "city": {
            "type": "text",
            "fields": {
              "raw": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 添加数据
    PUT /test_index03/_doc/1
     {
      "name" : "叶子在这儿",
      "city" : "陕西省西安市长安区"
     }
    PUT /test_index03/_doc/2
     {
       "name":"北京的小家",
       "city":"北京市昌平区回龙观街道"
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 精确查询(使用别名可以精确查询了)
    GET /test_index03/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "city.raw": {
                  "value": "陕西省西安市长安区"
                }
              }
            }
          ]
        }
      },
      "sort": {
        "city.raw": "asc"
      },
      "aggs": {
        "Cities": {
          "terms": {
            "field": "city.raw"
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    同字段多分词规则配置

    字段text,默认采用standard analyzer分词器;
    通过fields声明别名english,采用english分词器。

    PUT test_index04
    {
      "mappings": {
        "properties": {
          "city": { 
            "type": "text",
            "fields": {
              "english": { 
                "type":     "text",
                "analyzer": "english"
              }
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 添加数据
    PUT test_index03/_doc/1
    { "text": "quick brown fox" } 
    
    PUT test_index03/_doc/2
    { "text": "quick brown foxes" } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 查询

    利用multi_match多字段匹配查询,实现一个字段多种分词规则检索。

    GET /test_index03/_search
    {
      "query": {
        "multi_match": {
          "query": "quick brown foxes",
          "fields": [
            "text",
            "text.english"
          ],
          "type": "most_fields"
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    CLION openCL C/C++环境配置(两个坑)
    java计算机毕业设计web家庭财务管理系统源码+mysql数据库+系统+lw文档+部署
    idea清空缓存类
    css3中有哪些伪选择器?
    半导体晶片切割
    Redis的主从复制
    Makefile第十二课:Makefile动态库
    [附源码]计算机毕业设计JAVAjsp海纳装修报价管理系统
    【Linux】安装配置虚拟机及虚拟机操作系统的安装
    2022-08-26 Unity视频播放1——视频介绍
  • 原文地址:https://blog.csdn.net/weixin_43824520/article/details/126860414