使用ElasticSearch查询基于时间戳获取最新数据

hpcdzsge  于 2023-08-03  发布在  ElasticSearch
关注(0)|答案(1)|浏览(257)

我是Elastic搜索的新手,需要ES查询的帮助。我有一个ElasticSearch索引,记录如下(排序的基础上时间戳)
| 存储ID| BookStoreName|机架ID|机架名称|BookID|图书名称| BookName |
| --|--|--|--|--|--| ------------ |
| 122r3| ABC| 122r34| XYZ| 122e33r| ABC| abc |
| 122r3| ABCD| 122r35| XYZA| 1298e33r| Hb78| hb78 |
| 122r3| ABCE| 122r34| XYZ1| 9086795s| 8hb7| 8hb7 |
| 122r3| ABCF| 122r34| XYZ2| 132lkg h| Kho97| kho97 |
在此文档中,对于单个StoreID,可以有多个RackID,并且在此组合中将有大量可用数据(BookID和BookName)。查找ES查询以查找StoreID和RackID组合的最新记录。例如,这里的StoreID(122 r3)和RackID(122 r34)有3条记录,需要获取低于1的最新记录。
| 122r3| ABC| 122r34| XYZ| 122e33r| ABC| abc |
我正在使用Elastic search 7 &尝试Bool Must query,它返回所有记录。只需要基于时间戳的最新记录。
还可以获得多个组合的结果,以便使用单个查询可以获取所有StoreID和RackID组合的最新记录。

rta7y2nd

rta7y2nd1#

有两种方法可以实现最新记录。

  1. size + sort(按时间戳)
  2. top_hits聚合

以下是一个示例:

创建索引

POST _bulk
{"index": {"_index": "test_sort_by_timestamp"}}
{"TimeStamp": "2023-07-31T09:33:41Z", "StoreID": "122r3", "BookStoreName": "ABC", "RackID": "122r34", "RackName": "XYZ", "BookID": "122e33r", "BookName":     "abc"}
{"index": {"_index": "test_sort_by_timestamp"}}
{"TimeStamp": "2023-07-31T09:32:41Z", "StoreID": "122r3", "BookStoreName": "ABCD", "RackID": "122r35", "RackName": "XYZA", "BookID": "1298e33r", "BookName":    "hb78"}
{"index": {"_index": "test_sort_by_timestamp"}}
{"TimeStamp": "2023-07-31T09:31:41Z", "StoreID": "122r3", "BookStoreName": "ABCE", "RackID": "122r34", "RackName": "XYZ1", "BookID": "9086795s", "BookName":    "8hb7"}
{"index": {"_index": "test_sort_by_timestamp"}}
{"TimeStamp": "2023-07-31T09:30:41Z", "StoreID": "122r3", "BookStoreName": "ABCF", "RackID": "122r34", "RackName": "XYZ2", "BookID": "132lkgh", "BookName":     "kho97"}

字符串

size + sort按时间戳

GET test_sort_by_timestamp/_search
{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "StoreID.keyword": "122r3"
          }
        },
        {
          "term": {
            "RackID.keyword": "122r34"
          }
        }
      ]
    }
  },
  "aggs": {
    "latest_record": {
      "top_hits": {
        "size": 1,
        "sort": [
          {
            "TimeStamp": {
              "order": "desc"
            }
          }
        ]
      }
    }
  }
}


top_hits聚合

GET test_sort_by_timestamp/_search
{
  "size": 1,
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "StoreID.keyword": "122r3"
          }
        },
        {
          "term": {
            "RackID.keyword": "122r34"
          }
        }
      ]
    }
  },
  "sort": [
    {
      "TimeStamp": {
        "order": "desc"
      }
    }
  ]
}

编辑:

GET test_sort_by_timestamp/_search
{
  "size": 1,
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "RackID.keyword": ["122r34", "122r35"]
          }
        },
        {
          "term": {
            "StoreID.keyword": "122r3"
          }
        }
      ]
    }
  },
  "sort": [
    {
      "TimeStamp": {
        "order": "desc"
      }
    }
  ]
}


的数据
1.编辑

GET test_sort_by_timestamp/_search
{
  "size": 0,
  "aggs": {
    "store_rack_combinations": {
      "terms": {
        "script": {
          "source": "doc['StoreID.keyword'].value + '|' + doc['RackID.keyword'].value"
        },
        "size": 10
      },
      "aggs": {
        "latest_record": {
          "top_hits": {
            "size": 1,
            "sort": [
              {
                "TimeStamp": {
                  "order": "desc"
                }
              }
            ]
          }
        }
      }
    }
  }
}

相关问题