我使用的是elasticsearch 7.9.0
。
我的索引有如下檔案
{
"學生"。{
"name": "Guru",
"new_student": true,
"total_marks": 100
}
}
{
"學生": {
"name": "Mayur",
"new_student": false,
"total_marks": 90
}
}
{
"學生": {
"姓名": "Darshan",
"new_student": false,
"total_marks": 0
}
}
{
"學生": {
"name": "Manu",
"new_student": true,
"total_marks": 0
}
}
現在,我的輸出應該包含下面這個結果。
- 所有帶有
"new_student":true的學生。 - 所有具有
"new_student":true和具有"total_marks" > 0的學生。
- 所有學生的
"new_student":false和有"total_marks" > 0。
- 所有學生的
"new_student":true和有"total_marks" = 0。
- 所有
"new_student":false且"total_marks"=0的學生。
我們怎樣才能做到這一點?
我試著在bool查詢中加入與上述欄位分開匹配的引導。
然后我意識到我必須使用student.name進行排序。如果我這樣做,提升將不會有任何效果。
然后我試著用多搜索API。我得到了預期的結果,但后來我意識到分頁在multisearch中是很困難的。
如何解決這個問題?
謝謝
uj5u.com熱心網友回復:
不同的function_score查詢可以被添加到should子句中 每個function_score都會給一個組以相同的分數。 在排序中,分數和名稱可以用來對檔案進行排序。由于每個組有相同的分數,它將被按字母順序排序
。查詢
{
"查詢"。{
"bool": {
"應該"。[
{
"function_score": {
"查詢"。{
"bool": {
"必須"。[
{
"術語": {
"student.new_student": {
"值": true
}
}
},
{
"范圍": {
"student.total_marks": {
"gt": 0
}
}
}
]
}
},
"提升"。"5"
}
},
{
"function_score": {
"查詢"。{
"bool": {
"必須"。[
{
"術語": {
"student.new_student": {
"值": false
}
}
},
{
"范圍": {
"student.total_marks": {
"gt": 0
}
}
}
]
}
},
"提升"。"4"
}
},
{
"function_score": {
"查詢"。{
"bool": {
"必須"。[
{
"術語": {
"student.new_student": {
"值": true
}
}
},
{
"范圍": {
"student.total_marks": {
"gte": 0,
"lte": 0
}
}
}
]
}
},
"提升"。"3"
}
},
{
"function_score": {
"查詢"。{
"bool": {
"必須"。[
{
"術語": {
"student.new_student": {
"值": false
}
}
},
{
"范圍": {
"student.total_marks": {
"gte": 0,
"lte": 0
}
}
}
]
}
},
"提升"。"2"
}
}
]
}
},
"排序"。[
{
"_score": {
"順序"。"desc"
}
},
{
"student.name.keyword": {
"順序": "升"
}
}
]
}
結果
"hits" : [
{
"_index" : "index32",
"_type" : "_doc",
"_id" : "STtN5HsBssOzZCY8olFq",
"_score" : 7.6949825,
"_source" : {
"學生" : {
"name" : "Abc",
"new_student" : true,
"total_marks" : 90
}
},
"排序" : [
7.6949825,
"Abc"
]
},
{
"_index" : "index32",
"_type" : "_doc",
"_id" : "RTsT5HsBssOzZCY8olGD",
"_score" : 7.6949825,
"_source" : {
"學生" : {
"name" : "Guru",
"new_student" : true,
"total_marks" : 100
}
},
"排序" : [
7.6949825,
"Guru"
]
},
{
"_index" : "index32",
"_type" : "_doc",
"_id" : "RjsT5HsBssOzZCY8plFr",
"_score" : 7.501875,
"_source" : {
"學生" : {
"name" : "Mayur",
"new_student" : false,
"total_marks" : 90
}
},
"排序" : [
7.501875,
"馬尤爾"
]
},
{
"_index" : "index32",
"_type" : "_doc",
"_id" : "SDsT5HsBssOzZCY8uFEe",
"_score" : 4.6169896,
"_source" : {
"學生" : {
"name" : "Manu",
"new_student" : true,
"total_marks" : 0
}
},
"排序" : [
4.6169896,
"馬努"
]
},
{
"_index" : "index32",
"_type" : "_doc",
"_id" : "RzsT5HsBssOzZCY8rVFz",
"_score" : 3.7509375,
"_source" : {
"學生" : {
"name" : "Darshan",
"new_student" : false,
"total_marks" : 0
}
},
"排序" : [
3.7509375,
"達山"
]
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/310831.html
標籤:
