Requirement: The Boostapp column in the index is used as the base score for the rating, and is attenuated by time based on the Carpublishtime (Data Refresh Time field).
Implemented based on groovy scripting.
1. Query Script mode:
{"Fields ": [ "Boost", "Ucarid", "Boostapp", "Carpublishtime" ], "query": { " Function_score ": {" query ": { " Match_all ": {} }, " Functions ": [ { " Script_score ": { "Script": "Import java.util.date;import java.text.dateformat;import java.text.simpledateformat;time2= (New Date (). GetTime ()-New SimpleDateFormat (' Yyyy-mm-dd HH:mm:ss '). Parse (_source.carpublishtime.replace (' T ', ')). GetTime ())/ 60000), if (time2<=60) _SOURCE.BOOSTAPP+TIME2/5 else if (time2<=120) _source.boostapp+ (time2/10-6) *8 else if ( time2<=180) _source.boostapp+ (time2/10-12), _source.boostapp " } } ], " Boost_mode ":" Replace ", " Score_mode ":" Sum " } }, " from ": 0, " size ": 10}
2, Nest Way:
Querycontainer mainquery = null; Functionscorequery funcquery = new Functionscorequery (); funcquery.scoremode = Functionscoremode.sum; Funcquery.boostmode = Functionboostmode.replace;funcquery.maxboost = 1000.0f; Scriptfilter scriptfilter = new Scriptfilter (); scriptfilter.script = "Import Java.util.date;import Java.text.dateformat;import java.text.simpledateformat;time2= ((New Date (). GetTime ()-New SimpleDateFormat (' Yyyy-mm-dd HH:mm:ss '). Parse (_source.carpublishtime.replace (' T ', ')). GetTime ())/60000); if (time2<=60) _ SOURCE.BOOSTAPP+TIME2/5 else if (time2<=120) _source.boostapp+ (time2/10-6) *8 else if (time2<=180) _ source.boostapp+ (time2/10-12) "_source.boostapp"; funcquery.scriptscore = Scriptfilter;mainquery &= Funcquery;
Elasticsearch's groovy script custom scoring search