gpt4 book ai didi

elasticsearch - 在 ElasticSearch 中使用动态键映射 map

转载 作者:行者123 更新时间:2023-11-29 02:55:41 25 4
gpt4 key购买 nike

我正在尝试将此类数据存储在 Elasticsearch 索引中

{
"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
},
"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
}
}
}

然而,映射不断为“再现”的所有不同子级添加唯一 block 。

理想情况下,我会为“再现”定义一种子类型,然后断言它下面的所有对象都应该是这样。

这可能吗?

我研究过动态映射,但对它有点困惑...

最佳答案

您可以使用动态模板尝试此映射:

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
}
}
}

注意 baseImage 中额外的 dummy 键。它会因错误而失败:

{
"error": "StrictDynamicMappingException[mapping set to strict, dynamic introduction of [dummy] within [renditions.baseImage] is not allowed]",
"status": 400
}

如果你想让它轻松一点,允许这样的文档被索引,但不希望额外的字段被索引,那么将dynamic设置为false。这不会在索引此类文档时导致任何失败,而且额外的字段将出现在 _source 字段中。但是,该字段不会被索引,您将无法对该字段执行任何查询或筛选。希望这有帮助

关于elasticsearch - 在 ElasticSearch 中使用动态键映射 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34814235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com