当前位置: 首页 > news >正文

Elastic script_score的使用

script_score介绍

在Elasticsearch中,script_score是在function_score查询中的一种功能强大的方式,允许用户使用内置Painless脚本语言或者其他支持的语言来动态计算每个文档的评分

script_score语法

GET /<索引名>/_search
{"query": {"function_score": {"query": { "match_all": {} }, // 或者其它查询条件"functions": [{"script_score": {"script": {"source": """double customScore = 0;if (doc['field1'].value > params.threshold1) {customScore += doc['field1'].value * params.multiplier1;}customScore += doc['field2'].value;return customScore;""","params": {"threshold1": 50,"multiplier1": 0.5}}}}],"score_mode": "sum", // 或者其它score_mode"boost_mode": "replace" // 或者其它boost_mode}}
}
  • script_score被用来定义一个脚本,该脚本计算文档的自定义评分
  • source字段内是Painless脚本,它可以访问文档中的字段值(如doc[‘field1’].value和doc[‘field2’].value)并对它们进行计算
  • params是一个键值对对象,用于传递给脚本的参数,此处定义了两个参数:threshold1和multiplier1

script_score 案例

场景

假设我们有一个问答论坛索引,需要基于回答数量和点赞数查找高质量

索引创建

PUT /forum_questions
{"mappings": {"properties": {"question": {"type": "text"},"answer_count": {"type": "long"},"upvotes": {"type": "long"}}}
}

文档插入

POST /forum_questions/_doc/
{"question": "What is Elasticsearch?","answer_count": 5,"upvotes": 20
}POST /forum_questions/_doc/
{"question": "How to configure Elasticsearch for production?","answer_count": 3,"upvotes": 15
}POST /forum_questions/_doc/
{"question": "Best practices for indexing data in Elasticsearch?","answer_count": 10,"upvotes": 30
}POST /forum_questions/_doc/
{"question": "How to optimize Elasticsearch performance?","answer_count": 8,"upvotes": 18
}POST /forum_questions/_doc/
{"question": "What are shards and replicas in Elasticsearch?","answer_count": 6,"upvotes": 25
}POST /forum_questions/_doc/
{"question": "How to handle time-based data in Elasticsearch?","answer_count": 4,"upvotes": 12
}POST /forum_questions/_doc/
{"question": "What is the difference between match and term queries?","answer_count": 7,"upvotes": 23
}POST /forum_questions/_doc/
{"question": "How to set up Elasticsearch clusters?","answer_count": 9,"upvotes": 16
}

查询

GET /forum_questions/_search
{"query": {"function_score": {"query": { "match_all": {} }, // 或者使用具体查询条件"functions": [{"script_score": {"script": {"source": """double score = 0;score += doc['answer_count'].value * params.answer_weight;score += doc['upvotes'].value * params.upvote_weight;return score;""","params": {"answer_weight": 0.7,"upvote_weight": 0.3}}}}],"score_mode": "sum"}}
}
http://www.lryc.cn/news/317574.html

相关文章:

  • 【Spring Boot 3】获取已注入的Bean
  • C# 对于点位置的判断
  • 最新CLion + STM32 + CubeMX 开发环境搭建
  • 【Python3】观察者模式
  • HTML5 Web Worker之性能优化
  • 应对恶意IP攻击的有效方法
  • 如何使用“Docker registry创建本地仓库,在服务器之间进行文件push和pull”?
  • Rocky Linux - Primavera P6 EPPM 安装及分享
  • API 管理调研
  • 在centOS服务器安装docker,并使用docker配置nacos
  • JVM运行时数据区概述以及分别存放的内容
  • 数据体系规范化
  • 从政府工作报告探计算机行业发展
  • 【软件工具】网络性能测试工具 Iperf
  • Day32:安全开发-JavaEE应用Servlet路由技术JDBCMybatis数据库生命周期
  • C语言下使用SQL语言
  • Gitea相关漏洞
  • 基于深度学习的图像去雨去雾
  • 使用JS的for循环实现九九乘法表
  • Leetcode 70 爬楼梯
  • 基于SpringBoot+MYSQL+Vue的校园管理系统
  • Oracle P6 负浮时和必须完成日期
  • 【C++】STL--String
  • 深入理解与使用go之中间件--实现
  • 移动端研发技术的进化历程
  • ChromeDriver 122 版本为例 国内下载地址及安装教程
  • 【数据结构】双向链表及LRU缓存的实现
  • 2、计划任务不显示UI的问题
  • 学C还是学C++?
  • Springboot参数分组校验