乐途乐途
主页
  • 计算机基础

    • TCP/IP
    • Linux
    • HTTP
  • 数据库

    • SQL
    • MySQL 5.7
  • 编程语言

    • C
    • C++
    • Java SE
    • Python2
    • Python3
  • 数据格式

    • JSON
    • XML
  • 认证与安全

    • JWT
  • 工具

    • Markdown
  • Git

    • GitFlow
  • Quartz

    • Quartz
  • Java

    • Maven 入门
    • Maven 进阶
    • MyBatis
    • Spring
    • Spring MVC
  • Java

    • Spring Boot
    • Spring Cloud
    • Spring Cloud Alibaba
    • Spring Security
    • Spring AI
    • Spring Batch
    • Kafka
    • Java 设计模式
  • 缓存

    • Redis
  • 搜索引擎

    • Elasticsearch
  • 分布式协调

    • ZooKeeper
联系
阿里云
主页
  • 计算机基础

    • TCP/IP
    • Linux
    • HTTP
  • 数据库

    • SQL
    • MySQL 5.7
  • 编程语言

    • C
    • C++
    • Java SE
    • Python2
    • Python3
  • 数据格式

    • JSON
    • XML
  • 认证与安全

    • JWT
  • 工具

    • Markdown
  • Git

    • GitFlow
  • Quartz

    • Quartz
  • Java

    • Maven 入门
    • Maven 进阶
    • MyBatis
    • Spring
    • Spring MVC
  • Java

    • Spring Boot
    • Spring Cloud
    • Spring Cloud Alibaba
    • Spring Security
    • Spring AI
    • Spring Batch
    • Kafka
    • Java 设计模式
  • 缓存

    • Redis
  • 搜索引擎

    • Elasticsearch
  • 分布式协调

    • ZooKeeper
联系
阿里云
  • 学习路径
  • 全文搜索与Elasticsearch概述

    • 全文搜索与搜索引擎概述
    • Elasticsearch 与 Lucene 的关系
    • Elasticsearch 核心架构
  • 安装与快速上手

    • 安装 Elasticsearch
    • 快速上手
  • 索引、文档与映射

    • 索引
    • 文档与 CRUD
    • 元数据字段
    • 字段映射
    • 动态映射
    • Reindex 操作
  • 文本分析与分词

    • 倒排索引的构建与原理
    • 分词器组成
    • IK 分词器与自定义词典
    • 近实时搜索原理
    • Ingest Pipeline 数据预处理
  • 搜索基础(Query DSL)

    • match 查询
    • multi_match 多字段查询
    • match_phrase 短语匹配
    • term 与 terms 查询
    • range 范围查询
    • bool 查询
    • 高亮显示与分页
    • 深度分页与遍历
  • 相关性评分与排序

    • BM25 相关性评分
    • 排序与自定义评分
    • Painless 脚本基础
  • 聚合分析(Aggregations)

    • 指标聚合
    • 桶聚合
    • 管道聚合
  • 分布式集群原理

    • 节点与角色
    • 分片与副本
    • 路由
    • 选举与脑裂
    • 快照与恢复
  • 数据建模与最佳实践

    • 索引设计原则与模板
    • 嵌套对象与父子文档
  • 性能优化与运维监控

    • 写入性能优化
    • 搜索性能优化
    • ILM 索引生命周期管理
    • 集群监控与故障排查
  • 面试考点与实战场景

    • 实战案例:课程搜索引擎
    • Elasticsearch 面试考点精选

Painless 脚本基础

本章定位:当 BM25 不能满足排序需求时,Painless 脚本让你用代码干预评分和字段计算。它是 function_score 和 script_score 的执行引擎。


定义与作用

Painless 是 Elasticsearch 内置的安全脚本语言,语法类似 Java,支持在搜索(script_score)、更新(ctx._source)、聚合(scripted metric)和 Ingest Pipeline 中使用。

解决的痛点:标准评分无法表达业务逻辑(如"评分=BM25 × 销量权重"),Painless 脚本让你自定义计算。


完整示例:小崔的自定义评分

POST /courses/_search
{
  "query": {
    "script_score": {
      "query": { "match": { "title": "Python" } },
      "script": {
        "source": """
          double score = _score;
          double rating = doc['rating'].value;
          double enrollment = Math.log1p(doc['enrollment_count'].value);
          return score * (rating * 0.6 + enrollment * 0.4);
        """
      }
    }
  }
}

Update 中使用

POST /students/_update/1
{
  "script": {
    "source": "ctx._source.score += params.increment",
    "params": { "increment": 5 }
  }
}

小结

Painless 三使用场景:script_score(自定义评分)、update(字段计算)、aggregation(脚本聚合)。注意避免大量文档脚本计算(性能杀手),优先用 field_value_factor。

上一页
排序与自定义评分