Peter Hayduk
Peter Hayduk

Reputation: 65

elasticsearch Is it possible to combine score_mode for function_score query

I have a function_score query with 2 decay functions (both gauss) and script_score function. In the script_score function I add and multiply a few scores. Now I want to multiply the result with on gauss function (the location) and then add it together with the other gauss function (the creation_date). I want to do that to give the newer documents a boost.

How can I achieve this? With the score_mode of the function_score query I can only multiply or sum.

{
  "query": {
    "function_score": {
      "functions": [
        {
          "gauss": {
            "location": {
              "origin": {
                "lon": 16.37,
                "lat": 48.21
              },
              "scale": "100km",
              "offset": "15km",
              "decay": 0.3
            }
          }
        },
        {
          "gauss": {
            "creation_date": {
              "scale": "30d",
              "offset": "20d",
              "decay": 0.1
            }
          }
        },
        {
          "script_score": {
            "lang": "expression",
            "script": "(((doc['value_a'].value + doc['value_b'] + 1) * boost_a) + (ln(sqrt(doc['value_c'].value + 1)) * boost_c))",
            "params": {
              "boost_a": 0.2,
              "boost_b": 0.5
            }
          }
        }
      ],
      "query": {
        "match_all": {}
      },
      "score_mode": "multiply",
      "boost_mode": "multiply"
    }
  },
  "sort": {
    "_score": "desc"
  }
}

Thanks in advance.

Upvotes: 0

Views: 1177

Answers (1)

Peter Hayduk
Peter Hayduk

Reputation: 65

I found a solution.

{
  "query": {
    "function_score": {
      "functions": {
        "gauss": {
          "creation_date": {
            "scale": "30d",
            "offset": "20d",
            "decay": 0.1
          }
        }
      },
      "query": {
        "function_score": {
          "functions": [
            {
              "gauss": {
                "location": {
                  "origin": {
                    "lon": 16.37,
                    "lat": 48.21
                  },
                  "scale": "100km",
                  "offset": "15km",
                  "decay": 0.3
                }
              }
            },
            {
              "script_score": {
                "lang": "expression",
                "script": "(((doc['value_a'].value + doc['value_b'] + 1) * boost_a) + (ln(sqrt(doc['value_c'].value + 1)) * boost_c))",
                "params": {
                  "boost_a": 0.2,
                  "boost_b": 0.5
                }
              }
            }
          ],
          "query": {
            "match_all": {}
          },
          "score_mode": "multiply",
          "boost_mode": "multiply"
        }
      }
    },
    "score_mode": "multiply",
    "boost_mode": "sum"
  },
  "sort": {
    "_score": "desc"
  }
}

Upvotes: 4

Related Questions