├── .github
└── workflows
│ └── mavenpublish.yml
├── .gitignore
├── README.md
├── pom.xml
└── src
├── main
├── assemblies
│ └── plugin.xml
├── java
│ └── red
│ │ └── shiwen
│ │ └── firestEsPlugin
│ │ ├── ExpertScriptPlugin.java
│ │ ├── MyFirstPlugin.java
│ │ ├── MyNativeScriptPlugin.java
│ │ └── fieldaddScriptPlugin.java
└── resources
│ └── plugin-descriptor.properties
└── test
└── java
└── red
└── shiwen
└── firestEsPlugin
└── AppTest.java
/.github/workflows/mavenpublish.yml:
--------------------------------------------------------------------------------
1 | # This workflow will build a package using Maven and then publish it to GitHub packages when a release is created
2 | # For more information see: https://github.com/actions/setup-java#apache-maven-with-a-settings-path
3 |
4 | name: Maven Package
5 |
6 | on:
7 | release:
8 | types: [created]
9 |
10 | jobs:
11 | build:
12 |
13 | runs-on: ubuntu-latest
14 |
15 | steps:
16 | - uses: actions/checkout@v2
17 | - name: Set up JDK 1.8
18 | uses: actions/setup-java@v1
19 | with:
20 | java-version: 1.8
21 | server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
22 | settings-path: ${{ github.workspace }} # location for the settings.xml file
23 |
24 | - name: Build with Maven
25 | run: mvn -B package --file pom.xml
26 |
27 | - name: Publish to GitHub Packages Apache Maven
28 | run: mvn deploy -s $GITHUB_WORKSPACE/settings.xml
29 | env:
30 | GITHUB_TOKEN: ${{ github.token }}
31 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 |
4 | ### STS ###
5 | .apt_generated
6 | .classpath
7 | .factorypath
8 | .project
9 | .settings
10 | .springBeans
11 |
12 | ### IntelliJ IDEA ###
13 | .idea
14 | *.iws
15 | *.iml
16 | *.ipr
17 |
18 | ### NetBeans ###
19 | nbproject/private/
20 | build/
21 | nbbuild/
22 | dist/
23 | nbdist/
24 | .nb-gradle/
25 |
26 | .mvn
27 | mvnw
28 | mvnw.cmd
29 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # elasticsearchpluginsample
2 |
3 | ### elasticsearch 插件demo
4 |
5 | 本例包含三个脚本插件
6 | * MyFirstPlugin
7 | 该插件仅用于了解基本plugin结构,es加载时只输出一段log
8 |
9 | * MyNativeScriptPlugin
10 | 该插件通过NativeScriptFactory实现简单的功能,计算给定字段(String类型)的字符长度并加上"add"参数的值作为_score
11 |
12 | * fieldaddScriptPlugin
13 | 遵循elasticsearch5.6.X版本的插件规范,实现指定数组字段相加并添加并与给定参数相加功能
14 |
15 | * 三个插件在编译时需要指定pom.xml文件中的配置项
16 | ```
17 | red.shiwen.firestEsPlugin.fieldaddScriptPlugin
18 | ```
19 |
20 | ### 部署方法
21 | ```
22 | mvn clean install
23 | cp target/releases/firestEsPlugin-5.6.4.zip elasticsearch-5.6.4/plugins
24 | cd elasticsearch-5.6.4/plugins
25 | unzip firestEsPlugin-5.6.4.zip
26 | mv elasticsearch firestEsPlugin
27 | rm -fr firestEsPlugin-5.6.4.zip
28 | ```
29 |
30 | ### 启动es后输出
31 | ```
32 | [2018-01-02T21:01:23,710][WARN ][r.s.f.MyFirstPlugin ] This is MyNativeScriptPlugin
33 | ```
34 |
35 | ### 插件测试方法
36 |
37 | * 导入测试数据
38 | ```
39 | PUT hockey/player/_bulk?refresh
40 | {"index":{"_id":1}}
41 | {"first":"johnny","last":"gaudreau","goals":[9,27,1],"assists":[17,46,0],"gp":[26,82,1],"born":"1993/08/13"}
42 | {"index":{"_id":2}}
43 | {"first":"sean","last":"monohan","goals":[7,54,26],"assists":[11,26,13],"gp":[26,82,82],"born":"1994/10/12"}
44 | {"index":{"_id":3}}
45 | {"first":"jiri","last":"hudler","goals":[5,34,36],"assists":[11,62,42],"gp":[24,80,79],"born":"1984/01/04"}
46 | {"index":{"_id":4}}
47 | {"first":"micheal","last":"frolik","goals":[4,6,15],"assists":[8,23,15],"gp":[26,82,82],"born":"1988/02/17"}
48 | {"index":{"_id":5}}
49 | {"first":"sam","last":"bennett","goals":[5,0,0],"assists":[8,1,0],"gp":[26,1,0],"born":"1996/06/20"}
50 | {"index":{"_id":6}}
51 | {"first":"dennis","last":"wideman","goals":[0,26,15],"assists":[11,30,24],"gp":[26,81,82],"born":"1983/03/20"}
52 | {"index":{"_id":7}}
53 | {"first":"david","last":"jones","goals":[7,19,5],"assists":[3,17,4],"gp":[26,45,34],"born":"1984/08/10"}
54 | {"index":{"_id":8}}
55 | {"first":"tj","last":"brodie","goals":[2,14,7],"assists":[8,42,30],"gp":[26,82,82],"born":"1990/06/07"}
56 | {"index":{"_id":39}}
57 | {"first":"mark","last":"giordano","goals":[6,30,15],"assists":[3,30,24],"gp":[26,60,63],"born":"1983/10/03"}
58 | {"index":{"_id":10}}
59 | {"first":"mikael","last":"backlund","goals":[3,15,13],"assists":[6,24,18],"gp":[26,82,82],"born":"1989/03/17"}
60 | {"index":{"_id":11}}
61 | {"first":"joe","last":"colborne","goals":[3,18,13],"assists":[6,20,24],"gp":[26,67,82],"born":"1990/01/30"}
62 | ```
63 |
64 | * MyNativeScriptPlugin 查询语句
65 | ```
66 | GET hockey/_search
67 | {
68 | "query": {
69 |
70 | "function_score": {
71 |
72 | "functions": [
73 | {
74 | "script_score": {
75 | "script": {
76 | "source": "my_script",
77 | "lang" : "native",
78 | "params": {
79 | "add": 3
80 | }
81 | }
82 | }
83 | }
84 | ]
85 | }
86 | }
87 | }
88 | ```
89 |
90 | 修改"params"中"add"的值,观察返回结果中"_score"的变化
91 |
92 |
93 | * fieldaddScriptPlugin 查询语句
94 | ```
95 | GET hockey/_search
96 | {
97 | "query": {
98 | "function_score": {
99 | "query": {
100 | "match": {
101 | "first": "sam"
102 | }
103 | },
104 | "functions": [
105 | {
106 | "script_score": {
107 | "script": {
108 | "source": "example_add",
109 | "lang": "expert_scripts",
110 | "params": {
111 | "fieldname": "goals",
112 | "inc": 2
113 | }
114 | }
115 | }
116 | }
117 | ]
118 | }
119 | }
120 | }
121 | ```
122 | 修改"params"中"inc"的值,观察返回结果中"_score"的变化
123 |
124 |
125 | #### 参考文档
126 | http://cwiki.apachecn.org/pages/viewpage.action?pageId=9405376
127 | https://programtalk.com/java-api-usage-examples/org.elasticsearch.script.ScriptEngineService/
128 |
129 |
130 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | red.shiwen
6 | firestEsPlugin
7 | ${elasticsearch.version}
8 | jar
9 |
10 | firestEsPlugin
11 | http://maven.apache.org
12 |
13 |
14 | UTF-8
15 |
16 | 5.6.4
17 | 1.8
18 | ${project.basedir}/src/main/assemblies/plugin.xml
19 | firestesplugin
20 | red.shiwen.firestEsPlugin.fieldaddScriptPlugin
21 | true
22 |
23 |
24 | 6.4.1
25 |
26 |
27 |
28 |
29 | org.elasticsearch
30 | elasticsearch
31 | ${elasticsearch.version}
32 | provided
33 |
34 |
35 |
36 | junit
37 | junit
38 | 3.8.1
39 | test
40 |
41 |
42 |
43 |
44 |
45 |
46 | src/main/resources
47 | false
48 |
49 | *.properties
50 |
51 |
52 |
53 |
54 |
55 | org.apache.maven.plugins
56 | maven-assembly-plugin
57 | 2.6
58 |
59 | false
60 | ${project.build.directory}/releases/
61 |
62 | ${basedir}/src/main/assemblies/plugin.xml
63 |
64 |
65 |
66 |
67 | package
68 |
69 | single
70 |
71 |
72 |
73 |
74 |
75 | org.apache.maven.plugins
76 | maven-compiler-plugin
77 | 3.5.1
78 |
79 | ${maven.compiler.target}
80 | ${maven.compiler.target}
81 |
82 |
83 |
84 |
85 |
86 |
--------------------------------------------------------------------------------
/src/main/assemblies/plugin.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | plugin
4 |
5 | zip
6 |
7 | false
8 |
9 |
10 | ${project.basedir}/src/main/resources/plugin-descriptor.properties
11 | elasticsearch
12 | true
13 |
14 |
15 |
16 |
17 | elasticsearch
18 | true
19 | true
20 |
21 |
22 |
--------------------------------------------------------------------------------
/src/main/java/red/shiwen/firestEsPlugin/ExpertScriptPlugin.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to Elasticsearch under one or more contributor
3 | * license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright
5 | * ownership. Elasticsearch licenses this file to you under
6 | * the Apache License, Version 2.0 (the "License"); you may
7 | * not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 |
20 | package red.shiwen.firestEsPlugin;
21 |
22 | import java.io.IOException;
23 | import java.io.UncheckedIOException;
24 | import java.util.Map;
25 | import java.util.function.Function;
26 |
27 | import org.apache.lucene.index.LeafReaderContext;
28 | import org.apache.lucene.index.PostingsEnum;
29 | import org.apache.lucene.index.Term;
30 | import org.elasticsearch.common.Nullable;
31 | import org.elasticsearch.common.settings.Settings;
32 | import org.elasticsearch.plugins.Plugin;
33 | import org.elasticsearch.plugins.ScriptPlugin;
34 | import org.elasticsearch.script.CompiledScript;
35 | import org.elasticsearch.script.ExecutableScript;
36 | import org.elasticsearch.script.LeafSearchScript;
37 | import org.elasticsearch.script.ScriptEngineService;
38 | import org.elasticsearch.script.SearchScript;
39 | import org.elasticsearch.search.lookup.SearchLookup;
40 |
41 | /**
42 | * An example script plugin that adds a {@link ScriptEngineService} implementing expert scoring.
43 | */
44 | public class ExpertScriptPlugin extends Plugin implements ScriptPlugin {
45 |
46 | @Override
47 | public ScriptEngineService getScriptEngineService(Settings settings) {
48 | return new MyExpertScriptEngine();
49 | }
50 |
51 | /** An example {@link ScriptEngineService} that uses Lucene segment details to implement pure document frequency scoring. */
52 | // tag::expert_engine
53 | private static class MyExpertScriptEngine implements ScriptEngineService {
54 | @Override
55 | public String getType() {
56 | return "expert_scripts";
57 | }
58 |
59 | @Override
60 | public Function