elasticsearch 根据另一个索引对索引结果进行排序

eit6fx6z  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(155)

我有一些相关字段的数据;这是一个与我的索引相关的例子

用户索引

[
   {
      "id": "user_1",
      "name": "xxx",
   },
   {
      "id": "user_2",
      "name": "xxx",
   },
   {
      "id": "user_3",
      "name": "xxx",
   },
   {
      "id": "user_4",
      "name": "xxx",
   }
]

职位索引

[
   {
      "id": "post_1",
      "title": "xxx",
      "user_id": "user_1",
      "created": "10-2-2022"
   },
   {
      "id": "post_2",
      "title": "xxx",
      "user_id": "user_3",
      "created": "11-2-2022"
   },
   {
      "id": "post_3",
      "title": "xxx",
      "user_id": "user_1",
      "created": "9-2-2022"
   }
]

我想根据帖子created的日期对users进行排序。

我要的desc结果
[
   {
      "id": "user_3"   # Given that they have to most recent post `post_2`
   },
   {
      "id": "user_1"
   },
   {
      "id": "user_2"
   },
   {
      "id": "user_4"
   }
]

注:user_2 & user_4有0个帖子,但被添加在结果的末尾。

如何最佳地实现这一点?

c7rzv4ha

c7rzv4ha1#

这在ElasticSearch中是不可能的。
ElasticSearch没有连接的概念。您需要将数据反规范化。将所有数据放在一起。您可以浏览parent/childnested fields

相关问题