码迷,mamicode.com
首页 > 其他好文 > 详细

elasticsearch 打分插件

时间:2017-06-29 22:22:22      阅读:266      评论:0      收藏:0      [点我收藏+]

标签: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 打分插件

标签:elasticsearch   打分插件   

原文地址:http://12597095.blog.51cto.com/12587095/1943254

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!