代码之家  ›  专栏  ›  技术社区  ›  chouyangv3

索引设置中的Elasticsearch分析器没有影响

  •  0
  • chouyangv3  · 技术社区  · 2 年前

    我正试图在分析器的设置中将其设置为索引。

    // PUT /customers 
    {
      "mappings": {
        "properties": {
          "name": {
            "type": "text"
          },
          "title": {
            "type": "text"
          }
        },
        "dynamic": false
      },
      "settings": {
        "analysis": {
          "analyzer": "ik_smart"
        }
      }
    }
    

    然后我索引了一些数据。

    // POST /customers/_doc
    {
      "name": "张三",
      "title": "工程师"
    }
    
    // POST /customers/_doc
    {
      "name": "李四",
      "title": "测试员"
    }
    

    分析与 ik_smart 分析器

    //GET /customers/_analyze
    {
      "text": "李四工程师",
      "analyzer": "ik_smart"
    }
    
    // gets tokens ['李四', '工程师']
    

    使用默认分析器

    // GET /customers/_analyze
    {
      "text": "李四工程师"
    }
    
    // gets tokens ['李', '四', '工', '程', '师']
    

    最后搜索“·¨”

    // GET /customers/_search
    {
      "query": {
        "bool": {
          "must": {
            "multi_match": {
              "query": "李四工程师",
              "operator": "or",
              "type": "cross_fields",
              "fields": [
                "name^10",
                "title^7"
              ],
              "analyzer": "ik_smart"
            }
          }
        }
      }
    }
    
    // gets empty hits
    

    如果我把 ik_smart 分析仪进入每个字段

    // PUT /customers 
    {
      "mappings": {
        "properties": {
          "name": {
            "type": "text",
            "analyzer": "ik_smart"
          },
          "title": {
            "type": "text",
             "analyzer": "ik_smart"
          }
        },
        "dynamic": false
      }
    }
    

    那么请求就可以正常工作了。

    我怀疑第一个设置不会将分析器应用于索引中的任何字段。

    我用 ik_smart 这里的插件,它提供了一个 ik_smart 因为中文没有空格来分隔字符, ik_smart 是一个基于词典的分析器。没有 ik_smart 分析器,任何包含中文单词或句子的数据都将被索引为单个字符。因此,当我在搜索中使用默认分析器时,它也会起作用,因为短语 李四工程师 闯入 ['李', '四', 'å·¥', '程', '师'] ,并匹配索引数据。但这并不能提供非常准确的相关性。

    如果我使用 ik_smart 分析器在搜索时,我得到了令牌 ['李四', '工程师'] ,与索引数据不匹配。

    那么,为什么不是 settings.analysis.analyzer 按预期工作?

    如果此设置没有任何影响,那么它又有什么用呢?

    0 回复  |  直到 2 年前
        1
  •  1
  •   Amit    2 年前

    如果你愿意 ik_smart 要将其设置为索引的默认分析器,可以使用 default 分析器定义中的参数,如中所述 this 官方文件。

    PUT my-index-000001
    {
      "settings": {
        "analysis": {
          "analyzer": {
            "default": { --> Note this
              "type": "simple"
            }
          }
        }
      }
    }