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實戰技巧】「外掛使用專題」Maven-Assembly外掛實現自定義打包Maven
- gradle自定義外掛Gradle
- mybatis 自定義外掛MyBatis
- vue自定義全域性元件(或自定義外掛)Vue元件
- 自定義Gradle-Plugin 外掛GradlePlugin
- apisix~自定義外掛的部署API
- es 自定義分詞外掛分詞
- 藍燈+chrome外掛實現按需翻牆(自定義代理白名單)Chrome
- JMeter 擴充套件外掛實現對自定義協議的支援JMeter套件協議
- 使用Guava的ComparisonChain實現自定義的排序GuavaAI排序
- php短視訊原始碼,jQuery實現自定義輪播圖外掛PHP原始碼jQuery
- Cordova學習--iOS自定義外掛iOS
- 【django學習-24】自定義外掛Django
- gradle自定義任務和外掛Gradle
- Kube-Scheduler外掛的自定義
- APISIX Ingress 如何支援自定義外掛API
- Gradle理論與實踐四:自定義Gradle外掛Gradle
- 淺析MyBatis(三):聊一聊MyBatis的實用外掛與自定義外掛MyBatis
- 快速自定義Cordova外掛(-配置檔案)
- apisix-dashboard上新增自定義外掛API
- 二 阿里大模型接入:自定義外掛阿里大模型
- mybaits原始碼分析--自定義外掛(七)AI原始碼
- [-Flutter外掛篇 1-] 從自定義外掛開始說起Flutter
- [外掛擴充套件]onethink自定義欄位外掛 百度地圖定位 外掛套件地圖
- elasticsearch之ik分詞器和自定義詞庫實現Elasticsearch分詞
- svelte元件:Svelte3自定義Navbar+Tabbr元件|svelte自定義外掛元件
- iOS持續整合(三)——fastlane 自定義外掛iOSAST
- Higress 基於自定義外掛訪問 RedisRedis
- Java整合系列:高效構建自定義外掛Java
- Apache Maven Assembly自定義打包外掛的使用ApacheMaven
- JMeter自定義取樣器外掛開發JMeter
- [外掛擴充套件]自定義表單外掛【2015-02-28更新】套件
- 【Maven實戰技巧】「外掛使用專題」Maven-Archetype外掛建立自定義maven專案骨架Maven
- 使用Mybatis自定義外掛實現不侵入業務的公共引數自動追加MyBatis
- MySQL自定義排序MySql排序
- Python自定義排序Python排序
- PAT乙級——1092(陣列排序 自定義sort)Java實現陣列排序Java
- Elasticsearch外掛安裝Elasticsearch
- ##自定義一個自動注入Log 的 外掛