Elasticsearch:如何使对象类型的所有属性都不被分析?

Man*_*ngh 4 elasticsearch

我需要使用Object字段创建一个Elasticsearch映射,该字段的密钥事先是未知的.此外,值可以是整数或字符串.但我希望将值存储为非分析字段(如果它们是字符串).我尝试了以下映射:

PUT /my_index/_mapping/test
{
  "properties": {
    "alert_text": {
      "type": "object",
      "index": "not_analyzed"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

现在索引创建得很好.但是,如果我插入这样的值:

POST /my_index/test
{
  "alert_text": {
    "1": "hello moto"
  }
}
Run Code Online (Sandbox Code Playgroud)

使用标准分析仪将值"hello moto"存储为分析字段.我希望它存储为非分析字段.如果我事先不知道所有钥匙都可以出现,是否可能?

mol*_*are 5

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

在这个例子中,我已经配置了我认为你需要的规则,即其中的所有字符串字段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"
  }
}
Run Code Online (Sandbox Code Playgroud)

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

GET /my_index/_mapping
Run Code Online (Sandbox Code Playgroud)

你将获得:

{
   "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"
                     }
                  }
               }
            }
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

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