插件常用命令

./plugin list/remove/install


目录结构

wKioL1lU2w3jpUe6AABI-l6G8HA376.png-wh_50


plugin.xml 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<? 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 代码

1
2
3
4
5
6
7
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=2.3.5
jvm=true


pom.xml 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
< 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 >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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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 ;
         }
 
     }
  
}


执行打包命令

1
mvn clean install


执行安装命令

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


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


执行查询命令

1
2
3
4
5
6
"script_score" : {
             "script" : { 
                "lang": "native",
                "inline":"my_script"  
              }
         }