Home > Back-end >  Sort inside groups of results in elasticsearch
Sort inside groups of results in elasticsearch

Time:09-17

I am using elasticsearch 7.9.0
My index have documents like

{
  "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
  }
}

Now my output should contain the result in the below ordeer.\

  1. All the students with "new_student":true and having "total_marks" > 0
  2. All the students with "new_student":false and having "total_marks" > 0
  3. All the students with "new_student":true and having "total_marks" = 0
  4. All the students with "new_student":false and having "total_marks" = 0

How can we achive this?
I tried adding boot to the bool query matching the above fields seperatly.
Then i realized I have to sort using student.name. If I do so the boost will not have any effect.

Then I tried multisearch API. I was getting the expected result but then I realized pagination is dificult in multisearch.

How to solve this?
Thanks

CodePudding user response:

Different function_score queries can be added in should clause Each function_score will give same score to a group. In sort, score and name can be used to order documents. Since each group has same score, it will get alphabetically sorted

Query

{
  "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"
      }
    }
  ]
}

Result

  "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"
        ]
      }
    ]
  • Related