在字段中查找所有具有最大值的Documnents

发布于 2025-02-12 17:25:40 字数 1102 浏览 2 评论 0原文

我是elasticsearch的新手,这是我的第一个NOSQL DB,我有一些问题。

我有这样的东西:

index_logs: [
      {
          "entity_id": id_1, 
          "field_name": "name",
          "old_value": null
          "new_value": "some_name"
          "number": 1
      }, 
      {
          "entity_id": id_1, 
          "field_name": "description",
          "old_value": null
          "new_value": "some_descr"
          "number": 1
      }, 
      {
          "entity_id": id_1, 
          "field_name": "description",
          "old_value": "some_descr"
          "new_value": null
          "number": 2
      }, 
      {
          "entity_id": id_2, 
          "field_name": "enabled",
          "old_value": true
          "new_value":false
          "number": 25
      }, 
]

我需要找到所有我不知道的指定entity_id和max_number的文档。

postgres中,它将如下:

SELECT * 
FROM logs AS x
WHERE x.entity_id = <some_entity_id>
AND x.number = (SELECT MAX(y.number) FROM logs AS y WHERE y.entity_id = x.entity_id)

我该如何在elasticsearch中进行?

I am new in elasticsearch, it is my first NoSql DB and I have some problem.

I have something like this:

index_logs: [
      {
          "entity_id": id_1, 
          "field_name": "name",
          "old_value": null
          "new_value": "some_name"
          "number": 1
      }, 
      {
          "entity_id": id_1, 
          "field_name": "description",
          "old_value": null
          "new_value": "some_descr"
          "number": 1
      }, 
      {
          "entity_id": id_1, 
          "field_name": "description",
          "old_value": "some_descr"
          "new_value": null
          "number": 2
      }, 
      {
          "entity_id": id_2, 
          "field_name": "enabled",
          "old_value": true
          "new_value":false
          "number": 25
      }, 
]

And I need to find all documents with specified entity_id and max_number which I do not know.

In postgres it will be as following code:

SELECT * 
FROM logs AS x
WHERE x.entity_id = <some_entity_id>
AND x.number = (SELECT MAX(y.number) FROM logs AS y WHERE y.entity_id = x.entity_id)

How can I do it in elasticsearch?

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

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

发布评论

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

评论(1

誰認得朕 2025-02-19 17:25:40

使用以下查询:

{
  "query": {
    "term": {
      "entity_id": {
        "value": "1"
      }
    }
  },
  "aggs": {
    "max_number": {
      "terms": {
        "field": "number",
        "size": 1,
        "order": {
          "_key": "desc"
        }
      },
      "aggs": {
        "top_hits": {
          "top_hits": {
            "size": 10
          }
        }
      }
    }
  }
}

汇总将按“数字”进行分组,按数字降序排序,然后在“ top_hits'subgregation内部为您提供该数字的前10个结果。

获取“全部”文档的另一种方法是简单地使用相同的查询,而无需任何聚合,然后在“数字”字段上排序。在客户端,您使用“ search_after”的分页,并为所有结果分页,直到“数字”字段更改为止。第一次更改数字时,您将退出分页循环,并具有所有记录,其中包括给定的实体ID和最大号码。

Use the following query:

{
  "query": {
    "term": {
      "entity_id": {
        "value": "1"
      }
    }
  },
  "aggs": {
    "max_number": {
      "terms": {
        "field": "number",
        "size": 1,
        "order": {
          "_key": "desc"
        }
      },
      "aggs": {
        "top_hits": {
          "top_hits": {
            "size": 10
          }
        }
      }
    }
  }
}

The aggregation will group by the "number", sort by descending order of number, then give you the top 10 results with that number, inside the 'top_hits' subaggregation.

Another way to get "All" the documents is to simply use the same query, withouty any aggregations, and sort descending on the "number" field. On the client side you use pagination with "search_after", and paginate all the results, till the "number" field changes. The first time the number changes, you exit your pagination loop and you have all the records with the given entity id and the max number.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文