elasticsearch 修改索引mapping映射

2022年1月18日 891点热度 0人点赞 0条评论

1 原映射文件

{
  "esbook" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "authors" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "description" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "isbn" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "level_tags" : {
          "properties" : {
            "first" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "second" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "third" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        },
        "logo" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "page_number" : {
          "type" : "long"
        },
        "price" : {
          "type" : "float"
        },
        "publish_date" : {
          "type" : "long"
        },
        "source" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "source_code" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "tags" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "translator_authors" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "translators" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1642487126743",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "8QFdN2DRSeqyCBSG2i1_Kg",
        "version" : {
          "created" : "7010099"
        },
        "provided_name" : "esbook"
      }
    }
  }
}

2 修改为如下索引

# 修改 isbn 字段类型为keyword
# 删除 logo 字段
# 修改 publish_date 为date 并且设置为多种类型
# 修改 level_tags 为 object类型 dynamic 是否可以动态添加字段,enable是否可以索引
# 增加 editors 嵌套类型
PUT esbook
{
    "settings":{
        "index":{
            "number_of_shards":"1",
            "number_of_replicas":"1"
        }
    },
    "mappings":{
        "properties":{
            "authors":{
                "type":"text",
                "fields":{
                    "keyword":{
                        "type":"keyword",
                        "ignore_above":256
                    }
                }
            },
            "description":{
                "type":"text",
                "fields":{
                    "keyword":{
                        "type":"keyword",
                        "ignore_above":256
                    }
                }
            },
            "isbn":{
                "type":"keyword"
            },
            "level_tags":{
                "type":"object",
                "dynamic":true,
                "enabled":true,
                "properties":{
                    "first":{
                        "type":"keyword"
                    },
                    "second":{
                        "type":"keyword"
                    },
                    "third":{
                        "type":"keyword"
                    }
                }
            },
            "name":{
                "type":"text",
                "fields":{
                    "keyword":{
                        "type":"keyword",
                        "ignore_above":256
                    }
                }
            },
            "page_number":{
                "type":"long"
            },
            "price":{
                "type":"float"
            },
            "publish_date":{
                "type":"date",
                "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
            },
            "source":{
                "type":"text",
                "fields":{
                    "keyword":{
                        "type":"keyword",
                        "ignore_above":256
                    }
                }
            },
            "source_code":{
                "type":"text",
                "fields":{
                    "keyword":{
                        "type":"keyword",
                        "ignore_above":256
                    }
                }
            },
            "tags":{
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "translator_authors":{
                "type":"text",
                "fields":{
                    "keyword":{
                        "type":"keyword",
                        "ignore_above":256
                    }
                }
            },
            "translators" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "editors":{
                "type":"nested",
                "dynamic":true,
                "enabled":true,
                "properties":{
                    "id":{
                        "type":"keyword"
                    },
                    "name":{
                        "type":"text",
                        "fields":{
                            "keyword":{
                                "type":"keyword",
                                "ignore_above":256
                            }
                        }
                    }
                }
            }
        }
    }
}

3 使用reindex

POST _reindex
{
  "source": {
    "index": "esbook_bak"
  },
  "dest": {
    "index": "esbook2"
  }
}

//响应结果
{
  "took" : 1109,
  "timed_out" : false,
  "total" : 3217,
  "updated" : 0,
  "created" : 3217,
  "deleted" : 0,
  "batches" : 4,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}

2.1 插曲

tags是List<String>数组类型,设置netsted

"tags":{
              "type" : "nested"
            },
PUT esbook3
{
    "settings":{
        "index":{
            "number_of_shards":"1",
            "number_of_replicas":"1"
        }
    },
    "mappings":{
        "properties":{
            "authors":{
                "type":"text",
                "fields":{
                    "keyword":{
                        "type":"keyword",
                        "ignore_above":256
                    }
                }
            },
            "description":{
                "type":"text",
                "fields":{
                    "keyword":{
                        "type":"keyword",
                        "ignore_above":256
                    }
                }
            },
            "isbn":{
                "type":"keyword"
            },
            "level_tags":{
                "type":"object",
                "dynamic":true,
                "enabled":true,
                "properties":{
                    "first":{
                        "type":"keyword"
                    },
                    "second":{
                        "type":"keyword"
                    },
                    "third":{
                        "type":"keyword"
                    }
                }
            },
            "name":{
                "type":"text",
                "fields":{
                    "keyword":{
                        "type":"keyword",
                        "ignore_above":256
                    }
                }
            },
            "page_number":{
                "type":"long"
            },
            "price":{
                "type":"float"
            },
            "publish_date":{
                "type":"date",
                "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
            },
            "source":{
                "type":"text",
                "fields":{
                    "keyword":{
                        "type":"keyword",
                        "ignore_above":256
                    }
                }
            },
            "source_code":{
                "type":"text",
                "fields":{
                    "keyword":{
                        "type":"keyword",
                        "ignore_above":256
                    }
                }
            },
            "tags":{
              "type" : "nested"
            },
            "translator_authors":{
                "type":"text",
                "fields":{
                    "keyword":{
                        "type":"keyword",
                        "ignore_above":256
                    }
                }
            },
            "translators" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "editors":{
                "type":"nested",
                "dynamic":true,
                "enabled":true,
                "properties":{
                    "id":{
                        "type":"keyword"
                    },
                    "name":{
                        "type":"text",
                        "fields":{
                            "keyword":{
                                "type":"keyword",
                                "ignore_above":256
                            }
                        }
                    }
                }
            }
        }
    }
}
执行
POST _reindex
{
  "source": {
    "index": "esbook_bak"
  },
  "dest": {
    "index": "esbook3"
  }

提示错误:
{
  "took": 1694,
  "timed_out": false,
  "total": 3217,
  "updated": 0,
  "created": 0,
  "deleted": 0,
  "batches": 1,
  "version_conflicts": 0,
  "noops": 0,
  "retries": {
    "bulk": 0,
    "search": 0
  },
  "throttled_millis": 0,
  "requests_per_second": -1,
  "throttled_until_millis": 0,
  "failures": [
    {
      "index": "esbook3",
      "type": "_doc",
      "id": "9787115542007",
      "cause": {
        "type": "mapper_parsing_exception",
        "reason": "object mapping for [tags] tried to parse field [null] as object, but found a concrete value"
      },
      "status": 400
    }
  ]
}

原因是 es天生支持数组,不需要把List<String>数组定义为nested。

管理员

这个人很懒,什么都没留下

文章评论