我正在使用以下映射:
PUT /my_index { "mappings": { "blogpost": { "properties": { "title": {"type": "string"} "comments": { "type": "nested", "properties": { "comment": { "type": "string" }, "date": { "type": "date" } } } } } } }
文件示例:
PUT /my_index/blogpost/1 { "title": "Nest eggs", "comments": [ { "comment": "Great article", "date": "2014-09-01" }, { "comment": "More like this please", "date": "2014-10-22" }, { "comment": "Visit my website", "date": "2014-07-02" }, { "comment": "Awesome", "date": "2014-08-23" } ] }
我的问题是如何检索此文档并按"日期"对嵌套对象"注释"进行排序?结果:
PUT /my_index/blogpost/1 { "title": "Nest eggs", "comments": [ { "comment": "Awesome", "date": "2014-07-23" }, { "comment": "Visit my website", "date": "2014-08-02" }, { "comment": "Great article", "date": "2014-09-01" }, { "comment": "More like this please", "date": "2014-10-22" } ] }
ChintanShah2.. 8
你需要sort
在inner_hits上对它进行排序nested objects
.这将为您提供所需的输出
GET my_index/_search { "query": { "nested": { "path": "comments", "query": { "match_all": {} }, "inner_hits": { "sort": { "comments.date": { "order": "asc" } }, "size": 5 } } }, "_source": [ "title" ] }
我正在使用源过滤来获取仅"title"
在comments
内部检索,inner_hit
但如果你想要你可以避免
size
是5因为默认值是3,我们在给定的例子中有4个对象.
希望这可以帮助!
你需要sort
在inner_hits上对它进行排序nested objects
.这将为您提供所需的输出
GET my_index/_search { "query": { "nested": { "path": "comments", "query": { "match_all": {} }, "inner_hits": { "sort": { "comments.date": { "order": "asc" } }, "size": 5 } } }, "_source": [ "title" ] }
我正在使用源过滤来获取仅"title"
在comments
内部检索,inner_hit
但如果你想要你可以避免
size
是5因为默认值是3,我们在给定的例子中有4个对象.
希望这可以帮助!