有时,在我的结果集中,我的项目具有相同的_score,这没关系,但是我想拉起start_date和end_date在同一_score中位于“现在”之间的文档。
基本上我想做到这一点,但是在ElasticSearch中:
SELECT * FROM tbl WHERE ... ORDER BY _score DESC, (start_date <= NOW() AND NOW() <= end_date) DESC
我怎样才能做到这一点?
最佳答案
如何将(start_date <= NOW() AND NOW() <= end_date)
比较集成到查询分数计算本身中呢?
"query": {
"bool": {
"must": [
// your query here
],
"should": [
{
"bool": {
"must": [
{
"range": {
"start_date": {
"lte": "2015-03-03"
}
}
},
{
"range": {
"end_date": {
"gte": "2015-03-03"
}
}
}
]
}
}
]
}
}
或者,也可以使用脚本:
"sort": [
{
"_score": {
"order": "desc"
}
},
{
"_script": {
"type": "number",
"script": "def now_date=new DateTime(new Date()); if (doc['start_date'].date <= now_date && doc['end_date'].date >= now_date) return 1; return 0",
"order": "desc"
}
}
]
关于elasticsearch - 在ElasticSearch中使用附加逻辑对多列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36568013/