【问题标题】:elasticsearch - get intermediate scores within 'function_score'elasticsearch - 在“function_score”中获得中间分数
【发布时间】:2020-03-08 05:35:35
【问题描述】:

这是我的索引

POST /blogs/1
{
  "name" : "learn java", 
  "popularity" : 100
}

POST /blogs/2
{
  "name" : "learn elasticsearch", 
  "popularity" : 10
}

我的搜索查询:

GET /blogs/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "name": "learn"
        }
      },
      "script_score": {
        "script": {
          "source": "_score*(1+Math.log(1+doc['popularity'].value))"
        }
      }
    }
  }
}

返回:

[
  {
    "_index": "blogs",
    "_type": "1",
    "_id": "AW5fxnperVbDy5wjSDBC",
    "_score": 0.58024323,
    "_source": {
      "name": "learn elastic search",
      "popularity": 100
    }
  },
  {
    "_index": "blogs",
    "_type": "1",
    "_id": "AW5fxqmL8cCMCxtBYOyC",
    "_score": 0.43638366,
    "_source": {
      "name": "learn java",
      "popularity": 10
    }
  }
]

问题:我需要在结果中返回一个额外的字段,这会给我原始分数(只是 tf/idf,它没有考虑流行度)

我探索过的东西:script_fields(在获取时无法访问_score

【问题讨论】:

    标签: elasticsearch search elasticsearch-aggregation


    【解决方案1】:

    问题在于您查询的方式,它覆盖了_score 变量。相反,如果您使用 sort,则 _score 不会更改,并且可以在同一查询中提取。

    你可以试试这样查询:

    {
      "query": {
        "match": {
          "name": "learn"
        }
      },
      "sort": [
        {
          "_script": {
            "type": "number",
            "script": {
              "lang": "painless",
              "source": "_score*(1+Math.log(1+doc['popularity'].value))"
            },
            "order": "desc"
          }
        },
        "_score"
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-20
      • 2016-02-01
      • 2018-10-07
      • 2018-03-11
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 2019-09-03
      相关资源
      最近更新 更多