查找最近的时间戳

时间:2019-10-26 02:57:24

标签: elasticsearch

我正在使用Elasticsearch 6.4.2,并且需要考虑指定的时间戳来查找上一个和下一个文档。

Kinda就像我在SQL表上执行SELECT TOP 1 * from table WHERE date < 2019-01-01 ORDER BY date DESCSELECT TOP 1 * from table WHERE date > 2019-01-01 ORDER BY date ASC一样,要查找2019-01-01的上一条记录和下一条记录,知道吗?

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

数据:

[
      {
        "_index" : "index25",
        "_type" : "_doc",
        "_id" : "mceIBm4B1qXGA4PnKzvZ",
        "_score" : 1.0,
        "_source" : {
          "id" : 1,
          "date" : "2019-10-01"
        }
      },
      {
        "_index" : "index25",
        "_type" : "_doc",
        "_id" : "mseIBm4B1qXGA4PnRDvs",
        "_score" : 1.0,
        "_source" : {
          "id" : 2,
          "date" : "2019-10-02"
        }
      },
      {
        "_index" : "index25",
        "_type" : "_doc",
        "_id" : "m8eIBm4B1qXGA4PncDv9",
        "_score" : 1.0,
        "_source" : {
          "id" : 3,
          "date" : "2019-10-03"
        }
      },
      {
        "_index" : "index25",
        "_type" : "_doc",
        "_id" : "nMeIBm4B1qXGA4Pnhjvs",
        "_score" : 1.0,
        "_source" : {
          "id" : 4,
          "date" : "2019-10-04"
        }
      },
      {
        "_index" : "index25",
        "_type" : "_doc",
        "_id" : "nceIBm4B1qXGA4Pnmjtm",
        "_score" : 1.0,
        "_source" : {
          "id" : 5,
          "date" : "2019-10-05"
        }
      }
    ]

查询:我正在使用两个过滤条件和术语聚合,以使第一个日期大于或小于2019-10-03

{
  "size": 0,
  "aggs": {
    "above": {
      "filter": {
        "range": {
          "date": {
            "gt": "2019-10-03"
          }
        }
      },
      "aggs": {
        "TopDocument": {
          "terms": {
            "field": "date",
            "size": 1,
            "order": {
              "_term": "asc"
            }
          },
          "aggs": {
            "documents": {
              "top_hits": {
                "size": 10
              }
            }
          }
        }
      }
    },
    "below":{
      "filter": {
        "range": {
          "date": {
            "lt": "2019-10-03"
          }
        }
      },
      "aggs": {
        "TopDocument": {
          "terms": {
            "field": "date",
            "size": 1,
            "order": {
              "_term": "desc"
            }
          },
          "aggs": {
            "documents": {
              "top_hits": {
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}

响应:

{
    "below" : {
      "doc_count" : 2,
      "TopDocument" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 1,
        "buckets" : [
          {
            "key" : 1569974400000,
            "key_as_string" : "2019-10-02T00:00:00.000Z",
            "doc_count" : 1,
            "documents" : {
              "hits" : {
                "total" : {
                  "value" : 1,
                  "relation" : "eq"
                },
                "max_score" : 1.0,
                "hits" : [
                  {
                    "_index" : "index25",
                    "_type" : "_doc",
                    "_id" : "mseIBm4B1qXGA4PnRDvs",
                    "_score" : 1.0,
                    "_source" : {
                      "id" : 2,
                      "date" : "2019-10-02"
                    }
                  }
                ]
              }
            }
          }
        ]
      }
    },
    "above" : {
      "doc_count" : 2,
      "TopDocument" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 1,
        "buckets" : [
          {
            "key" : 1570147200000,
            "key_as_string" : "2019-10-04T00:00:00.000Z",
            "doc_count" : 1,
            "documents" : {
              "hits" : {
                "total" : {
                  "value" : 1,
                  "relation" : "eq"
                },
                "max_score" : 1.0,
                "hits" : [
                  {
                    "_index" : "index25",
                    "_type" : "_doc",
                    "_id" : "nMeIBm4B1qXGA4Pnhjvs",
                    "_score" : 1.0,
                    "_source" : {
                      "id" : 4,
                      "date" : "2019-10-04"
                    }
                  }
                ]
              }
            }
          }
        ]
      }
    }
  }

答案 1 :(得分:0)

  

您可以尝试以下方法:

从表WHERE date <2019-01-01 ORDER BY date DESC 中选择TOP 1

   {
      "sort": [
        {
          "date": {
            "order": "desc"
          }
        }
      ],
      "query": {
        "bool": {
          "filter": [
            {
              "range": {
                "date": {
                  "lt": "2019-01-01"
                }
              }
            }
          ]
        }
      },
      "size": 1
    }

从表WHERE date> 2019-01-01 ORDER BY date ASC中选择前1 *

   {
      "sort": [
        {
          "date": {
            "order": "asc"
          }
        }
      ],
      "query": {
        "bool": {
          "filter": [
            {
              "range": {
                "date": {
                  "gt": "2019-01-01"
                }
              }
            }
          ]
        }
      },
      "size": 1
    }