elasticsearch 打分插件

发布时间:2020-07-14 19:01:52 作者:大海之中
来源:网络 阅读:1532

插件常用命令

./plugin list/remove/install



plugin.xml 代码

<?xml version="1.0"?>
<assembly>
    <id>plugin</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <files>
        <file>
            <source>${project.basedir}/src/main/resources/plugin-descriptor.properties</source>
            <outputDirectory>elasticsearch</outputDirectory>
            <filtered>true</filtered>
        </file>
    </files>
    <dependencySets>
        <dependencySet>
            <outputDirectory>elasticsearch</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <useTransitiveFiltering>true</useTransitiveFiltering>
        </dependencySet>
    </dependencySets>
</assembly>


plugin-descriptor.properties 代码

description=my native script that does something great
version=1.0
name=my-native-scrip
classname=com.*.score.plugin.MyNativeScriptPlugin
java.version=1.8
elasticsearch.version=*
jvm=true


pom.xml 代码

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.*.es</groupId>
  <artifactId>score-plugins</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <name>score-plugins</name>
  
  
  <url>http://maven.apache.org</url>
  
  
 
    
  <dependencies>
             <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <version>*</version>
                <scope>provided</scope>
            </dependency>
  </dependencies>
  
  
  <build>
        
        <plugins>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.6</version>
          <configuration>
              <appendAssemblyId>false</appendAssemblyId>
              <outputDirectory>${project.build.directory}/releases/</outputDirectory>
              <descriptors>
                  <descriptor>${basedir}/src/main/assemblies/plugin.xml</descriptor>
              </descriptors>
          </configuration>
          <executions>
              <execution>
                  <phase>package</phase>
                  <goals>
                      <goal>single</goal>
                  </goals>
              </execution>
          </executions>
      </plugin>
      
      <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>
      
      
  </plugins>
        
       <resources>
         <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <excludes>
                <exclude>*.properties</exclude>
            </excludes>
        </resource>
      </resources>
    </build>
 </project>


只有一个java 类 MyNativeScriptPlugin

public class MyNativeScriptPlugin extends Plugin {
    
    private static final ESLogger logger = Loggers.getLogger(MyNativeScriptPlugin.class);
    
    @Override
    public String name() {
        return "my-native-scrip";//native-script为插件的名称
    }
 
    @Override
    public String description() {
        return "new score rule";
    }
 
    public void onModule(ScriptModule module) {
        module.registerScript("my_script", MyNativeScriptFactory.class);
    }
    
    
    
    public static class MyNativeScriptFactory implements NativeScriptFactory {
        @Override
        public ExecutableScript newScript(@Nullable Map<String, Object> params) {
            return new MyNativeScript();
        }
        @Override
        public boolean needsScores() {
            return true;
        }
    }

    public static class MyNativeScript extends AbstractDoubleSearchScript {
        @Override
        public double runAsDouble() {
            try {
               //你的逻辑
                
            } catch (IOException e) {
                logger.error("",e);
            }
            
            return 0;
        }

    }
 
}


执行打包命令

mvn clean install


执行安装命令

/*/bin/plugin install file:///root/score-plugins-1.0.0-SNAPSHOT.zip


需要所有机器都安装 否则报错 需要重启ES


执行查询命令

"script_score" : {
            "script" : { 
               "lang": "native",
               "inline":"my_script"  
             }
        }


推荐阅读:
  1. Elasticsearch 搜索打分计算原理浅析
  2. elasticsearch 打分精度

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

elasticsearch 打分插件 st

上一篇:mysql基础(六)mysql事务

下一篇:CentOS 7 忘记root密码修改大法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》