Elasticsearch實現自定義排序外掛(轉載)
轉自:http://www.cnblogs.com/xffy1028/p/6383676.html
外掛入口:
package ttd.ugc.plugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.script.ScriptModule;
/**
* Created by jin_h on 2017/1/9.
*/
public class NativeScriptPlugin extends Plugin {
@Override
public String name() {
return “comment-default-sort”;
}
@Override
public String description() {
return “comment-default-sort”;
}
public void onModule(ScriptModule module) {
//comment-default-sort排序演算法的名稱
module.registerScript(“comment-default-sort”, CommentDefaultSortScriptFactory.class);
}
}
外掛具體實現:
package ttd.ugc.plugin;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.script.AbstractDoubleSearchScript;
import org.elasticsearch.script.AbstractLongSearchScript;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.NativeScriptFactory;
import org.elasticsearch.search.lookup.LeafDocLookup;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
/**
* Created by jin_h on 2017/1/9.
*/
public class CommentDefaultSortScriptFactory implements NativeScriptFactory {
@Override
public ExecutableScript newScript(@Nullable Map map) {
return new CustomScript(map);
}
@Override
public boolean needsScores() {
return false;
}
protected class CustomScript extends AbstractDoubleSearchScript {
//params 通過外部傳入的引數方式進行排序干預
public CustomScript(@Nullable Map params) {
}
@Override
public double runAsDouble() {
//三種獲取文件方式.
//((ScriptDocValues.Longs)doc().get(“wordnumber”)).getValue()
//(int)source().get(“wordnumber”);
//this.docFieldLongs(“wordnumber”);
double wordNumber;
double commentTime;
double useDate;
double numPicture;
double feedBack;
try {
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
String today = sdf.format(new Date());
if (source().get(“wordnumber”) == null) {
wordNumber = 0;
} else {
wordNumber = (int)source().get(“wordnumber”);
if (wordNumber >= 100) {
wordNumber = 1;
} else if (wordNumber >= 70) {
wordNumber = 0.9;
} else if (wordNumber >= 60) {
wordNumber = 0.8;
} else if (wordNumber >= 50) {
wordNumber = 0.7;
} else if (wordNumber >= 40) {
wordNumber = 0.6;
} else if (wordNumber >= 30) {
wordNumber = 0.5;
} else if (wordNumber >= 20) {
wordNumber = 0.4;
} else if (wordNumber >= 10) {
wordNumber = 0.3;
} else if (wordNumber >= 5) {
wordNumber = 0.2;
} else if (wordNumber >= 1) {
wordNumber = 0.1;
} else {
wordNumber = 0;
}
}
if (source().get(“imgcount”) == null) {
numPicture = 0;
} else {
numPicture = (int)source().get(“imgcount”);
if (numPicture >= 10) {
numPicture = 1;
} else if (numPicture >= 9) {
numPicture = 0.9;
} else if (numPicture >= 8) {
numPicture = 0.8;
} else if (numPicture >= 7) {
numPicture = 0.7;
} else if (numPicture >= 6) {
numPicture = 0.6;
} else if (numPicture >= 5) {
numPicture = 0.5;
} else if (numPicture >= 4) {
numPicture = 0.4;
} else if (numPicture >= 3) {
numPicture = 0.3;
} else if (numPicture >= 2) {
numPicture = 0.2;
} else if (numPicture >= 1) {
numPicture = 0.1;
} else {
numPicture = 0;
}
}
if (source().get(“useful”) == null) {
feedBack = 0;
} else {
feedBack = (int)source().get(“useful”);
if (feedBack >= 10) {
feedBack = 1;
} else if (feedBack >= 9) {
feedBack = 0.9;
} else if (feedBack >= 8) {
feedBack = 0.8;
} else if (feedBack >= 7) {
feedBack = 0.7;
} else if (feedBack >= 6) {
feedBack = 0.6;
} else if (feedBack >= 5) {
feedBack = 0.5;
} else if (feedBack >= 4) {
feedBack = 0.4;
} else if (feedBack >= 3) {
feedBack = 0.3;
} else if (feedBack >= 2) {
feedBack = 0.2;
} else if (feedBack >= 1) {
feedBack = 0.1;
} else {
feedBack = 0;
}
}
commentTime =source().get(“cmtdate”)==null?-1:(sdf.parse(today).getTime() – sdf.parse(source().get(“cmtdate”).toString()).getTime())/(24*60*60*1000);
if (commentTime >= 620) {
commentTime = 0.1;
} else if (commentTime >= 360) {
commentTime = 0.2;
} else if (commentTime >= 180) {
commentTime = 0.3;
} else if (commentTime >= 120) {
commentTime = 0.4;
} else if (commentTime >= 90) {
commentTime = 0.5;
} else if (commentTime >= 60) {
commentTime = 0.6;
} else if (commentTime >= 30) {
commentTime = 0.7;
} else if (commentTime >= 14) {
commentTime = 0.8;
} else if (commentTime >= 7) {
commentTime = 0.9;
} else if (commentTime >= 0) {
commentTime = 1;
} else {
commentTime = 0;
}
useDate =source().get(“usedate”)==null?-1: (sdf.parse(today).getTime() – sdf.parse(source().get(“usedate”).toString()).getTime())/(24*60*60*1000);
if (useDate >= 620) {
useDate = 0.1;
} else if (useDate >= 360) {
useDate = 0.2;
} else if (useDate >= 180) {
useDate = 0.3;
} else if (useDate >= 120) {
useDate = 0.4;
} else if (useDate >= 90) {
useDate = 0.5;
} else if (useDate >= 60) {
useDate = 0.6;
} else if (useDate >= 30) {
useDate = 0.7;
} else if (useDate >= 14) {
useDate = 0.8;
} else if (useDate >= 7) {
useDate = 0.9;
} else if (useDate >= 0) {
useDate = 1;
} else {
useDate = 0;
}
double iw_wordNumber = 0.3;
double iw2_commentTime = 0.2;
double iw3_useDate = 0.2;
double iw4_numPicture = 0.15;
double iw5_feedBack = 0.15;
double sumW = iw_wordNumber + iw2_commentTime + iw3_useDate + iw4_numPicture + iw5_feedBack;
double sumScore = wordNumber * iw_wordNumber + commentTime * iw2_commentTime + useDate * iw3_useDate + numPicture * iw4_numPicture + feedBack * iw5_feedBack;
return (sumScore / sumW);
}catch (Exception ex){
ex.printStackTrace();
return -1;//this.docFieldLongs(“wordnumber”).getValue();
}
}
}
}
相關文章
- 使用Maven的assembly外掛實現自定義打包Maven
- mybatis 自定義外掛MyBatis
- 【Maven實戰技巧】「外掛使用專題」Maven-Assembly外掛實現自定義打包Maven
- gradle自定義外掛Gradle
- vue自定義全域性元件(或自定義外掛)Vue元件
- WM自定義桌面"今日外掛"
- es 自定義分詞外掛分詞
- apisix~自定義外掛的部署API
- 藍燈+chrome外掛實現按需翻牆(自定義代理白名單)Chrome
- JMeter 擴充套件外掛實現對自定義協議的支援JMeter套件協議
- 自定義Gradle-Plugin 外掛GradlePlugin
- Kube-Scheduler外掛的自定義
- jQuery如何製作自定義外掛jQuery
- Cordova學習--iOS自定義外掛iOS
- Gradle自定義外掛詳解Gradle
- gulp進階-自定義gulp外掛
- 【django學習-24】自定義外掛Django
- php短視訊原始碼,jQuery實現自定義輪播圖外掛PHP原始碼jQuery
- 淺析MyBatis(三):聊一聊MyBatis的實用外掛與自定義外掛MyBatis
- 使用Guava的ComparisonChain實現自定義的排序GuavaAI排序
- js實現的陣列自定義排序介紹JS陣列排序
- gradle自定義任務和外掛Gradle
- APISIX Ingress 如何支援自定義外掛API
- 自定義jquery外掛簡單介紹jQuery
- jQuery自定義外掛簡單介紹jQuery
- 快速自定義Cordova外掛(-配置檔案)
- 二 阿里大模型接入:自定義外掛阿里大模型
- apisix-dashboard上新增自定義外掛API
- [-Flutter外掛篇 1-] 從自定義外掛開始說起Flutter
- 使用Mybatis自定義外掛實現不侵入業務的公共引數自動追加MyBatis
- elasticsearch之ik分詞器和自定義詞庫實現Elasticsearch分詞
- Gradle理論與實踐四:自定義Gradle外掛Gradle
- iOS持續整合(三)——fastlane 自定義外掛iOSAST
- JMeter自定義取樣器外掛開發JMeter
- mybaits原始碼分析--自定義外掛(七)AI原始碼
- Apache Maven Assembly自定義打包外掛的使用ApacheMaven
- Java整合系列:高效構建自定義外掛Java
- svelte元件:Svelte3自定義Navbar+Tabbr元件|svelte自定義外掛元件