Blog icon indicating copy to clipboard operation
Blog copied to clipboard

迁移 Elastic Search:Elastic Search 7.x mapping 的调整

Open nonacosa opened this issue 5 years ago • 0 comments

以一个不分词的非常简单 Index 下的 type 为例:

image

注意,原来的结构是 mappings 复数的,这样的 JSON 可以存好多个 type :

{
    "mappings":{
        "transfer":{
            "properties":{
                "loveCount":{
                    "index":"not_analyzed",
                    "type":"string"
                },
                "test":{
                    "index":"not_analyzed",
                    "type":"string"
                },
                "id":{
                    "index":"not_analyzed",
                    "type":"string"
                }
            }
        },
        "goods":{
            ...... 另一个 type
        }
    }
}

现在,我们同样的 transfer 按照新的标准应该这么写:

Mapping

image

PUT /transfer
{
  "mappings": {
    "properties": {
      "loveCount":    { "type": "keyword" }, 
      "test":  { "type": "keyword"  },
      "id":   { "type": "text"  }    
    }
  }
}

------response------

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "transfer"
}

image

  • keyword 是相当于 not_analyzed 不分词,text 会预分词,倒排索引,不指定分词器就用默认的分词器。

  • es7 索引下仅支持一个 mapping。

下面搞一个2.2中我们使用的带有分词的复杂的例子,对照下面文档写一下新的 document:

Test an analyzer | Elasticsearch Reference [7.x] | Elastic

  • 这里面还写了一些自定义分词器的使用方式

Mapping

{
    "inventory":{
        "properties":{
            "genTime":{
                "format":"YYYY-MM-dd HH:mm:ss",
                "type":"date"
            },
            "rowId":{
                "index":"not_analyzed",
                "type":"string"
            },
            "listId":{
                "analyzer":"whitespace",
                "type":"string"
            },
            "createTime":{
                "format":"YYYY-MM-dd HH:mm:ss",
                "type":"date"
            } 
        }
    }
}

对应的 elasticSearch 7.x 应该这么写:

PUT /inventory
{
  "mapping": {
    "properties": {
      "genTime": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      },
      "listId": {
        "type": "text",
        "analyzer": "whitespace"
      },
      "rowId": {
        "type": "keyword"
      }
    }
  }
}

-----respose-----

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "inventory"
}
  • tips : 关于日期格式现在可以写成这样:
PUT my_index
{
  "mappings": {
    "properties": {
      "date": {
        "type":   "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      }
    }
  }
}

Date datatype | Elasticsearch Reference [7.4] | Elastic

我觉得这样写的好处,是减少了之前保存文档的时候因为时间格式的忽视保存失败的查找问题的事件。

而且调整了 document 中 index 的作用,之前是用来指定是否分词,或者使用哪种分词器的,现在是:

image

现在是决定是否对这个 field 进行索引和搜索(我理解索引 = 倒排索引 = 搜索,一个意思)

通过以上,对 es 2.x 强行升级到 7.x 又有了一定的把握。官方升级太麻烦官方每次大版本自然会提供一种所谓的“单节点平滑迁移方式”,但是我们实在欠了太多技术债,落了 5.x 6.x 7.x 三个大版本,官方建议 2.x 先升级到 5.8 然后升级到 6.x 以此类推,我是绝对不会这样干的

nonacosa avatar Aug 18 '20 08:08 nonacosa