C.R*_*lon 8 mapping elasticsearch
当我想使用此命令将字段类型从文本更改为关键字时:
PUT indexStat/_mapping/StatCateg
{
"StatCateg":{
"properties": {
"nom_categorie": {
"type":"keyword","index": true
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有这样的信息:
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "mapper [nom_categorie] of different type, current_type [text], merged_type [keyword]"
}
],
"type": "illegal_argument_exception",
"reason": "mapper [nom_categorie] of different type, current_type [text], merged_type [keyword]"
},
"status": 400
}
Run Code Online (Sandbox Code Playgroud)
C.R*_*lon 15
最后我在文档中看到,无法更改字段的数据类型:
更新现有映射
除了记录之外,现有的类型和字段映射无法更新.更改映射意味着使已编制索引的文档无效.相反,您应该使用正确的映射创建一个新索引,并将数据重新索引到该索引中.
所以唯一的解决方案是:
Jos*_*ush 12
不支持更改现有索引的数据类型(映射)。为此,请使用正确的类型(映射)和reindex您的数据创建一个新索引。
关于这样做的Elastic博客文章,并建议使用最佳实践方法为您的索引设置别名。
这些是所需的步骤,下次会更轻松,无需停机
StatCategStatCateg_v1使用正确的映射创建新索引StatCateg到StatCateg_v1 StatCategStatCateg_v1-> StatCateg(以便下次无需停机就可以更轻松地进行)示例片段(在 python 中):
import requests
current_index_name = 'StatCateg'
new_index_name = 'StatCateg-v1'
base_url = 'https://...'
mapping_changes = {
"nom_categorie": {"type": "keyword"}
}
# ------------------------------------------------
# Get current mapping
r = requests.get('{base_url}/{index_name}'.format(base_url=base_url, index_name=current_index_name))
r.raise_for_status()
content = r.json()
mappings = content[current_index_name]['mappings']
mappings['properties'].update(mapping_changes)
# ------------------------------------------------
# Create a new index with the correct mappings
r = requests.put('{base_url}/{index_name}'.format(base_url=base_url, index_name=new_index_name), json={
'mappings': mappings
})
r.raise_for_status()
# ------------------------------------------------
# Reindex
r = requests.post('{base_url}/_reindex'.format(base_url=base_url), json={
"source": {
"index": current_index_name
},
"dest": {
"index": new_index_name
}
})
r.raise_for_status()
# ------------------------------------------------
# Delete the old index
r = requests.delete('{base_url}/{index_name}'.format(base_url=base_url, index_name=current_index_name))
r.raise_for_status()
# ------------------------------------------------
# Create an alias (so that on next time this will be easier to do without downtime)
r = requests.post('{base_url}/_aliases'.format(base_url=base_url), json={
"actions": [
{"add": {
"alias": current_index_name,
"index": new_index_name
}}
]
})
r.raise_for_status()
Run Code Online (Sandbox Code Playgroud)
这些是所需的步骤,没有停机时间
StatCateg_v1StatCateg_v2使用正确的映射创建新索引StatCateg_v1到StatCateg_v2 StatCateg_v1- > StatCateg)和(StatCateg_v2- > StatCateg)StatCateg_v1示例片段(在 python 中):
import requests
index_name = 'StatCateg'
current_index_name = 'StatCateg_v1'
next_index_name = 'StatCateg_v2'
base_url = 'https://...'
mapping_changes = {
"nom_categorie": {"type": "keyword"}
}
# ------------------------------------------------
# Get current mapping
r = requests.get('{base_url}/{index_name}'.format(base_url=base_url, index_name=current_index_name))
r.raise_for_status()
content = r.json()
mappings = content[current_index_name]['mappings']
mappings['properties'].update(mapping_changes)
# ------------------------------------------------
# Create a new index with the correct mappings
r = requests.put('{base_url}/{index_name}'.format(base_url=base_url, index_name=next_index_name), json={
'mappings': mappings
})
r.raise_for_status()
# ------------------------------------------------
# Reindex
r = requests.post('{base_url}/_reindex'.format(base_url=base_url), json={
"source": {
"index": current_index_name
},
"dest": {
"index": next_index_name
}
})
r.raise_for_status()
# ------------------------------------------------
# Replace old index alias with new
r = requests.post('{base_url}/_aliases'.format(base_url=base_url), json={
"actions": [
{"remove": {
"alias": index_name,
"index": current_index_name
}},
{"add": {
"alias": index_name,
"index": next_index_name
}}
]
})
r.raise_for_status()
# ------------------------------------------------
# Delete the old index
r = requests.delete('{base_url}/{index_name}'.format(base_url=base_url, index_name=current_index_name))
r.raise_for_status()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10610 次 |
| 最近记录: |