You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Elasticsearch 6转8迁移:已弃用SearchScript的替代类咨询

Elasticsearch 8.x 替代已弃用的SearchScript类方案

在Elasticsearch 8.x版本中,SearchScript类已被完全弃用,替代方案需基于ES 8的统一脚本体系,核心要点如下:

  • 基础替代类:使用Script类作为统一的脚本入口,通过指定ScriptContext.Standard.SEARCH上下文来适配搜索场景,替代原SearchScript的基础调用逻辑。
  • 自定义脚本实现:若需编写自定义搜索脚本,不再直接继承SearchScript,而是根据具体场景实现对应细分接口:
    • 评分脚本:实现ScoreScript及ScoreScript.Factory接口
    • 字段组合脚本:实现CompositeFieldScript及CompositeFieldScript.Factory接口
    • 通用搜索脚本:实现ScriptFactory并关联ScriptContext.Standard.SEARCH上下文

代码示例(评分脚本迁移)

原ES 6.x自定义SearchScript实现:

public class CustomSearchScript extends SearchScript {
    @Override
    public double runAsDouble() {
        return doc("field").value() * 2;
    }
}

ES 8.x中对应的替代实现:

public class CustomScoreScript implements ScoreScript {
    private final DocValues fieldValues;

    public CustomScoreScript(LeafReaderContext context, Map<String, Object> params) throws IOException {
        this.fieldValues = context.reader().getDocValues("field");
    }

    @Override
    public double execute(Scorer scorer) throws IOException {
        int docId = scorer.docID();
        return fieldValues.longValue(docId) * 2;
    }

    public static class Factory implements ScoreScript.Factory {
        @Override
        public ScoreScript newInstance(LeafReaderContext context, Map<String, Object> params, SearchLookup lookup) throws IOException {
            return new CustomScoreScript(context, params);
        }
    }
}

脚本注册

在自定义插件中注册脚本工厂,关联对应上下文:

@Override
public List<ScriptPlugin> getScriptPlugins() {
    return List.of(new ScriptPlugin() {
        @Override
        public Map<String, ScriptFactory> getScriptFactories() {
            return Map.of("custom-score-script", new CustomScoreScript.Factory());
        }
    });
}

内容的提问来源于stack exchange,提问作者Ulysse

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 05:06:25