如何在elasticsearch中返回最新的不同行忽略时间戳字段

发布于 2025-01-10 04:11:21 字数 606 浏览 2 评论 0原文

我有这样的文档:

{'foo': 'foo', 'bar': 'bar', ..., 'timestamp': 3}
{'foo': 'diffval', 'bar': 'diffval', ..., 'timestamp': 2}
{'foo': 'foo', 'bar': 'bar', ..., 'timestamp': 2}
{'foo': 'foo', 'bar': 'bar', ..., 'timestamp': 1}

我通过 _search?from=0&size=20&sort=timestamp%3Adesc 进行搜索

我现在想只搜索最新的不同行 - 例如:

{'foo': 'foo', 'bar': 'bar', ..., 'timestamp': 3}
{'foo': 'diffval', 'bar': 'diffval', ..., 'timestamp': 2}

但我会喜欢在不明确指示 foobar 字段的情况下执行此操作,因为可能有很多字段并且不一致 - 但是 timestamp 字段是一致的。

I have documents like:

{'foo': 'foo', 'bar': 'bar', ..., 'timestamp': 3}
{'foo': 'diffval', 'bar': 'diffval', ..., 'timestamp': 2}
{'foo': 'foo', 'bar': 'bar', ..., 'timestamp': 2}
{'foo': 'foo', 'bar': 'bar', ..., 'timestamp': 1}

Which I search for via _search?from=0&size=20&sort=timestamp%3Adesc

I would like to now search for just the latest distinct row - e.g:

{'foo': 'foo', 'bar': 'bar', ..., 'timestamp': 3}
{'foo': 'diffval', 'bar': 'diffval', ..., 'timestamp': 2}

But I would like to do this without explicitly indicating the foo, bar, fields as there could be a lot and are not consistently there - the timestamp field however is consistent.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独享拥抱 2025-01-17 04:11:21

我找到了一个解决方案,在存储到文档中之前,我为除时间戳之外的所有字段创建一个 hash 字段。然后我使用 opensearch 中的折叠功能 - 然后命中将返回最新的不同哈希值。

GET /.../_search?sort=timestamp%3Adesc
{
  "collapse": {
    "field": "hash",
    "inner_hits": {
      "name": "",
      "size": 0,
      "sort": [
        {
          "timestamp": {
            "order": "desc"
          }
        }
      ]
    }
  }
}

I have found a sollution where I create a hash field of all the fields apart from the timestamp before storing in the document. Then I use the collapse functionality in opensearch - the hits will then return the latest distinct hash.

GET /.../_search?sort=timestamp%3Adesc
{
  "collapse": {
    "field": "hash",
    "inner_hits": {
      "name": "",
      "size": 0,
      "sort": [
        {
          "timestamp": {
            "order": "desc"
          }
        }
      ]
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文