Elasticsearch: 使用 has_parent 查询父文档 - 以 orderparent 为例
"POST /orderparent/_search\n{"from": 0,"size": 20,"sort": {},"query": {"has_parent": {"parent_type": "parent","query": {"term": {"userName": "zhangjie"} } } } }\n\n\n"mapping" 如下:\n{"mappings": {"dynamic": "false","properties": {"child": {"properties": {"commentTime": {"type": "date"} ,"content": {"type": "text","analyzer": "ik_max_word"} ,"userId": {"type": "keyword"} } },"parent": {"properties": {"createTime": {"type": "date"} ,"orderNum": {"type": "text","analyzer": "ik_max_word"} ,"userName": {"type": "keyword"} } },"relation": {"type": "join","eager_global_ordinals": true,"relations": {"parent": "child"} } } } }\n\n数据如下:\n{"_index": "orderparent","_type": "_doc","_id": "dtjqC4kBGU-xW33U2oAi","_score": 1,"_source": {"comment": [{"commentTime": 1688122086419,"content": "产品很好","userId": "yaosan"} ],"createTime": 1688122086418,"orderNum": "11122","userName": "zhangjie"} }\n\n没有查询出数据内容:根据您提供的查询语句和数据,我没有发现明显的错误。但是,根据您的查询条件,只有当父文档的"userName"字段的值为"zhangjie"时,才会返回结果。请确保您的数据中确实存在这样的父文档,并且该字段的值确实为"zhangjie"。\n\n另外,请注意,您的查询语句中的排序条件为空。如果您希望按照某个字段进行排序,请在"sort"字段中指定相应的排序条件。\n\n如果问题仍然存在,请提供更多的相关信息,以便我能够更好地帮助您解决问题。
原文地址: https://www.cveoy.top/t/topic/psWG 著作权归作者所有。请勿转载和采集!