我正在尝试索引文档,其中我不知道文档的一部分,我将其声明为对象。我想要实现的是“告诉”elasticsearch将我在此对象中给出的任何字段索引为字符串,换句话说,任何整数,长,日期字段将被映射并存储为字符串类型,例如
假设我们有以下文档来索引
{
"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 (取自documentation)。有什么想法吗?
答案 0 :(得分:1)
您可以使用dynamic templates表示名称custom_object
中的所有字段都必须映射为string
定义映射时,可以按如下方式设置
"dynamic_templates":[
{
"custom_object_template":{
"path_match":"custom_object.*",
"mapping":{
"type": "string"
}
}
}
答案 1 :(得分:1)
当Elasticsearch遇到新的字符串字段时,它会检查是否 该字符串包含可识别的日期,如2014-01-01。如果它看起来 与日期一样,该字段将添加为日期类型。否则,它被添加 作为类型字符串。
您可以在弹性搜索中自定义dynamic mapping以满足您的需求。
可以通过在根对象上将date_detection
设置为false
来关闭日期检测:
PUT /my_index
{
"mappings": {
"my_type": {
"date_detection": false
}
}
}
有了这个映射,string
将永远是string
。如果您需要date
字段,则必须手动添加。