Home > Blockchain >  Scoring in Elastic search
Scoring in Elastic search

Time:08-04

I have a constant_score query with filter but need a lower score if all the condition of filter in constant_score are matching but also an additional check is passing

Index looks like this

{
"mappings": {
 "properties": {
   "name": {
     "type": "text"
   },
   "expiry" : {
     "type": "nested"
   }
 }
}
}

Sample document is:

{
  "name": "Awesome",
  "expiry": [
    {
      "batchNo": 1,
      "expiryInDays": 10
    },
    {
      "batchNo": 2,
      "expiryInDays": 20
    },
    {
      "batchNo": 3,
      "expiryInDays": 0
    }
  ]
}

Constant score query is:

{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": [
            {
              "match": {
                "name": "awesome"
              }
            },
            {
              "nested": {
                "path": "expiry",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "expiry.batchNo": 3
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      },
      "boost": 1
    }
  }
}

Looking to create query which can score the record with expiry.expiryInDays = 0 along with the condition in filter with lower score than only with filter condition

CodePudding user response:

Constant score query can be wrapped in should clause to get two set of scores.

Query

{
  "query": {
    "bool": {
      "filter": [
        {
          "match": {
            "name": "awesome"
          }
        },
        {
          "nested": {
            "path": "expiry",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "expiry.batchNo": 3
                    }
                  }
                ]
              }
            }
          }
        }
      ],
      "should": [
        {
          "constant_score": {
            "filter": {
              "nested": {
                "path": "expiry",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "expiry.batchNo": 3
                        }
                      },
                      {
                        "range": {
                          "expiry.expiryInDays": {
                            "gt": 0
                          }
                        }
                      }
                    ]
                  }
                }
              }
            },
            "boost": 2
          }
        },
        {
          "constant_score": {
            "filter": {
              "nested": {
                "path": "expiry",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "expiry.batchNo": 3
                        }
                      },
                      {
                        "range": {
                          "expiry.expiryInDays": {
                            "lte": 0
                          }
                        }
                      }
                    ]
                  }
                }
              }
            },
            "boost": 1
          }
        }
      ]
    }
  }
}

Result

"hits" : [
      {
        "_index" : "test6",
        "_type" : "_doc",
        "_id" : "ZGQcZIIBcSdWVUHM9rQD",
        "_score" : 2.0,
        "_source" : {
          "name" : "Awesome",
          "expiry" : [
            {
              "batchNo" : 1,
              "expiryInDays" : 10
            },
            {
              "batchNo" : 2,
              "expiryInDays" : 20
            },
            {
              "batchNo" : 3,
              "expiryInDays" : 5
            }
          ]
        }
      },
      {
        "_index" : "test6",
        "_type" : "_doc",
        "_id" : "YmQcZIIBcSdWVUHMurTD",
        "_score" : 1.0,
        "_source" : {
          "name" : "Awesome",
          "expiry" : [
            {
              "batchNo" : 1,
              "expiryInDays" : 10
            },
            {
              "batchNo" : 2,
              "expiryInDays" : 20
            },
            {
              "batchNo" : 3,
              "expiryInDays" : 0
            }
          ]
        }
      },
      {
        "_index" : "test6",
        "_type" : "_doc",
        "_id" : "ZWQ4ZIIBcSdWVUHMcrRa",
        "_score" : 1.0,
        "_source" : {
          "name" : "Awesome",
          "expiry" : [
            {
              "batchNo" : 1,
              "expiryInDays" : 10
            },
            {
              "batchNo" : 2,
              "expiryInDays" : 20
            },
            {
              "batchNo" : 3,
              "expiryInDays" : 0
            }
          ]
        }
      },
      {
        "_index" : "test6",
        "_type" : "_doc",
        "_id" : "ZmQ4ZIIBcSdWVUHMkLQ-",
        "_score" : 1.0,
        "_source" : {
          "name" : "Awesome",
          "expiry" : [
            {
              "batchNo" : 1,
              "expiryInDays" : 10
            },
            {
              "batchNo" : 2,
              "expiryInDays" : 50
            },
            {
              "batchNo" : 3,
              "expiryInDays" : 0
            }
          ]
        }
      }
    ]
  • Related