首页 > 代码库 > 搜索实现最新的文章排序在前

搜索实现最新的文章排序在前

新闻搜索的时候,一般需要把最近的新闻排序在前,以突出时效性。

solr实现最新的文章排序在前

在用solr进行解决该问题的方法,很简单,solr已经提供相关函数进行了实现。
recip(rord(creationDate),1,1000,1000)。

关于recip函数定义
A reciprocal function with recip(x,m,a,b) implementing a/(m*x+b). m,a,b are constants, x is any numeric field or arbitrarily complex function.

When a and b are equal, and x>=0, this function has a maximum value of 1 that drops as x increases. Increasing the value of a and b together results in a movement of the entire function to a flatter part of the curve. These properties can make this an ideal function for boosting more recent documents when x is rord(datefield).

关于rord函数定义
The reverse ordering of what ord provides.

例如:
rord(myDateField) is a metric for how old a document is: the youngest document will return 1, the oldest document will return the total number of documents.

详情参看:http://wiki.apache.org/solr/FunctionQuery

elasticsearch实现最新的文章排序在前

elasticsearch现在版本没有提供recip函数和rord函数,不能直接实现。但是我们可以依据 y = a / (m * x + b)函数来实现最近文章排序在前。各取值如下:m=3.16E-11, a=0.08, and b=0.05.

参数为:(0.08 / ((3.16*10^-11) * |x| + 0.05)) + 1.0 from 0 to 1000*60*60*24*365/
效果图如下:
技术分享
实现json如下:

{  "query": {    "custom_filters_score": {      "query": { ...the main query... },      "params": {        "now": ...current time when query is run, expressed as milliseconds since the epoch...      },      "filters": [        {          "filter": {            "exists": {              "field": "date"            }          },          "script": "(0.08 / ((3.16*pow(10,-11)) * abs(now - doc[‘date‘].date.getMillis()) + 0.05)) + 1.0"        }      ]    }  }}

java代码如下:

QueryStringQueryBuilder queryBuilder = new QueryStringQueryBuilder("中国");        queryBuilder.analyzer("ik").field("title");return new CustomFiltersScoreQueryBuilder(queryBuilder).add(query,    			"(0.08 / ((3.16*pow(10,-11)) * abs(now - doc[‘updatetime‘].value) + 0.05)) + 1.0"				).param("now", System.currentTimeMillis()/1000l);

备注:我们updatetime存储的是一个毫秒级的长整数,具体情况具体分析。

以上方法来自于:http://jontai.me/blog/2013/01/advanced-scoring-in-elasticsearch/

本文固定链接: http://www.chepoo.com/search-realization-latest-articles-sorted-first.html | IT技术精华网

搜索实现最新的文章排序在前