POST movies/_search?pretty

{
  "_source": {
    "includes": [
      "*.title",
      "*.release_date",
      "*.running_time_secs"
    ],
    "excludes": [
      "*.actors*",
      "*.genres"
    ]
  },
  "query": {
    "function_score": {
      "query": {
        "match_phrase": {
          "fields.directors": {
            "query": "James Cameron"
          }
        }
      },
      "functions": [
        {
          "gauss": {
            "fields.running_time_secs": {
              "origin": "7200",
              "scale": "1",
              "offset": "0",
              "decay": 0.5
            }
          }
        }
      ]
    }
  }

I need to lower the score of matching documents whose field "running_time_secs" is above 7200. I wrote the query above using decay_function. But apparently decay_funtions are applied to the two sides of the origins (means under 7200 and above 7200 for my example). Is there a way to make it decrease the score in only one direction (means only above 7200)?

Regards,

1 Answers

0
tom slabbaert On

Add a filter to your nested function:

"functions": [
        {
           "filter": {
              "range" : {
                "fields.running_time_secs" : {
                   "gte" : 7200,
              }
            }
          },
          "gauss": {
            "fields.running_time_secs": {
              "origin": "7200",
              "scale": "1",
              "offset": "0",
              "decay": 0.5
            }
          }
        }
      ]

Now the gaussian will only apply to documents with runtime greater than 7200 while other documents will remain unaffected.