日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Elasticsearch自定排序插件实现

發(fā)布時間:2024/1/17 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Elasticsearch自定排序插件实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文將介紹以插件的形式實(shí)現(xiàn)Elasticsearch自定義排序。

整個插件項(xiàng)目的結(jié)構(gòu)為

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

下面會介紹各個文件的內(nèi)容
1.pom.xml
pom.xml添加對Elasticsearch并且設(shè)置打包的方式,打包的方式引用了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就是搜索請求中傳入的參數(shù)

????????publicJyhScript(@NullableMap<String,Object> params){

????????????this.price = (Double)params.get("price");

????????????this.category = (String)params.get("category");

????????}

?

????????@Override

????????publicdouble runAsDouble() {

????????????//iismerchant對應(yīng)著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項(xiàng)目相同,使用下面的命令

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.調(diào)用排序插件
調(diào)用自定義排序時,需要指定實(shí)現(xiàn)方式為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,大家在嘗試的時候可以自己建立其他的索引。

總結(jié)

以上是生活随笔為你收集整理的Elasticsearch自定排序插件实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。