gpt4 book ai didi

sorting - Elasticsearch如何按条件排序

转载 作者:行者123 更新时间:2023-12-02 23:28:45 24 4
gpt4 key购买 nike

在我的ElasticSearch(2.x)上,我有这样的文档:

{
"title": "A good title",
"formats": [{
"name": "pdf",
"prices": [{
"price": 11.99,
"currency": "EUR"
}, {
"price": 18.99,
"currency": "AUD"
}]
}]
}

我想按 formats.prices.price对文档进行排序,但仅对 formats.prices.currency === 'EUR'进行排序

我试图在 formats.prices上做一个嵌套字段,然后运行以下查询:
{
"query": {
"filtered": {
"query": {
"and": [
{
"match_all": {}
}
]
}
}
},
"sort": {
"formats.prices.price": {
"order": "desc",
"nested_path": "formats.prices",
"nested_filter": {
"term": {
"currency": "EUR"
}
}
}
}
}

但是不幸的是我不能得到正确的命令。

更新:
映射的相关部分:
   "formats": {
"properties": {
"name": {
"type": "string"
},
"prices": {
"type": "nested",
"include_in_parent": true,
"properties": {
"currency": {
"type": "string"
},
"price": {
"type": "double"
}
}
}
}
},

最佳答案

我希望这能解决您的问题

{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "formats.prices",
"filter": {
"match": {
"formats.prices.currency": "EUR"
}
}
}
}
]
}
},
"from": 0,
"size": 50,
"sort": [
{
"formats.prices.price": {
"order": "asc",
"nested_path": "formats.prices",
"nested_filter": {
"match": {
"formats.prices.currency": "EUR"
}
}
}
}
]
}

关于sorting - Elasticsearch如何按条件排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39274990/

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