Elasticsearch:script_score使用嵌套值,标记强度,嵌套文档

时间:2014-10-15 22:16:31

标签: elasticsearch

我正在尝试实现功能评分查询。 Similar to this post here,仅使用新的function_score,而不是旧版custom_score

问题: 我无法从我的脚本中获取值(始终返回0)。这使我相信doc['weight'].value尝试访问代码的权重是不正确的。

我有三个类似于此的文档(全部来自现有的SO帖子):

{
    "title": "3",
    "tags": [{
        "tag": "B",
        "weight": 16
    }, {
        "tag": "D",
        "weight": 4
    }]
}

我的查询:

GET test-idx/doc/_search
{
  "query": {
    "function_score": {
      "query": {
        "nested": {
          "path": "tags",
          "query": {
            "terms": {
              "tag": [
                "C",
                "B"
              ],
              "minimum_match": 1
            }
          }
        }
      },
      "functions": [
          { "script_score": { "script": "doc['weight'].value" } }
      ],
      "score_mode": "sum"
    }
  }
}

我的结果:

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 0,
      "hits": [
         {
            "_index": "test-idx",
            "_type": "doc",
            "_id": "1",
            "_score": 0,
            "_source": {
               "title": "1",
               "tags": [
                  {
                     "tag": "A",
                     "weight": 1
                  },
                  {
                     "tag": "B",
                     "weight": 2
                  },
                  {
                     "tag": "C",
                     "weight": 4
                  }
               ]
            }
         },
         {
            "_index": "test-idx",
            "_type": "doc",
            "_id": "2",
            "_score": 0,
            "_source": {
               "title": "2",
               "tags": [
                  {
                     "tag": "B",
                     "weight": 2
                  },
                  {
                     "tag": "C",
                     "weight": 3
                  }
               ]
            }
         },
         {
            "_index": "test-idx",
            "_type": "doc",
            "_id": "3",
            "_score": 0,
            "_source": {
               "title": "3",
               "tags": [
                  {
                     "tag": "B",
                     "weight": 16
                  },
                  {
                     "tag": "D",
                     "weight": 4
                  }
               ]
            }
         }
      ]
   }
}

1 个答案:

答案 0 :(得分:0)

基本上,这只是一个形式错误的查询。 这按预期工作。杜!

GET test-idx/doc/_search
{
"query": {
  "nested": {
    "path": "tags",
    "score_mode": "avg", 
    "query": {
      "function_score": {
        "query": {
            "terms": {
              "tag": [
                "C",
                "A",
                "B"
              ],
              "minimum_match": 1
            }
        },
        "script_score": {
          "script": "_score * doc['weight'].value"
        }
      }
    }
  }
}
}