Home > database >  How to get some document at bottom in Elasticsearch without using sort?
How to get some document at bottom in Elasticsearch without using sort?

Time:09-17

I am using elasticsearch 7.9.0
The document stored like:

{
  "student": {
    "marks": [
      {
        "sub": 80
      },
      {
        "sub": 90
      },
      {
        "sub": 100
      }
    ],
    "total_marks": 270
  }
}

In case the student absent for the exam my document will be

{
  "student": {
    "marks": [
      {
        "sub": 0
      },
      {
        "sub": 0
      },
      {
        "sub": 0
      }
    ],
    "total_marks": 0
  }
}

Now while searching for the student my hits should contain all the students but the one are absent should come at bottom.
I should not add the sort on total marks.
There are some more fields to be matched and the ordering should be done based on default scoring of elasticsearch. For example topper may come foirst or failed student may also come first, but all the students who are absent should come at last.
How can we achive this?

I have tried adding negetive boosting for totalmarks. It didn't work as expected.
Please help me. Thanks in advance.

CodePudding user response:

Boosting query can be used to demote certain documents

Positive :- I have used match_all to return all documents.

Negative :- Applies to subset of documents filtered from positive query i.e negative documents must also be returned in positive query.

Query

{
  "query": {
    "boosting": {
      "positive": {
        "match_all": {}
      },
      "negative": {
        "range": {
          "student.total_marks": {
            "gte": 0,
            "lte": 0
          }
        }
      },
      "negative_boost": 0.2,
      "boost": 1
    }
  }
}

Result

"hits" :
 [
      {
        "_index" : "index32",
        "_type" : "_doc",
        "_id" : "RTsT5HsBssOzZCY8olGD",
        "_score" : 1.0,
        "_source" : {
          "student" : {
            "name" : "Guru",
            "new_student" : true,
            "total_marks" : 100
          }
        }
      },
      {
        "_index" : "index32",
        "_type" : "_doc",
        "_id" : "RjsT5HsBssOzZCY8plFr",
        "_score" : 1.0,
        "_source" : {
          "student" : {
            "name" : "Mayur",
            "new_student" : false,
            "total_marks" : 90
          }
        }
      },
      {
        "_index" : "index32",
        "_type" : "_doc",
        "_id" : "STtN5HsBssOzZCY8olFq",
        "_score" : 1.0,
        "_source" : {
          "student" : {
            "name" : "Abc",
            "new_student" : true,
            "total_marks" : 90
          }
        }
      },
      {
        "_index" : "index32",
        "_type" : "_doc",
        "_id" : "RzsT5HsBssOzZCY8rVFz",
        "_score" : 0.2,
        "_source" : {
          "student" : {
            "name" : "Darshan",
            "new_student" : false,
            "total_marks" : 0
          }
        }
      },
      {
        "_index" : "index32",
        "_type" : "_doc",
        "_id" : "SDsT5HsBssOzZCY8uFEe",
        "_score" : 0.2,
        "_source" : {
          "student" : {
            "name" : "Manu",
            "new_student" : true,
            "total_marks" : 0
          }
        }
    }
]
  • Related