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

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

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/
效果图如下:
elasticsearch-recip
实现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技术精华网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值