我正在尝试索引那些我不知道文档的一部分的文档,我将其声明为对象。我想要实现的是"告诉"弹性搜索,以字符串形式索引我在该对象中给出的任何字段,换句话说,任何整数、长、日期字段,以字符串类型映射和存储,例如
假设我们有以下文档来索引
{
"foo":"bar",
"custom_object":{
"a_name":"jim",
"a_date":"2016-3-31"
}
}
我确实不先验地知道custom_object中我的字段的名称,该文档log的映射如下:
"mappings": {
'log': {
'properties': {
'foo': {
'type': 'string',
'index': 'not_analyzed'
},
'custom_object': {
'type': 'object'
}
}
}
我如何判断,无论我给custom_object中的文档什么,都要映射为字符串?这主要与日期值有关,如果我面临这个问题的话。我不想忽略dynamic=false或enabled=false(取自文档)。有什么想法吗?
您可以使用动态模板来表示名称custom_object
中的所有字段都必须映射为string
定义映射时,可以按以下进行设置
"dynamic_templates":[
{
"custom_object_template":{
"path_match":"custom_object.*",
"mapping":{
"type": "string"
}
}
}
当Elasticsearch遇到一个新的字符串字段时,它会检查是否字符串包含一个可识别的日期,如2014-01-01。如果看起来与日期类似,该字段被添加为日期类型。否则,添加作为类型字符串。
您可以在elasticsearch中自定义动态映射以满足您的需求。
通过在根对象上设置date_detection
到false
,可以关闭日期检测:
PUT /my_index
{
"mappings": {
"my_type": {
"date_detection": false
}
}
}
有了这个映射,string
将始终是string
。如果需要date
字段,则必须手动添加。