Elasticsearch's groovy script custom scoring search

Source: Internet
Author: User
Tags dateformat groovy script

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

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.