gpt4 book ai didi

elasticsearch - Elasticsearch映射通过模板选择所有字段以更改其数据类型Elasticsearch

转载 作者:行者123 更新时间:2023-12-03 00:58:22 26 4
gpt4 key购买 nike

大家好,我正在使用elasticsearch-template.json将我所有字段的数据类型设置为字符串。以下是模板的代码段:

{
"template": "logstash-*",
"settings": {
"index.refresh_interval": "5s",
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"logs": {
"_all": {
"enabled": true
},
"properties": {
"level1": {
"properties": {
"level2": {
"properties": {
"_all": {"type": "string"}
}
}
}
}
}
}
}
}

在level2下,我有很多要创建的字段,我想将所有字段设置为字符串,如何设置它。我尝试使用“*”字符以及“%”字符来选择所有字段。但不幸的是,它仅作为新字段添加到映射中。如何在模板中指定选择某个级别下的所有字段?

最佳答案

我相信您正在寻找的是dynamic_templates,并使用path_match而不是match。这说明了它可能如何工作:

curl -DELETE localhost:9200/test-*
curl -XDELETE http://localhost:9200/_template/test
curl -XPOST http://localhost:9200/_template/test -d '
{
"template": "test-*",
"mappings": {
"_default_": {
"dynamic_templates": [
{
"level1_level2_all": {
"path_match": "level1.level2.*",
"match_mapping_type": "*",
"mapping": {
"index": "not_analyzed",
"type": "string"
}
}
}
]
}
}
}
'

curl -XPOST http://localhost:9200/test-1/a -d '
{
"level1": {
"level2": {
"x":1
}
}
}'
curl -XPOST http://localhost:9200/test-1/a -d '
{
"level1": {
"level2": {
"y":1
}
}
}'

curl http://localhost:9200/test-1/_mapping?pretty

输出为:
  "test-1" : {
"mappings" : {
"_default_" : {
"dynamic_templates" : [ {
"level1_level2_all" : {
"mapping" : {
"index" : "not_analyzed",
"type" : "string"
},
"match_mapping_type" : "*",
"path_match" : "level1.level2.*"
}
} ],
"properties" : { }
},
"a" : {
"dynamic_templates" : [ {
"level1_level2_all" : {
"mapping" : {
"index" : "not_analyzed",
"type" : "string"
},
"match_mapping_type" : "*",
"path_match" : "level1.level2.*"
}
} ],
"properties" : {
"level1" : {
"properties" : {
"level2" : {
"properties" : {
"x" : {
"type" : "string",
"index" : "not_analyzed"
},
"y" : {
"type" : "string",
"index" : "not_analyzed"
}
}
}
}
}
}
}
}
}
}

关于elasticsearch - Elasticsearch映射通过模板选择所有字段以更改其数据类型Elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41036899/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com