需求:以索引中的boostapp列作为评分的基础分值,同时根据carpublishtime(数据的刷新时间字段)按时间进行衰减。

基于Groovy脚本实现。

1、query脚本方式:

{
"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)*5 else _source.boostapp"
}
}
],
"boost_mode": "replace",
"score_mode": "sum"
}
},
"from": 0,
"size": 10
}

  

2、Nest方式:

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)*5 else _source.boostapp";
funcQuery.ScriptScore = scriptFilter;
mainQuery &= funcQuery;

  

Elasticsearch的Groovy Script自定义评分检索的更多相关文章

  1. How to use groovy script on jenkins

    1. Install groovy plugin 2. Add a step of groovy. (normal & systerm) 3. Execute groovy script im ...

  2. SoapUI Pro Project Solution Collection –Easy develop Groovy Script to improve SoapUI ability

    As you know the groovy script and java script language is the soapui supported .but unfortunately So ...

  3. Common tasks that you can perform with the Groovy Script test step

    https://support.smartbear.com/readyapi/docs/soapui/steps/groovy.html Get test case object To obtain ...

  4. Access Java API in Groovy Script

    $ cat Hello.java package test; public class Hello { public int myadd(int x, int y) { return 10 * x + ...

  5. 基于Elasticsearch的自定义评分算法扩展

    实现思路: 重写评分方法,调整计算文档得分的过程,然后根据function_score或script_sort进行排序检索.   实现步骤: 1.新建java项目TestProject,引入Elast ...

  6. [fiddler] 使用fiddler script自定义代理规则

    场景 fiddler作为调试代理工具,可以捕获电脑与互联网之间所有http通讯. 通过可视化操作或命令行可以按某些规则截获特定请求并修改,但当我们需要批量对请求进行更复杂的逻辑操作时,则不是很方便. ...

  7. 自定义评分器Similarity,提高搜索体验(转)

    文章转自:http://blog.csdn.net/duck_genuine/article/details/6257540 首先说一下lucene对文档的评分规则: score(q,d)   =   ...

  8. IOS-一步一步教你自定义评分星级条RatingBar ——转载的

    由于项目的需要,需要设计能评分.能显示评分数据的星级评分条,但是IOS上好像没有这个控件,Android是有RatingBar这个控件的(又发现一个IOS不如Android好的),那就只能自定义了,在 ...

  9. lucene 自定义评分

    摘自:http://blog.csdn.net/seven_zhao/article/details/42708953 1.基于FunctionQuery,(1)创建类并继承ValueSource:( ...

随机推荐

  1. Struts2简单环境搭建

    一.开篇 Struts2是一个运行于web容器的表示层框架,其核心作用是帮助我们处理Http请求.Struts2处理Http请求(Request),并进行内部处理,再进行Http返回. 下载strut ...

  2. Creative Cloud 无法连接问题

    防火墙允许 PDApp.exe Windows:Program Files\Common Files\Adobe\OOBE\PDApp\core Mac OS:应用程序 > 实用工具 > ...

  3. 使用layer.tips实现鼠标悬浮时触发事件提示消息实现

    代码: <body> <label id="test" onmouseover="show('test')"> 你瞅啥!?过来试试! & ...

  4. Java压缩图片

    阅读目录 前言 压缩的要求 实现 优点 其他功能 前言 作为靠谱的java服务端程序员,图片这个事情一直是个头疼的事情. 现在很多网站上,都有上传图片这个功能,而图片对于现在的很多手机来说,拍摄出来的 ...

  5. node——模块分类,require执行顺序,require注意事项,原理

    node.js模块 在node.js开发中一个文件就可以认为是一个模块. 一.node.js模块分类 核心模块Code Module.内置模块.原生模块 fs http path url ... 所有 ...

  6. sublime text3安装js提示的插件

    今天安装Sublime Text3的js插件,在网上查了很多资料,为了方便以后看,写一个安装插件的总结和方法. 要安装js相关的插件,就要先安装一个Package Control(插件管理器)的插件, ...

  7. 8、Collaborative Metric Learning

    一.摘要: 文章的核心思想:是如何把Metric learning 和 CF结合起来从而达到更好的推荐效果. 提出了CML(Collaborative Metric Learning),其学习一个联合 ...

  8. 使用 Jersey 和 Apache Tomcat 构建 RESTful Web 服务

    作者: Yi Ming Huang, 软件工程师, IBM Dong Fei Wu, 软件工程师, IBM Qing Guo, 软件工程师, IBM 出处: http://www.ibm.com/de ...

  9. UVA10082-WERTYU(紫书例题3.2)

    A common typing error is to place the hands on the keyboard one row to the right of the correct posi ...

  10. php的更新

    因为 PHP 那“集百家之长”的蛋疼语法,加上社区氛围不好,很多人对新版本,新特征并无兴趣.本文将会介绍自 PHP5.2 起,直至 PHP5.6 中增加的新特征 本文目录:PHP5.2 以前:auto ...