Home > Enterprise >  Custom sort after score calculation and the value of specific field
Custom sort after score calculation and the value of specific field

Time:08-05

Need help to understand sorting in a query where _score is either 0 or 1 based on some conditions but there is also an additional_filed type integer, sort should be in the order of _score * additional_field

sample index

PUT /product_t
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "placed" :{
        "type": "integer"
        
      },
      "store" : {
        "type": "nested"
      }
    }
  }
}

sample document

PUT /product_t/_doc/10
{
  "name": "awesome",
  "count_sold": 199,
  "stock": [
    {
      "id": 1,
      "count": 10
    },
    {
      "id": 2,
      "count": 5
    },
    {
      "id": 3,
      "count": 0
    }
  ]
  
}

Query Used :

POST /product_t/_search
{
  "from": 0,
  "size": 100,
  "timeout": "300ms",
  "query": {
    "bool": {
      "filter": [
        {
          "match": {
            "name": {
              "value": "awesome"
            }
          }
        },
        {
          "nested": {
            "path": "stock",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "stock.id": 3
                    }
                  }
                ]
              }
            }
          }
        }
      ],
      "should": [
        {
          "constant_score": {
            "filter": {
              "nested": {
                "path": "stock",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "stock.id": 3
                        }
                      },
                      {
                        "range": {
                          "stock.count": {
                            "gt": 0
                          }
                        }
                      }
                    ]
                  }
                }
              }
            },
            "boost": 1
          }
        },
        {
          "constant_score": {
            "filter": {
              "nested": {
                "path": "stock",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "stock.id": 3
                        }
                      },
                      {
                        "range": {
                          "stock.count": {
                            "lte": 0
                          }
                        }
                      }
                    ]
                  }
                }
              }
            },
            "boost": 0
          }
        }
      ]
    }
  }
}


count_sold is additional field

CodePudding user response:

You can use script sort

"sort": {
    "_script": {
      "type": "number",
      "script": {
        "lang": "painless",
        "source": "doc['count_sold'].value * _score"
      },
      "order": "asc"
    }
  }
  • Related