Elasticsearch Implementing a custom sort plug-in

Source: Internet
Author: User

Plug-in entry:

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 name of the sorting algorithm
Module.registerscript ("Comment-default-sort", Commentdefaultsortscriptfactory.class);
}
}


Plug-in specific implementations:


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<string, object> Map) {
return new Customscript (map);
}

@Override
public Boolean needsscores () {
return false;
}

Protected class Customscript extends Abstractdoublesearchscript {

The params is sequenced by an externally-passed parameter method.
Public Customscript (@Nullable map<string,object> params) {

}

@Override
Public double runasdouble () {
Three ways to get documents.
((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_nu Mpicture + feedBack * iw5_feedback;
return (SUMSCORE/SUMW);
}catch (Exception ex) {
Ex.printstacktrace ();
Return-1;//this.docfieldlongs ("Wordnumber"). GetValue ();
}
}
}
}

Elasticsearch Implementing a custom sort plug-in

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.