在我的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/