elasticsearch - elasticsearch - 在“function_score”中获得中间分数
问题描述
这是我的索引
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
(在获取时不提供访问权限。
解决方案
问题在于您查询的方式,它会覆盖_score
变量。相反,如果您使用sort
then_score
不会更改并且可以在同一查询中提取。
您可以尝试这样查询:
{
"query": {
"match": {
"name": "learn"
}
},
"sort": [
{
"_script": {
"type": "number",
"script": {
"lang": "painless",
"source": "_score*(1+Math.log(1+doc['popularity'].value))"
},
"order": "desc"
}
},
"_score"
]
}
推荐阅读
- python - 无法在 Python 中使用 __repr__ 打印对象 repr
- typescript - 使用 Typescript 在运行时动态增长对象类型
- google-apps-script - 如何在保持文本样式的同时替换 Google doc 中的单词?
- python-3.x - 如何将同一破折号(情节)应用程序布局的部分调用到网站的各个部分
- python - 当管道 linux 命令时,如何避免管道 python 打印
- .net - 澄清 - 我可以将带有 odp.net 12c 的 .net 应用程序连接到 Oracle 19c 数据库吗?
- javascript - 如何将 mp3 文件从 Java 后端发送到前端并在 Vue.js 前端播放文件
- php - 在结帐重定向到错误的 URL 之前检查并重定向以登录
- python - 如何使用 Tkinter 使用下拉菜单打开文件夹中的文件
- ios - 安装新的 iOS 应用后接收相同的 FCM 令牌