需求:以索引中的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. [转]C#多线程和线程池

    鸣谢原文:http://www.cnblogs.com/wwj1992/p/5976096.html 1.概念  1.0 线程的和进程的关系以及优缺点 windows系统是一个多线程的操作系统.一个程 ...

  2. 常规RPC通讯过程【转载】

    在 HTTP2 协议正式开始工作前, 如果已经知道服务器是 HTTP2 的服务器, 通讯流程如下: 客户端必须首先发送一个连接序言,其逻辑结构: PRI * HTTP/2.0\r\n\r\nSM\r\ ...

  3. Stack Overflow大揭密:哪一种程序员工资最高?

    Stackoverflow在程序员之间可以說是无人不知无人不晓,甚至常有人开玩笑说:“如果stackoverflow倒闭了,全世界代码的产出率将下降一半以上”或许听起来有点夸张,但是不难想像这个网站在 ...

  4. iOS系统结构

    应用交互层.多媒体层.核心服务层.系统层. 参考官方文档apple Develop GuidesiOS Technologies IOS分为四级结构,由上到下为可触摸层,媒体层,核心服务层,核心系统层 ...

  5. 【AnjularJS系列6 】 过滤器

    第六篇,过滤器 AngularJS 过滤器可用于转换数据: 过滤器 描述 currency 格式化数字为货币格式. filter 从数组项中选择一个子集. lowercase 格式化字符串为小写. o ...

  6. dp入门(先摆在这里,之后细细读)

    网址转载链接:  http://bbs.chinaunix.net/thread-4094539-1-1.html 动态规划:从新手到专家 Hawstein翻译 前言 我们遇到的问题中,有很大一部分可 ...

  7. sqlhelper 数据库帮助操作类

    数据库帮助类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  8. CF1041F Ray in the tube构造_思维

    不难发现起点必定是一个点. 每次间隔的距离一定是 2k2^k2k,关键就是要判断两点是否在同一跳跃距离上可被同时覆盖. 我们可以对上边进行 x1≡x_{1}\equivx1​≡ x2mod(2∗dx) ...

  9. 浅谈自底向上的Shell脚本编程及效率优化

    作者:沐星晨 出处:http://blog.csdn.net/sosodream/article/details/6276758 浅谈自底向上的Shell脚本编程及效率优化 小论文,大家多批评指导:) ...

  10. 新手须知 QT类大全

    QT类大全,在行内容中罗列出来了,希望大家多看看,如果是API就更好了,但可惜不是.这些是一些大类,请多做参考. QApplication 应用程序类 QLabel 标签类 QPushButton 按 ...