[英]How are the documents ordered in Elasticsearch if the sort value for two documents is same?
[英]How to sort the documents in the bucket using more then one field match in Elasticsearch?
我正在使用彈性搜索 7.9.0
我的索引有這樣的文件
{
"student": {
"name": "Guru",
"new_student": true,
"total_marks": 100
}
}
{
"student": {
"name": "Mayur",
"new_student": false,
"total_marks": 90
}
}
{
"student": {
"name": "Darshan",
"new_student": false,
"total_marks": 0
}
}
{
"student": {
"name": "Manu",
"new_student": true,
"total_marks": 0
}
}
現在我的輸出應該包含以下順序的結果。\\
"new_student":true
且"total_marks" > 0
"new_student":false
且"total_marks" > 0
所有學生"new_student":true
且"total_marks" = 0
"new_student":false
且"total_marks" = 0
我們怎樣才能做到這一點?
我嘗試將 boot 添加到分別匹配上述字段的 bool 查詢中。
然后我意識到我必須使用student.name
進行排序。 如果我這樣做,提升將不會產生任何影響。
然后我嘗試了多搜索 API。 我得到了預期的結果,但后來我意識到在多搜索中分頁很困難。
如何解決這個問題?
謝謝
可以在 should 子句中添加不同的function_score查詢每個 function_score 將給一個組相同的分數。 在排序中,分數和名稱可用於對文檔進行排序。 由於每個組的分數相同,因此將按字母順序排序
詢問
{
"query": {
"bool": {
"should": [
{
"function_score": {
"query": {
"bool": {
"must": [
{
"term": {
"student.new_student": {
"value": true
}
}
},
{
"range": {
"student.total_marks": {
"gt": 0
}
}
}
]
}
},
"boost": "5"
}
},
{
"function_score": {
"query": {
"bool": {
"must": [
{
"term": {
"student.new_student": {
"value": false
}
}
},
{
"range": {
"student.total_marks": {
"gt": 0
}
}
}
]
}
},
"boost": "4"
}
},
{
"function_score": {
"query": {
"bool": {
"must": [
{
"term": {
"student.new_student": {
"value": true
}
}
},
{
"range": {
"student.total_marks": {
"gte": 0,
"lte": 0
}
}
}
]
}
},
"boost": "3"
}
},
{
"function_score": {
"query": {
"bool": {
"must": [
{
"term": {
"student.new_student": {
"value": false
}
}
},
{
"range": {
"student.total_marks": {
"gte": 0,
"lte": 0
}
}
}
]
}
},
"boost": "2"
}
}
]
}
},
"sort": [
{
"_score": {
"order": "desc"
}
},
{
"student.name.keyword": {
"order": "asc"
}
}
]
}
結果
"hits" : [
{
"_index" : "index32",
"_type" : "_doc",
"_id" : "STtN5HsBssOzZCY8olFq",
"_score" : 7.6949825,
"_source" : {
"student" : {
"name" : "Abc",
"new_student" : true,
"total_marks" : 90
}
},
"sort" : [
7.6949825,
"Abc"
]
},
{
"_index" : "index32",
"_type" : "_doc",
"_id" : "RTsT5HsBssOzZCY8olGD",
"_score" : 7.6949825,
"_source" : {
"student" : {
"name" : "Guru",
"new_student" : true,
"total_marks" : 100
}
},
"sort" : [
7.6949825,
"Guru"
]
},
{
"_index" : "index32",
"_type" : "_doc",
"_id" : "RjsT5HsBssOzZCY8plFr",
"_score" : 7.501875,
"_source" : {
"student" : {
"name" : "Mayur",
"new_student" : false,
"total_marks" : 90
}
},
"sort" : [
7.501875,
"Mayur"
]
},
{
"_index" : "index32",
"_type" : "_doc",
"_id" : "SDsT5HsBssOzZCY8uFEe",
"_score" : 4.6169896,
"_source" : {
"student" : {
"name" : "Manu",
"new_student" : true,
"total_marks" : 0
}
},
"sort" : [
4.6169896,
"Manu"
]
},
{
"_index" : "index32",
"_type" : "_doc",
"_id" : "RzsT5HsBssOzZCY8rVFz",
"_score" : 3.7509375,
"_source" : {
"student" : {
"name" : "Darshan",
"new_student" : false,
"total_marks" : 0
}
},
"sort" : [
3.7509375,
"Darshan"
]
}
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.