gpt4 book ai didi

Elasticsearch:如何将对象类型的所有属性设为未分析?

转载 作者:行者123 更新时间:2023-11-29 02:51:57 24 4
gpt4 key购买 nike

我需要创建一个 Elasticsearch 映射,其对象字段的键事先未知。此外,值可以是整数或字符串。但如果它们是字符串,我希望将这些值存储为非分析字段。我尝试了以下映射:

PUT /my_index/_mapping/test
{
"properties": {
"alert_text": {
"type": "object",
"index": "not_analyzed"
}
}
}

现在索引创建好了。但是如果我插入这样的值:

POST /my_index/test
{
"alert_text": {
"1": "hello moto"
}
}

值“hello moto”使用标准分析器存储为分析字段。我希望将其存储为非分析字段。如果我事先不知道所有 key 都可以存在,是否可能?

最佳答案

尝试 dynamic templates .使用此功能,您可以为动态创建的字段配置一组规则。

在此示例中,我配置了我认为您需要的规则,即 alert_text 中的所有字符串字段都是 not_analyzed:

PUT /my_index
{
"mappings": {
"test": {
"properties": {
"alert_text": {
"type": "object"
}
},
"dynamic_templates": [
{
"alert_text_strings": {
"path_match": "alert_text.*",
"match_mapping_type": "string",
"mapping": {
"type": "string",
"index": "not_analyzed"
}
}
}
]
}
}
}

POST /my_index/test
{
"alert_text": {
"1": "hello moto"
}
}

执行上述请求后,您可以执行此查询以显示当前映射:

GET /my_index/_mapping

你将获得:

{
"my_index": {
"mappings": {
"test": {
"dynamic_templates": [
{
"alert_text_strings": {
"mapping": {
"index": "not_analyzed",
"type": "string"
},
"match_mapping_type": "string",
"path_match": "alert_text.*"
}
}
],
"properties": {
"alert_text": {
"properties": {
"1": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
}

在哪里可以看到 alert_text.1 存储为 not_analyzed

关于Elasticsearch:如何将对象类型的所有属性设为未分析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34662685/

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