Elastic 5.3 Native Script实现

本文介绍了如何在Elasticsearch 5.3中使用Native Scripts实现自定义排序。通过编写Java代码并配置插件,实现根据特定规则计算score进行文档排序。文章详细阐述了从Java代码实现、配置文件设置到编译打包、安装插件的全过程,并提供了自定义排序测试案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Native (Java) Scripts

Elastic 提供丰富的排序,大部分基于TF/IDF计算score。然后有时业务需要自定义排序,就是根据一个规则来计算score,然后根据这个score进行排序。目前实现自定义排序有两种方案:
- Function Score
- Script
1.1 Groovy scripts
1.2 Native Scripts

本文重点介绍以Native Scripts插件的形式实现Elatic 自定义排序。

注意:Elastic版本更新比较快,在不同版本实现方式不一样。在参考本文时候注意Elastic的版本。Native Scripts在5.0~5.4可以正常使用,在5.5版本中被弃用,6.0版本完全被移除。使用Elastic 5.5版本以上需要使用ScriptEngine。
- Sometimes groovy and expression aren’t enough. For those times you can implement a native script.
- Native Scripts were deprecated in v5.5.0 and removed in v6.0.0。Consider migrating your native scripts to the ScriptEngine.

自定义排序实现

使用ScriptPlugin插件实现一个简单排序:
定义一个”feature”字段,而该字段的打分规则由我们自己制定。其规则如下:
- 如果查询字段feature与被查询字段feature长度相等,此时被查询的文档得分90
- 如果查询字段feature长度比被查询字段feature长度小,此时被查询的文档得分60
- 如果查询字段feature长度比被查询字段feature长度大,此时被查询的文档得分30

Java代码实现

参考 Native(Java)Scripts帮助文档, 其代码实现如下:

package com.zz.localservice.es.plugin;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.ScriptPlugin;
import org.elasticsearch.script.AbstractDoubleSearchScript;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.NativeScriptFactory;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: smartfly2017
 * Date: 2017/10/16
 * Time: 15:53
 * Description:
 * To change this template use File | Settings | File Templates | Includes | File Header
 */
public class MyNativeScriptPlugin extends Plugin implements ScriptPlugin{
   
   

    private final static Logger logger = LogManager.getLogger(MyNativeScriptPlugin.class);

    @Override
    public List<NativeScriptFactory> getNativeScripts() {
        return Collections.singletonList(new MyNativeScriptFactory());
    }

    public static class MyNativeScriptFactory implements NativeScriptFactory {
   
   
        @Override
        public ExecutableScript newScript(@Nullable Map<String, Object> params) {
            String feature = params == null ? null : XContentMapValues.nodeStringValue(params.get("feature"), null);
            if (feature == null){
                logger.info("feature is null!");
            }
            return new MyNativeScript(feature);
        }
        @Override
        public boolean needsScores() {
            return false;
        }
        @Override
        public String getName() {
            return "my_script";
        }
    }

    public static class MyNativeScript extends AbstractDoubleSearchScript {
   
   

        private final String feature;

        public MyNativeScript(String feature) {
            this.feature = feature;
        }

        @Override
        public double runAsDouble() {
            String sourceFeature = (String) source().get("feature");
            int len1 = feature.length();
            int len2 = sourceFeature.length();
            if (len1 == len2){
                return 90;
            } else if (len1 < len2){
                return 60
### 安装 Elasticsearch 5.3 并配置集群 #### 下载与安装 为了在 CentOS 上安装 Elasticsearch 5.3 版本,首先需要下载对应的版本文件。可以通过官方提供的链接获取适用于 Linux 的压缩包[^1]。 ```bash wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.3.0.rpm sudo rpm -ivh elasticsearch-5.3.0.rpm ``` 完成上述命令后,Elasticsearch 将被成功安装到系统中。 #### 修改配置文件 编辑 `/etc/elasticsearch/elasticsearch.yml` 文件来设置集群名称以及节点名称: ```yaml cluster.name: my_cluster_name node.name: node_1 network.host: 0.0.0.0 discovery.zen.ping.unicast.hosts: ["host1", "host2"] ``` 这里 `my_cluster_name` 是自定义的集群名字;`node_1` 表示当前机器作为第一个节点的名字;`network.host` 设置为 `0.0.0.0` 允许外部访问;最后指定其他成员地址用于发现新加入的节点。 对于多台服务器组成的集群而言,在每台主机上的配置应保持一致除了 `node.name` 和实际 IP 地址外。 #### 启动服务并验证状态 启动 Elasticsearch 服务,并将其设为开机自动运行: ```bash sudo systemctl start elasticsearch.service sudo systemctl enable elasticsearch.service ``` 通过下面这条命令可以查看正在运行的服务实例的状态: ```bash curl &#39;localhost:9200/_cat/nodes?v&#39; ``` 如果一切正常,则会返回有关节点的信息列表。 #### 配置防火墙规则 确保允许必要的端口通信以便于不同节点间的数据交换。通常情况下,默认监听的是 TCP 协议下的 9200 至 9300 端口号范围内的请求。 ```bash firewall-cmd --zone=public --add-port=9200/tcp --permanent firewall-cmd --reload ``` 以上就是关于如何在 CentOS 中安装 Elasticsearch 5.3 及其基本集群配置方法介绍。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值