http://www.w3cschool.tech/s?btnG=&w=elastic+%E6%8E%92%E5%BA%8F
参考: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);
}
}
- 具体实现
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<String, Object> map) {
return new CustomScript(map);
}
@Override
public boolean needsScores() {
return false;
}
protected class CustomScript extends AbstractDoubleSearchScript {
//params 通过外部传入的参数方式进行排序干预
public CustomScript(@Nullable Map<String,Object> 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();
}
}
}
}
网友评论