迁移至OpenSearch后,如何定义文档间的parent/child关系?
在AWS OpenSearch中定义文档的Parent/Child关系
AWS OpenSearch Service完全支持join字段类型来实现文档间的父子关系,和你之前在Elasticsearch 6.8中的用法一致,以下是具体实现步骤与注意事项:
1. 创建包含join字段的索引映射
直接在索引映射中定义join类型,指定父类型与子类型的关联关系:
PUT /my_index { "mappings": { "properties": { "doc_relation": { "type": "join", "relations": { "parent_doc": "child_doc" } }, "title": { "type": "text" }, "content": { "type": "text" } } } }
其中doc_relation为自定义的join字段名,parent_doc、child_doc可根据业务场景替换为实际的文档类型名称。
2. 插入父文档与子文档
- 插入父文档时,直接指定join字段的父类型:
PUT /my_index/_doc/1 { "title": "产品说明文档", "doc_relation": "parent_doc" }
- 插入子文档时,必须通过
routing参数指定父文档ID(确保父子文档存储在同一分片),同时在join字段中关联父文档:
PUT /my_index/_doc/2?routing=1 { "content": "产品参数详情", "doc_relation": { "name": "child_doc", "parent": "1" } }
3. 父子文档查询示例
- 查询指定父文档对应的所有子文档:
GET /my_index/_search { "query": { "has_parent": { "parent_type": "parent_doc", "query": { "match": { "title": "产品说明文档" } } } } }
- 查询指定子文档对应的父文档:
GET /my_index/_search { "query": { "has_child": { "type": "child_doc", "query": { "match": { "content": "产品参数详情" } } } } }
迁移注意事项
- 若通过快照迁移Elasticsearch 6.8的索引,只要快照保留了join字段映射,恢复后可直接使用,无需额外修改。
- 若使用
_reindexAPI迁移,需提前在目标OpenSearch索引中创建好包含join字段的映射,避免自动映射导致类型错误。 - OpenSearch的join字段用法与Elasticsearch 6.8完全兼容,原有业务查询逻辑无需调整。
内容的提问来源于stack exchange,提问作者Eager
相关产品推荐
相关产品推荐

