1. 程式人生 > >elasticsearch 打分插件

elasticsearch 打分插件

elasticsearch 打分插件

插件常用命令

./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.wuage.score.plugin.MyNativeScriptPlugin
java.version=1.8
elasticsearch.version=2.3.5
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.wuage.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>2.3.5</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


執行安裝命令

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


需要所有機器都安裝 否則報錯 需要重啟ES


執行查詢命令

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


elasticsearch 打分插件