Elasticsearch:使用标签进行自定义评分

时间:2014-05-15 08:30:23

标签: elasticsearch

我有以下形式的文件:

{
    "content": "lorem ipsum...",
    "tags": [
        { "tag": "red", weight: 3 },
        { "tag": "green", weight: 1 },
    ]
}

我想搜索带有全文搜索的文档,并根据标记匹配更改分数。

假设我正在搜索短语ipsum和标记red。对于上述文档,文本搜索会产生2的分数。该文档的标记red的权重为3,因此我希望将得分乘以此权重,得分为6

我目前的做法是使用script_score

{
  "query": {
    "function_score": {
      "query": {
        "match": { ... }
      },
      "functions": [
        {
          "filter": {
            "nested": {
              "path": "tags",
              "filter": {
                "term": {
                  "key": "red"
                }
              }
            }
          },
          "script_score": {
            "script": "_score * doc['tags']['red'].weight"
          }
        },
        {
          "script_score": {
            "script": "_score"
          }
        }
      ],
      "score_mode": "first"
    }
  }
}

无效的部分是"script": "_score * doc['tags']['red'].weight"

我如何编写脚本来访问权重?

0 个答案:

没有答案