How do I conditionally call a gauss function in Elasticsearch?

110 Views Asked by At

I have a search index for my website which has (among others) the following fields:

type title body published_at

What I want to do is execute the following gauss function only when type='article':

        {
          "gauss": {
            "published_at": {
              "scale": "14d",
              "offset": "7d",
              "decay": 0.95
            }
          }
        },

Basically, I want to keep all types of search results relevant, but articles are allowed to age and slowly fall down the ranking.

Can anyone help me with this, because I cannot work it out.

Here is the complete function_score, which includes weighting for different values of type:

    "function_score": {
      "functions": [
        {
          "gauss": {
            "published_at": {
              "scale": "14d",
              "offset": "7d",
              "decay": 0.95
            }
          }
        },
        {
          "filter": {
            "match": {
              "type": "programme"
            }
          },
          "weight": 60
        },
        {
          "filter": {
            "match": {
              "type": "podcast"
            }
          },
          "weight": 50
        },
        {
          "filter": {
            "match": {
              "type": "article"
            }
          },
          "weight": 40
        },
        {
          "filter": {
            "match": {
              "type": "clip"
            }
          },
          "weight": 30
        }
      ],
1

There are 1 best solutions below

1
On

You simply need to move the gauss function to where you have the filter on type: article:

  "function_score": {
    "functions": [
      {
        "filter": {
          "match": {
            "type": "programme"
          }
        },
        "weight": 60
      },
      {
        "filter": {
          "match": {
            "type": "podcast"
          }
        },
        "weight": 50
      },
      {
        "filter": {
          "match": {
            "type": "article"
          }
        },
        "gauss": {
          "published_at": {
            "scale": "14d",
            "offset": "7d",
            "decay": 0.95
          }
        },
        "weight": 40
      },
      {
        "filter": {
          "match": {
            "type": "clip"
          }
        },
        "weight": 30
      }
    ]
  }