zoukankan      html  css  js  c++  java
  • Elastic的字符串查找

    Elastic的字符串属性分成:keyword 和 text ,一般我们会把所有字符串设置为 keyword:

    默认字段属性的设置规则:

    PUT /test_idx
    {
      "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 1
      },
      "mappings": {
        "month": {
          "dynamic": "true",
          "dynamic_templates": [
            {
              "regex_template": {
                "match_pattern": "regex",
                "mapping": {
                  "type": "keyword"
                },
                "match": "^(?!.*?_time|is_.*|.*?_cnt|.*?_count|.*?_dbl|.*?_double|.*?_obj|.*?_object).*$"
              }
            },
            {
              "time_template": {
                "mapping": {
                  "format": "strict_date_time || yyyy-MM-dd HH:mm:ss || epoch_millis || epoch_second || yyyy-MM-dd",
                  "type": "date"
                },
                "match": "*_time"
              }
            },
            {
              "double_template": {
                "mapping": {
                  "type": "double"
                },
                "match": "*_dbl"
              }
            },
            {
              "double_template": {
                "mapping": {
                  "type": "double"
                },
                "match": "*_double"
              }
            },
            {
              "integer_template": {
                "mapping": {
                  "type": "integer"
                },
                "match": "is_*"
              }
            },
            {
              "long_template": {
                "mapping": {
                  "type": "integer"
                },
                "match": "*_cnt"
              }
            },
            {
              "long_template": {
                "mapping": {
                  "type": "integer"
                },
                "match": "*_count"
              }
            },
            {
              "object_template": {
                "mapping": {
                  "type": "object"
                },
                "match": "*_obj"
              }
            },
            {
              "object_template": {
                "mapping": {
                  "type": "object"
                },
                "match": "*_object"
              }
            }
          ]
        }
      }
    }  
    View Code

    总结一下(其实主要是围绕着是否查询关键词会被分词):

    1 对于 keyword 的字段,以下各个查找的意义:

    keyword + match : 全等匹配

    keyword + prefix : mysql的like 搜索 - 字符串不分词模糊匹配 - 左边没有%而右边有 (等同于 wildcard 的"关键词*")

    keyword + text :  查询会失败

    keyword + query_string : 完全匹配才会有结果

    keyword + wildcard : 如果*keyowrd* 则等价于MySQL的%keyword% ,如果 *keyword 等价于 %keyword 注意此刻右边是没有模糊百分号匹配的, keyword*同理, 如果wildcard 是没有*号的话,则等同于字符串完全匹配

    keyword + term:全等匹配

    2 对于 text 的字段,以下查找的意义:

    text + match:会被分词,比如“老虎”,会被分词查找

    text + term:查找的关键词会被分词,如果字段值有“老虎”这里使用“虎”查询会有结果,因为是“分词完全匹配”,而搜索“老虎”却没有结果,相当于Elastic里面存储的是“老”、“虎”所以没有结果

    text + prefix:查询“老虎”相当于是拿着这两个字组成的字符串,与text字段分词完全匹配,这里依赖于搜索字符串是否会被分词,与存储字段值的分词比对,会有很奇怪的结果

    总之使用 text 要慎重,由于你对分词逻辑无法控制,在需要做模糊搜索的时候,拿着字符串与里面的分词结果比对,就会有不可控的结果

    I can see a bigger world.
  • 相关阅读:
    Split Temporary Variable
    Introduce Explaining Variable
    Replace Temp with Query
    Extract Method
    自测代码的价值
    代码的坏味道
    Divergent Change & Shotgun Surgery
    Large Class
    Long Method
    Duplicated Code
  • 原文地址:https://www.cnblogs.com/xuweiqiang/p/14891011.html
Copyright © 2011-2022 走看看