溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

elasticsearch 打分插件

發(fā)布時(shí)間:2020-07-14 19:01:52 來(lái)源:網(wǎng)絡(luò) 閱讀:1537 作者:大海之中 欄目:開發(fā)技術(shù)

插件常用命令

./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>


只有一個(gè)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;
        }

    }
 
}


執(zhí)行打包命令

mvn clean install


執(zhí)行安裝命令

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


需要所有機(jī)器都安裝 否則報(bào)錯(cuò) 需要重啟ES


執(zhí)行查詢命令

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


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI