V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
swordwinter
V2EX  ›  问与答

ElasticSearch mapping type 从 keyword 变成了 text

  •  
  •   swordwinter · 2018-09-20 12:34:45 +08:00 · 2798 次点击
    这是一个创建于 2044 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有人知道这会是什么造成的吗? index 里有个字段的 mapping type 从 keyword 变成了 text。查了一圈对这个 index 有操作的代码,没有会影响的。

    {
        album: {
            mappings: {
                album: {
                    attrs: {
                        full_name: "attrs",
                        mapping: {
                            attrs: {
                                type: "keyword"
                            }
                        }
                    }
                }
            }
        }
    }
    

    变成了

    {
        album: {
            mappings: {
                doc: {
                    attrs: {
                        full_name: "attrs",
                        mapping: {
                            attrs: {
                                type: "text",
                                fields: {
                                    keyword: {
                                        type: "keyword",
                                        ignore_above: 256
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    第 1 条附言  ·  2018-09-20 15:22:49 +08:00
    上面变化后的 mapping 手误有错的地方,更正如下:
    ```
    {
    album: {
    mappings: {
    album: {
    attrs: {
    full_name: "attrs",
    mapping: {
    attrs: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    }
    }
    }
    }
    }
    }
    }
    ```
    第 2 条附言  ·  2018-09-20 15:39:18 +08:00

    把探索过程记录下,看看是不是能帮到有缘人。。

    前提:有个index的字段是attrs,值是一个List,查询需求是精确匹配,所以在建立index的时候指定了这个字段的type是keyword。

    这个问题更准确点说应该是datatype转成了multi fields,即一个字段定义多种映射类型,比如name字段同时有textkeyword类型,name的text类型用于模糊搜索,keyword类型用户完全匹配搜索。

    但是是什么原因导致这个字段变成了multi fields,目前还是没有找到。

    我按如下方式尝试绕过: 重建这个index,对attrs字段手动指定multi fields的类型,可以看这里的例子,大概是这样:

    {
      "mappings": {
        "album": {
          "properties": {
            "attrs": {
                "type": "keyword",
                "fields": {
                    "raw": {
                        "type": "keyword"
                    }
                }
            }
        }
      }
    }
    

    然后查询的时候改用attrs.raw字段名,大概是这样:

    {
      "query" : {
        "bool" : {
          "filter" : [
            {
              "terms" : {
                "attrs.raw" : [
                  "某attr"
                ]
              }
            }
          ]
        }
      }
    }
    

    目前就是这个情况,更到线上看看type还会不会被改掉。

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2810 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:34 · PVG 23:34 · LAX 08:34 · JFK 11:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.