elasticsearch中一个键中所有嵌套对象的弹性堆栈聚合

ztigrdn8  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(280)

我在“提交”索引中有一个文件,看起来像这样,

{
  "took" : 18,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "submissions",
        "_type" : "_doc",
        "_id" : "90_169",
        "_score" : 1.0,
        "_source" : {
          "id" : "90_169",
          "locked" : false,
          "account_id" : 5,
          "campaign_id" : 90,
          "contact_id" : 1179,
          "submission_id" : 169,
          "answers" : [
            {
              "question_id" : 8451,
              "answer_bool" : true
            },
            {
              "question_id" : 8452,
              "answer_bool" : false
            },
            {
              "question_id" : 8453,
              "answer_bool" : true
            },
            {
              "question_id" : 8454,
              "answer_bool" : false
            }
          ]
        }
      }
    ]
  }
}

这只是一份文件。
我想对所有问题进行聚合,得到一个最终的bucket聚合,其中显示每个问题id的true和false的数量。
关于如何做到这一点有什么见解吗?

f0ofjuux

f0ofjuux1#

您需要使用嵌套聚合以及术语和筛选器聚合
添加索引Map、数据、搜索查询和搜索结果的工作示例
索引Map:

{
  "mappings": {
    "properties": {
      "answers": {
        "type": "nested"
      }
    }
  }
}

索引数据:

{
  "id": "90_169",
  "locked": false,
  "account_id": 5,
  "campaign_id": 90,
  "contact_id": 1179,
  "submission_id": 169,
  "answers": [
    {
      "question_id": 8451,
      "answer_bool": true
    },
    {
      "question_id": 8452,
      "answer_bool": false
    },
    {
      "question_id": 8453,
      "answer_bool": true
    },
    {
      "question_id": 8454,
      "answer_bool": false
    }
  ]
}
{
  "id": "90_169",
  "locked": false,
  "account_id": 5,
  "campaign_id": 90,
  "contact_id": 1179,
  "submission_id": 169,
  "answers": [
    {
      "question_id": 8451,
      "answer_bool": true
    },
    {
      "question_id": 8452,
      "answer_bool": false
    },
    {
      "question_id": 8453,
      "answer_bool": true
    },
    {
      "question_id": 8454,
      "answer_bool": true
    }
  ]
}
{
  "id": "90_169",
  "locked": false,
  "account_id": 5,
  "campaign_id": 90,
  "contact_id": 1179,
  "submission_id": 169,
  "answers": [
    {
      "question_id": 8451,
      "answer_bool": true
    },
    {
      "question_id": 8452,
      "answer_bool": false
    },
    {
      "question_id": 8453,
      "answer_bool": true
    },
    {
      "question_id": 8454,
      "answer_bool": true
    }
  ]
}

搜索查询:

{
  "size": 0,
  "aggs": {
    "nested_Agg": {
      "nested": {
        "path": "answers"
      },
      "aggs": {
        "id": {
          "terms": {
            "field": "answers.question_id"
          },
          "aggs": {
            "true_count": {
              "filter": {
                "term": {
                  "answers.answer_bool": "true"
                }
              }
            },
            "false_count": {
              "filter": {
                "term": {
                  "answers.answer_bool": "false"
                }
              }
            }
          }
        }
      }
    }
  }
}

搜索结果:

"aggregations": {
    "nested_Agg": {
      "doc_count": 12,
      "id": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": 8451,
            "doc_count": 3,
            "false_count": {
              "doc_count": 0
            },
            "true_count": {
              "doc_count": 3
            }
          },
          {
            "key": 8452,
            "doc_count": 3,
            "false_count": {
              "doc_count": 3
            },
            "true_count": {
              "doc_count": 0
            }
          },
          {
            "key": 8453,
            "doc_count": 3,
            "false_count": {
              "doc_count": 0
            },
            "true_count": {
              "doc_count": 3
            }
          },
          {
            "key": 8454,
            "doc_count": 3,
            "false_count": {
              "doc_count": 1
            },
            "true_count": {
              "doc_count": 2
            }
          }
        ]
      }
    }
  }

相关问题