Elasticsearch自定排序插件实现
本文將介紹以插件的形式實現Elasticsearch自定義排序。
整個插件項目的結構為
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | project --src ----main --------assemblies ------------plugin.xml --------java ------------com.dcharm.plugin ----------------NativeScriptPlugin.java ----------------JyhBaseScriptFactory.java --------resources ------------es-plugin.properties ----test --pom.xml |
下面會介紹各個文件的內容
1.pom.xml
pom.xml添加對Elasticsearch并且設置打包的方式,打包的方式引用了plugin.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 | ???<dependencies> ???????<dependency> ???????????<groupId>org.elasticsearch</groupId> ???????????<artifactId>elasticsearch</artifactId> ???????????<version>${es.version}</version> ???????????<scope>compile</scope> ???????</dependency> ??</dependencies> <build> ???????<plugins> ???????????<plugin> ???????????????<artifactId>maven-assembly-plugin</artifactId> ???????????????<version>2.3</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> ???????</plugins> ???</build> |
2.plugin.xml
plugin.xml指定了插件打包的方式
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?xmlversion="1.0"?> <assembly> ????<id>plugin</id> ????<formats> ????????<format>zip</format> ????</formats> ????<includeBaseDirectory>false</includeBaseDirectory> ????<dependencySets> ????????<dependencySet> ????????????<outputDirectory>/</outputDirectory> ????????????<useProjectArtifact>true</useProjectArtifact> ????????????<useTransitiveFiltering>true</useTransitiveFiltering> ????????????<excludes> ????????????????<exclude>org.elasticsearch:elasticsearch</exclude> ????????????</excludes> ????????</dependencySet> ????</dependencySets> </assembly> |
3.NativeScriptPlugin類
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | publicclass NativeScriptPlugin?extendsAbstractPlugin { ????@Override ????publicString name() { ????????return"native-script";//native-script為插件的名稱 ????} ? ????@Override ????publicString description() { ????????return"native scripts"; ????} ? ????publicvoid onModule(ScriptModule module) { ????????//m2c_jyh_default就是排序算法的名稱 ????????module.registerScript("m2c_jyh_default", JyhBaseScriptFactory.class); ????} ? } |
4.JyhBaseScriptFactory類
| 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 | publicclass JyhBaseScriptFactory?implementsNativeScriptFactory { ????@Override ????publicExecutableScript newScript(@NullableMap<String, Object> params) { ????????returnnew JyhScript(params); ????} ? ????protectedclass JyhScript?extendsAbstractDoubleSearchScript { ????????privatedouble price; ????????privateString category; ? ????????//params就是搜索請求中傳入的參數 ????????publicJyhScript(@NullableMap<String,Object> params){ ????????????this.price = (Double)params.get("price"); ????????????this.category = (String)params.get("category"); ????????} ? ????????@Override ????????publicdouble runAsDouble() { ????????????//iismerchant對應著doc values中iismerchant字段的值 ????????????longiismerchant = ((ScriptDocValues.Longs)doc().get("iismerchant")).getValue(); ????????????doublescore =?30; ????????????if(iismerchant ==?1) { ????????????????score +=?20; ????????????} ????????????else{ ????????????????score +=?this.price; ????????????} ????????????score = score <?0??0: score; ????????????returnscore; ????????} ????} } |
5.es-plugin.properties
在src/main/resources下新增es-plugin.properties文件
| 1 | plugin=com.dcharm.NativeScriptPlugin |
6.打包
插件打包的方式和一般的maven項目相同,使用下面的命令
| 1 | mvn clean?install |
打包后,插件為target/release目錄下的project.zip文件
7.安裝
| 1 | bin/elasticsearch-plugin--url?file:///path-to-target/project.zip --installnative-script |
其中native-script為插件的名稱
?
bin/elasticsearch-plugin--urlfile:///path-to-target/project.zip --installnative-script
安裝的時候一定要注意這個路徑的寫法:file:/// 我在這掉坑里了。
?
8.調用排序插件
調用自定義排序時,需要指定實現方式為native,排序名稱為m2c_jyh_default
| 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 | curl -XPOST localhost:9200/m2c/item/_search-d '{ ??"fields": ["id","sproduct"], ??"from": 0, ??"size": 1, ??"query": { ????"function_score": { ??????"query": { ????????"match": {"sproduct":"nike"} ??????}, ??????"functions": [ ????????{ ??????????"script_score": { ????????????"lang":"native", ????????????"script":"m2c_jyh_default", ??????????????"params": { ??????????????"price": 10.0, ??????????????"category":"12 34" ????????????} ??????????} ????????} ??????], ??????"boost_mode":"replace" ????} ??} }' |
上面的請求訪問的是我自己測試用的索引m2c,大家在嘗試的時候可以自己建立其他的索引。
總結
以上是生活随笔為你收集整理的Elasticsearch自定排序插件实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于Elasticsearch实现搜索推
- 下一篇: 使用Elasticsearch实现推荐系