V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
swordwinter
V2EX  ›  问与答

ElasticSearch mapping type 从 keyword 变成了 text

  •  
  •   swordwinter · Sep 20, 2018 · 3520 views
    This topic created in 2776 days ago, the information mentioned may be changed or developed.

    有人知道这会是什么造成的吗? 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
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    Supplement 1  ·  Sep 20, 2018
    上面变化后的 mapping 手误有错的地方,更正如下:
    ```
    {
    album: {
    mappings: {
    album: {
    attrs: {
    full_name: "attrs",
    mapping: {
    attrs: {
    type: "text",
    fields: {
    keyword: {
    type: "keyword",
    ignore_above: 256
    }
    }
    }
    }
    }
    }
    }
    }
    }
    ```
    Supplement 2  ·  Sep 20, 2018

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

    前提:有个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还会不会被改掉。

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1406 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 17:10 · PVG 01:10 · LAX 10:10 · JFK 13:10
    ♥ Do have faith in what you're doing.