欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Elasticsearch修改字段类型

程序员文章站 2022-07-09 15:37:07
...

1.设置索引t2为想要的数据类型

2.将t1 reindex到t2

3.数据reindex完成删除t1

4.设置索引t1为想要的数据类型

5.将t2 reindex到t1

PUT t1
{
     "settings": {
      "index": {
        "number_of_shards": "2",
        "analysis": {
          "analyzer": {
            "ik_custom_analyzer": {
              "filter": [
                "lowercase"
              ],
              "type": "custom",
              "tokenizer": "ik_max_word"
            }
          }
        },
        "number_of_replicas": "1"
      }
    },
    "mappings": {
      "doc": {
        "properties": {
          "abs_path": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "comp_id": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "id": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "id_path": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "index_update_time": {
            "type": "long"
          },
          "name": {
            "type": "text",
            "analyzer": "ik_custom_analyzer"
          },
          "parent_id": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "star": {
            "type": "long"
          },
          "time": {
            "type": "long"
          },
          "weight": {
            "type": "long"
          }
        }
      }
    }  
}

PUT /t2
{

	"settings": {
		"number_of_shards": 2,
		"number_of_replicas": 1,
		"analysis": {
			"analyzer": {
				"simplechar_index_analyzer": {
					"type": "custom",
					"tokenizer": "simplechar_index"
				},
				"simplechar_search_analyzer": {
					"type": "custom",
					"tokenizer": "simplechar_search"
				},
				"pinyin_index_analyzer": {
					"type": "custom",
					"tokenizer": "pinyin_index"
				},
				"pinyin_search_analyzer": {
					"type": "custom",
					"tokenizer": "pinyin_search"
				}
			},
			"tokenizer": {
				"simplechar_index": {
					"type": "simplechar_and_pinyin",
					"simple_char": true,
					"prefix_en": true,
					"prefix_num": true
				},
				"simplechar_search": {
					"type": "simplechar_and_pinyin",
					"simple_char": true,
					"prefix_num": true,
					"prefix_en": true
				},
				"pinyin_index": {
					"type": "simplechar_and_pinyin",
					"simple_char": true,
					"han_to_pinyin": true,
					"keep_first": true,
					"keep_fuzzy": true,
					"simple_num": true
				},
				"pinyin_search": {
					"type": "simplechar_and_pinyin",
					"simple_char": true,
					"en_to_pinyin": true,
					"simple_num": true
				}
			}
		}
	},
	"mappings": {
		"doc": {
			"properties": {
				"name": {
					"type": "text",
					"analyzer": "simplechar_index_analyzer",
					"search_analyzer": "simplechar_search_analyzer",
					"similarity": "boolean",
					"fielddata": true,
					"fields": {
						"pinyin": {
							"type": "text",
							"similarity": "boolean",
							"analyzer": "pinyin_index_analyzer",
							"search_analyzer": "pinyin_search_analyzer",
							"fielddata": true
						}
					}
				}
			}
		}
	}
}


PUT t1/doc/3
{
          "id": "3",
          "name": "第七层",
          "star": 0,
          "parent_id": "134111385604698112",
          "comp_id": "500000151",
          "abs_path": "啤啤第三个公司/这个是子部门1/这个是部门下的部门/再加/第五层/第六层/第七层",
          "id_path": "131773053453164544,131775211384823808,131775319170052096,132666474766233600,134111338699763712,134111385604698112,134112080391122944",
          "weight": "2",
          "index_update_time": 1547469387,
          "time": 1547469386994201600
}

GET t1/doc/_search
{
  "size": 20,
  "query": {
    "match_all": {}
  }
}


POST _reindex
{
  "source": {
    "index": "t1"
  },
  "dest": {
    "index": "t2",
    "version_type": "external"
  },
  "script": {
    "source":"def w = ctx._source.remove('weight');ctx._source.weight=w;",
    "lang": "painless"
  }
}
GET t1/_mapping
GET t2/_mapping

GET t2/doc/_search
{
  "size": 20,
  "query": {
    "match_all": {}
  }
}