ElasticSearch使用嵌套日期范围进行高级评分,今天>明天>昨天

时间:2014-10-29 14:44:14

标签: elasticsearch nested scoring

这是ES中的数据模型:

{
  id: 1,
  dateRanges: [
    { from: 2014-01-01, to: 2014-01-05 },
    { from: 2014-01-08, to: 2014-01-10 },
    ...
  ]
}

现在我想根据当前日期对我的文档进行评分。伪代码:

SCORE = 
  if( today IS IN any dateRange ) {
    return MAX_VALUE;
  } else if( today < any dateRange.from ) {
    dateRange.from = lowest dateRange.from, which is > today;
    return 1 / ( dateRange.from - today );
  } else {
    dateRange.to = highest dateRange.to, which is < today;
    return dateRange.to - today;
  }

实施例

id | dateRange1               | dateRange2               | ...
-------------------------------------------------------------------
1  | 2014-01-01 to 2014-01-10 | 2014-01-12 to 2014-01-13 | 
2  | 2014-01-03 to 2014-01-03 |                          |
3  | 2014-01-01 to 2014-01-02 | 2014-01-10 to 2014-01-15 |

today = 2014-01-11

结果应按此顺序排列:3 1 2

原因:

  • 3得分最高,因为today至少在一个日期范围内
  • 1获得第二高的得分,因为它至少有一个日期范围dateRange.from > today
  • 2得分最低,因为其所有日期范围均为dateRange.to < today

有谁知道怎么做?

感谢您的帮助:)

0 个答案:

没有答案