Elasticsearch 自定义聚合结果

Elasticsearch custom Aggregation Result

在我开发的培训模块中,有一个页面将所有课程列在一个 table 视图中,可过滤 header。其中一列是 "Saved" 列,如果我将其添加为书签,则该列为 "Saved",否则为 "Unsaved"。

课程实体索引如下:

{
  "_index": "myindex",
  "_type": "course",
  "_id": "248fc0a2-06e1-11e6-b740-000c298fdb4d",
  "_score": 1,
  "_source": {
  "boost_number": 1,
  "entity_type_number": 64,
  "course_status_string": "Closed",
  "title": "My Test Course",
  "body": "This is just a test course",
  "created_date": "20160420T101753Z",
  "categories_uuid": [ ],
  "segment_string": [
    "Other"
  ],
  "vertical_string": [
    "Other"
  ],
  "saved_users_uuid": [
    "251bde26-4adf-11e4-b705-000c298fdb4d",
    "00026884-7cc8-11e3-a570-fa163e2bcb7a",
    "00061164-9283-11e5-a394-000c298fdb4d",
    "00110a72-0b8b-11e4-9a64-fa163e2bcb7a",
  ]
}

假设我使用 ID 登录:“251bde26-4adf-11e4-b705-000c298fdb4d”。 在查询弹性方面时,我收到了这个桶:

"saved_users" : {
    "doc_count_error_upper_bound": 0, 
    "sum_other_doc_count": 0, 
    "buckets" : [ 
        {
            "key" : "251bde26-4adf-11e4-b705-000c298fdb4d",
            "doc_count" : 5
        },
        {
            "key" : "00026884-7cc8-11e3-a570-fa163e2bcb7a",
            "doc_count" : 0
        },
        ...
    ]
}

但是,我想要另一个自定义存储桶,例如:

{
  ...
  "buckets": [
    {
      "key": "Saved",
      "doc_count": ...
    },
    {
      "key": "Unsaved",
      "doc_count": ...
    },
  ]
}

这可能吗?我该怎么做?

您可以尝试添加以下内容filters aggregation:

{
  "size": 0,
  "aggs": {
    "category": {
      "filters": {
        "filters": {
          "Saved": {
            "exists": {
              "field": "saved_users_uuid"
            }
          },
          "Unsaved": {
            "missing": {
              "field": "saved_users_uuid"
            }
          }
        }
      }
    }
  }
}