【问题标题】:Is it possible to returns other fields when you aggregate results on Elasticsearch?在 Elasticsearch 上聚合结果时是否可以返回其他字段?
【发布时间】:2017-04-13 21:01:55
【问题描述】:

这是我的索引 PublicationsLikes 的映射:

  • id:字符串
  • 帐户:字符串
  • api:字符串
  • 日期:日期

我目前正在 ES 上进行聚合,在其中我按(出版物的)id 对结果计数进行分组。

{
    "key": "<publicationId-1>",
    "doc_count": 25
},
{
    "key": "<publicationId-2>",
    "doc_count": 387
},
{
    "key": "<publicationId-3>",
    "doc_count": 7831
}

返回的“key”(id)是一个信息,但我还需要选择发布的其他字段,如帐户和 api。有点像:

{
   "key": "<publicationId-1>",
   "api": "Facebook",
   "accountId": "65465z4fe6ezf456ezdf",
   "doc_count": 25
},
{
  "key": "<publicationId-2>",
  "api": "Twitter",
  "accountId": "afaez5f4eaz",
  "doc_count": 387
}

我该如何管理?

谢谢。

【问题讨论】:

    标签: elasticsearch aggregate elasticsearch-aggregation


    【解决方案1】:

    这个要求最好通过top_hits聚合来实现,你可以对每个桶中的文档进行排序并选择第一个,还可以控制要返回的字段:

    {
      "size": 0,
      "aggs": {
        "publications": {
          "terms": {
            "field": "id"
          },
          "aggs": {
            "sample": {
              "top_hits": {
                "size": 1,
                "_source": ["api","accountId"]
              }
            }
          }
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      您可以为此使用子聚合。

      GET /PublicationsLikes/_search
      {
       "aggs" : {
        "ids": {
         "terms": {
          "field": "id"
         },
         "aggs": {
          "accounts": {
           "terms": {
            "field": "account",
            "size": 1
           }
          }
         }
        }
       }
      }
      

      您的结果不会完全符合您的要求,但会有点相似:

      {
          "key": "<publicationId-1>",
          "doc_count": 25,
          "accounts": {
            "buckets": [
              {
                "key": "<account-1>",
                "doc_count": 25
              }
            ]
          }
      },
      {
          "key": "<publicationId-2>",
          "doc_count": 387,
          "accounts": {
            "buckets": [
              {
                "key": "<account-2>",
                "doc_count": 387
              }
            ]
          }
      },
      {
          "key": "<publicationId-3>",
          "doc_count": 7831,
          "accounts": {
            "buckets": [
              {
                "key": "<account-3>",
                "doc_count": 7831
              }
            ]
          }
      }
      

      您也可以查看the link了解更多信息

      【讨论】:

        【解决方案3】:

        感谢您的快速回复。我认为第一个解决方案是最“漂亮”的(就请求而言,但也用于检索结果),但两者似乎都是子聚合查询。

        { "size": 0, "aggs": { "publications": { "terms": { "size": 0, "field": "publicationId" }, "aggs": { "sample": { "top_hits": { "size": 1, "_source": ["accountId", "api"] } } } } } }

        我想我必须小心 size=0 参数,所以,因为我在 Java Api 中工作,所以我决定把 INT.Max 而不是 0。

        感谢很多人。

        【讨论】:

          猜你喜欢
          • 2016-08-14
          • 2014-09-08
          • 1970-01-01
          • 2017-10-29
          • 2018-06-13
          • 1970-01-01
          • 2015-01-15
          • 2017-11-04
          • 2016-01-22
          相关资源
          最近更新 更多