说我有对象的映射,因为映射是:
{"my_type":
{"properties":
{"name":{"type":"string","store":"yes","index":"not_analyzed"},
"more":{"type":"object",
"properties":{"a_known_number":{"type":"long","index":"yes"},
"some_json_object":{"type":"object"}
}
}
}
}
}
我不知道“some_json_object”会有哪些子字段,但我知道我只想存储这个对象,而不是索引它的任何子字段。 我可以这样做:
{"my_type":
{"properties":
{"name":{"type":"string","store":"yes","index":"not_analyzed"},
"more":{"type":"object",
"properties":{"a_known_number":{"type":"long","index":"yes"},
"some_json_object":{"type":"object","store":"yes","index":"no"}
}
}
}
}
}
并影响所有生成的子字段?
答案 0 :(得分:6)
不,您不能将整个“对象”指定为未编入索引。但是,您可以使用dynamic_templates(http://www.elasticsearch.org/guide/reference/mapping/root-object-type/)来执行此操作:
{
"my_type":{
"properties":{
"name":{
"type":"string",
"store":"yes",
"index":"not_analyzed"
}
},
"dynamic_templates":[
{
"stored_json_object_template":{
"path_match":"some_json_object.*",
"mapping":{
"store":"yes",
"index":"no"
}
}
}
]
}
}
这告诉映射器将“some_json_object”的所有属性映射为存储的字符串。
<强>更新强> 从映射中删除了类型以匹配所有属性类型(match_path =&gt; path_match)。
更新2 如果您随后创建索引:
{
"mappings":{
"my_type":{
"properties":{
"name":{
"type":"string",
"store":"yes",
"index":"not_analyzed"
}
},
"dynamic_templates":[
{
"stored_json_object_template":{
"path_match":"some_json_object.*",
"mapping":{
"store":"yes",
"index":"no"
}
}
}
]
}
}
}
并索引一个对象:
{
"Name":"Henrik",
"some_json_object":{
"string":"string",
"long":12345
}
}
然后它将获得以下映射:
{
"testindex":{
"my_type":{
"dynamic_templates":[
{
"stored_json_object_template":{
"mapping":{
"index":"no",
"store":"yes"
},
"path_match":"some_json_object.*"
}
}
],
"properties":{
"name":{
"type":"string",
"index":"not_analyzed",
"store":true,
"omit_norms":true,
"index_options":"docs"
},
"some_json_object":{
"properties":{
"long":{
"type":"long",
"index":"no",
"store":true
},
"string":{
"type":"string",
"index":"no",
"store":true
}
}
}
}
}
}
}