在Elasticsearch中对嵌套对象进行排序

我正在使用以下映射:

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"

}

]

}

回答:

您需要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个对象。

希望这可以帮助!

以上是 在Elasticsearch中对嵌套对象进行排序 的全部内容, 来源链接: utcz.com/qa/411126.html

回到顶部