我试图将此类数据存储在Elasticsearch索引
中x = raw_input('Select an option -> ')`
然而,该映射不断为所有不同的孩子添加独特的块,以及#34;再现"。
理想情况下,我会为"再现"定义一种类型的孩子。然后断言它下面的所有对象应该是那样的。
这可能吗?
我已经研究过Dynamic Mappings,但我对此一点感到困惑......
答案 0 :(得分:2)
您可以使用动态模板尝试此映射:
PUT /test
PUT /test/_mapping/test
{
"properties": {
"id": {
"type": "string"
},
"headline": {
"type": "string"
}
},
"dynamic_templates": [
{
"renditions_objects": {
"mapping": {
"dynamic": "strict",
"type": "object",
"properties": {
"height": {
"type": "integer"
},
"href": {
"type": "string"
},
"mimetype": {
"type": "string"
},
"width": {
"type": "integer"
}
}
},
"match_mapping_type": "object",
"path_match": "renditions.*"
}
}
]
}
该行
"dynamic": "strict",
确保您无法索引存在额外字段的任何文档。如果您尝试索引这样的内容,则该过程将失败:
POST /test/test
{
"id": "5644596f9bf67301645999d9",
"headline": "Scientists Look Beyond Solar System to Study Planet",
"renditions": {
"baseImage": {
"height": 933,
"href": "www.imgur.com/animage",
"mimetype": "image/jpeg",
"width": 1400,
"dummy": 12
},
"preview": {
"height": 500,
"href": "www.imgur.com/animage",
"mimetype": "image/jpeg",
"width": 400
},
"thumbnail": {
"height": 150,
"href": "www.imgur.com/animage",
"mimetype": "image/jpeg",
"width": 125
}
}
}
请注意dummy
中的额外baseImage
密钥。它将失败并出现错误:
{
"error": "StrictDynamicMappingException[mapping set to strict, dynamic introduction of [dummy] within [renditions.baseImage] is not allowed]",
"status": 400
}
如果您想让它稍微放松并允许对这些文档编制索引,但又不希望将其他字段编入索引,则将dynamic
设置为false
。索引此类文档时不会导致任何失败,_source
字段中也会出现额外字段。但是,该字段不会被编入索引,并且您无法对该字段执行任何查询或过滤。希望这有帮助