如何在 Elasticsearch 中有条件地调用高斯函数?

问题描述

我的网站有一个搜索索引,其中包含(除其他外)以下字段:

类型 标题 身体 已发布

我想要做的是仅在 type='article' 时执行以下高斯函数

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

基本上,我想保持所有类型的搜索结果相关,但允许文章老化并慢慢降低排名。

谁能帮我解决这个问题,因为我无法解决

这是完整的 function_score,其中包括不同类型值的权重:

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

解决方法

您只需将 gauss 函数移动到 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
      }
    ]
  }