elasticsearch - 无论如何,在聚合之前是否要对索引进行排序

标签 elasticsearch

我有以下索引模板

{
  "index_patterns": "notificationtiles*",
  "order": 1,
  "version": 1,
  "aliases": {
    "notificationtiles": {}
  },
  "settings": {
    "number_of_shards": 5,
    "analysis": {
      "normalizer": {
        "lowercase_normalizer": {
          "type": "custom",
          "char_filter": [],
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "mappings": {
    "dynamic": "false",
    "properties": {
      "id": {
        "type": "keyword",
        "normalizer": "lowercase_normalizer"
      },
      "influencerId": {
        "type": "keyword",
        "normalizer": "lowercase_normalizer"
      },
      "friendId": {
        "type": "keyword",
        "normalizer": "lowercase_normalizer"
      },
      "message": {
        "type": "keyword",
        "normalizer": "lowercase_normalizer"
      },
      "type": {
        "type": "keyword",
        "normalizer": "lowercase_normalizer"
      },
      "sponsorshipCharityId": {
        "type": "keyword",
        "normalizer": "lowercase_normalizer"
      },
      "createdTimestampEpochInMilliseconds": {
        "type": "date",
        "format": "epoch_millis",
        "index": false
      },
      "updatedTimestampEpochInMilliseconds": {
        "type": "date",
        "format": "epoch_millis",
        "index": false
      },
      "createdDate": {
        "type": "date"
      },
      "updatedDate": {
        "type": "date"
      }
    }
  }
}
与以下查询
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "influencerId": "52407710-f7be-49c1-bc15-6d52363144a6"
                    }
                },
                {
                    "match": {
                        "type": "friend_completed_sponsorship"
                    }
                }
            ]
        }
    },
    "size": 0,
    "aggs": {
        "friendId": {
            "terms": {
                "field": "friendId",
                "size": 2
            },
            "aggs": {
                "latest": {
                    "top_hits": {
                        "sort": [
                            {
                                "createdDate": {
                                    "order": "desc"
                                }
                            }
                        ],
                        "_source": {
                            "includes": [
                                "sponsorshipCharityId",
                                "message",
                                "createdDate"
                            ]
                        },
                        "size": 1
                    }
                }
            }
        }
    }
}

哪个返回
{
    "took": 72,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 12,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "friendId": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 7,
            "buckets": [
                {
                    "key": "cf750fd8-998f-4dcd-9c88-56b2b6d6fce9",
                    "doc_count": 3,
                    "latest": {
                        "hits": {
                            "total": {
                                "value": 3,
                                "relation": "eq"
                            },
                            "max_score": null,
                            "hits": [
                                {
                                    "_index": "notificationtiles-1",
                                    "_type": "_doc",
                                    "_id": "416a8e07-fd72-46d4-ade1-b9442ef46978",
                                    "_score": null,
                                    "_source": {
                                        "createdDate": "2020-06-24T17:35:17.816842Z",
                                        "sponsorshipCharityId": "336de13c-f522-4796-9218-f373ff0b4373",
                                        "message": "Contact Test 788826 Completed Sponsorship!"
                                    },
                                    "sort": [
                                        1593020117816
                                    ]
                                }
                            ]
                        }
                    }
                },
                {
                    "key": "93ab55c5-795f-44b0-900c-912e3e186da0",
                    "doc_count": 2,
                    "latest": {
                        "hits": {
                            "total": {
                                "value": 2,
                                "relation": "eq"
                            },
                            "max_score": null,
                            "hits": [
                                {
                                    "_index": "notificationtiles-1",
                                    "_type": "_doc",
                                    "_id": "66913b8f-94fe-49fd-9483-f332329b80dd",
                                    "_score": null,
                                    "_source": {
                                        "createdDate": "2020-06-24T17:57:17.816842Z",
                                        "sponsorshipCharityId": "dbad136c-5002-4470-b85d-e5ba1eff515b",
                                        "message": "Contact Test 788826 Completed Sponsorship!"
                                    },
                                    "sort": [
                                        1593021437816
                                    ]
                                }
                            ]
                        }
                    }
                }
            ]
        }
    }
}

但是,我希望结果包含最新文档(按createdDate desc排序),例如以下文档
            {
                "_index": "notificationtiles-1",
                "_type": "_doc",
                "_id": "68a2a0a8-27aa-4347-8751-d7afccfa797d",
                "_score": 1.0,
                "_source": {
                    "id": "68a2a0a8-27aa-4347-8751-d7afccfa797d",
                    "influencerId": "52407710-f7be-49c1-bc15-6d52363144a6",
                    "friendId": "af342805-1990-4794-9d67-3bb2dd1e36dc",
                    "message": "Contact Test 788826 Completed Sponsorship!",
                    "type": "friend_completed_sponsorship",
                    "sponsorshipCharityId": "b2db72e6-a70e-414a-bf8b-558e6314e7ec",
                    "createdDate": "2020-06-25T17:35:17.816842Z",
                    "updatedDate": "2020-06-25T17:35:17.816876Z",
                    "createdTimestampEpochInMilliseconds": 1593021437817,
                    "updatedTimestampEpochInMilliseconds": 1593021437817
                }
            }
我需要获取按friendId分组的2个最新文档,以及每个friendId的最新文档。按friendId与每个friendId的最新文档进行分组的部分工作正常。但是,在聚合发生之前,我无法按createdDate desc对索引进行排序。
本质上,在聚合发生之前,我想按createdDate desc对索引进行排序。我不想由createdDate生成父聚合,因为那样不会导致唯一的friendId。如何实现?

最佳答案

看来您需要设置条款汇总的order属性。默认情况下,它们按命中数排序。您希望它们按最大createdDate排序。因此,您应该添加一个子聚合来计算最大的createdDate,然后可以使用该聚合的ID来对父项聚合进行排序。

关于elasticsearch - 无论如何,在聚合之前是否要对索引进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62598468/

相关文章:

elasticsearch - 在 Elasticsearch 中执行聚合查询时出错。 “illegal_argument_exception/默认情况下,在文本字段上禁用了字段数据

elasticsearch - 无法在Elasticsearch上创建映射

django - 序列化错误重建 Elasticsearch Django 应用程序

node.js - 如何在响应中获取更新的文档

spring-boot - 千分尺无法将指标保存到 Elasticsearch ,因为最终映射将有超过 1 种类型 : [_doc, doc]

amazon-web-services - Elasticsearch 与Dynamodb进行过滤

elasticsearch - 如何检查 Elasticsearch 脚本中的真实条件?

java - ES删除重复项

c# - Elasticsearch Nest Client - 搜索嵌套属性

ruby-on-rails - 使用 gem Tire 和 elasticsearch 进行高级搜索