在Elasticsearch上查询最接近日期时间的文档的最佳方法是什么? [英] What is the best way to query the document closest to a date-time on elasticsearch?

查看:473
本文介绍了在Elasticsearch上查询最接近日期时间的文档的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检索具有与请求最接近的地理位置和日期时间的文档,因此我不是在寻找日期时间的匹配项,而是在寻找最接近的日期和时间.我使用自定义脚本解决了该问题,但是我猜想可能会有更好的方法,类似于我根据位置和距离过滤地理位置的方法.

I need to retrieve the document that has the closest geo location and date-time to the request, so I'm not looking for a match of the date-time, but the closest one. I solved it using a custom script, however I'm guessing there might be a better way to do it, similar to the way I'm filtering the geo location based on a location and a distance.

这是我的代码(在python中):

Here's my code (in python):

query = {
        "query": {
            "function_score": {
                "boost_mode": "replace",
                "query": {
                    "filtered": {
                        "query" : {
                            "match_all" : {}
                        },
                        "filter" : {
                            "geo_distance" : {
                                "distance" : "10km",
                                "location" : json.loads(self.request.body)["location"]
                            }
                        }
                    }
                },
                "script_score": {
                    "lang": "groovy",
                    "script_file": "calculate-score",
                    "params": {
                        "stamp": json.loads(self.request.body)["stamp"]
                    }
                }
            }
        },
        "sort": [
                    {"_score": "asc"}
        ],
        "size": 1
    }

    response = requests.get('http://localhost:9200/meteo/meteo/_search', data=json.dumps(query))

自定义的calculate-score.groovy脚本包含以下内容:

The custom calculate-score.groovy script contains the following:

abs(new java.text.SimpleDateFormat("yyyy-MM-dd\'T\'HH:mm").parse(stamp).getTime() - doc["stamp"].date.getMillis()) / 60000

脚本将分数返回为文档日期时间与请求日期时间之间的绝对差(分钟).

The script returns the score as the absolute difference in minutes between the document date-time and the requested date-time.

还有其他方法可以实现这一目标吗?

Is there any other way to achieve this?

推荐答案

您应该可以使用

You should be able to use function_score to do this. You could use the decay functions mentioned in the doucmentation to give a larger score to documents closer to the origin timestamp. Below is the example where the scale=28800 mins i.e 20d.

示例:

put test
put test/test/_mapping
{
    "properties": {
          "stamp": {
                  "type": "date",
                  "format": "dateOptionalTime"
               }
    }
}
put test/test/1
{
    "stamp":"2015-10-15T00:00"
}

put test/test/2
{
    "stamp":"2015-10-15T12:00"
}


post test/_search
{
   "query": {
      "function_score": {
         "functions": [
            {
               "linear": {
                   "stamp" : {
                        "origin": "now",
                        "scale": "28800m"
                   }
               }
            }
         ],
         "score_mode" : "multiply",
         "boost_mode": "multiply",
         "query": {
            "match_all": {}
         }
      }
   }
}

这篇关于在Elasticsearch上查询最接近日期时间的文档的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆