elasticsearch在列表字段中按属性聚合

wfveoks0  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(496)

我有一些文章,每篇文章可能有一些评论员,每个评论员可能有不同的批准状态,如下所示:

{
  ArticleId: 1,
  Title: "1",
  Reviewers: [
    {
       "Reviewer": "abc",
       "Status": "Pending"
    },
    {
       "Reviewer": "def",
       "Status": "Approved"
    }
  ]
}

{
  ArticleId: 2,
  Title: "2",
  Reviewers: [
    {
       "Reviewer": "abc",
       "Status": "Approved"
    },
    {
       "Reviewer": "def",
       "Status": "Approved"
    }
  ]
}

{
  ArticleId: 3,
  Title: "2",
  Reviewers: [
    {
       "Reviewer": "def",
       "Status": "Approved"
    }
  ]
}

我想将这些数据保存在es中,搜索所有以为审阅者的文章,并按的审阅状态进行聚合。

{
 ...
 buckets:[{
        "doc_count": "1",
        "Key": "Pending"
    },{
        "doc_count": "1",
        "Key": "Approved"
    }]
}

我在想怎么用es做?

rggaifut

rggaifut1#

假设:审阅者是嵌套类型。
尝试下面的查询。详细信息作为代码中的注解。

{
  "size": 0, 
  "aggs": {
    "NAME": {
      "nested": {              // <========== Use nested aggregation
        "path": "Reviewers"
      },
      "aggs": {
        "NAME": {
          "filter": {
            "term": {
              "Reviewers.Reviewer.keyword": "abc" // <========= Filter docs with reviewer = abc
            }
          },
          "aggs": {
            "NAME": {
              "terms": {
                "field": "Reviewers.Status.keyword", // <======= aggregate based on reviewer status
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}

相关问题