我在ElastiSearch中有下面的数据结构。
[{
"name": "Kapil",
"age": 32,
"hobbies": ["Cricket", "Football", "Swimming"]
},
{
"name": "John",
"age": 33,
"hobbies": ["Baseball", "Football", "Swimming"]
},
{
"name": "Vick",
"age": 30,
"hobbies": ["Baseball", "Karate", "Swimming"]
}]我想按以下顺序从数据中获取所有记录:
Football爱好的用户按年龄分类。因此,预期的结果是按照顺序约翰,卡皮尔和维克。
我使用下面的查询来获得#1的结果。
{
"size": 500,
"query": {
"bool": {
"must": [
{
"match_phrase": {
"hobbies.keyword": "Football"
}
}
]
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
},用于下面第2点。
{
"size": 500,
"query": {
"bool": {
"must_not": [
{
"match_phrase": {
"hobbies.keyword": "Football"
}
}
]
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}有了以上所述,我无法维护分页逻辑。它还要求我单独执行这两个查询。有人能帮忙如何实现这一点吗?
发布于 2021-11-29 14:42:58
试试这个:
{
"query": {
"bool": {
"must": {
"match_all": {} <-- retrieve all docs
},
"should": { <-- give higher score to docs that match this clause
"match_phrase": {
"hobbies.keyword": "Football"
}
}
}
},
"sort": [
"_score", <-- sort by doc score first
{
"age": { <-- when score is equal then sort by age
"order": "desc"
}
}
]
}https://stackoverflow.com/questions/70155976
复制相似问题