Elasticsearch多个groupBy和Count

时间:2016-08-25 09:59:11

标签: elasticsearch aggregation

我有以下记录

 {
      "id": "ebcd43a5-51f7-441c-9a5c-fcdff079344f",
      "sId": "",
      "attempted": 7,
      "correct": 6,
      "submittedAt": "2016-08-18T15:33:03+0530",
      "responses": [
        {
          "qId": "Q1",
          "aIdx": 1
        },
        {
          "qId": "Q2",
          "aIdx": 2
        }
      ]
    },
 {
      "id": "ebcd43a5-51f7-441c-9a5c-fcdff079344f",
      "sId": "",
      "attempted": 7,
      "correct": 6,
      "submittedAt": "2016-08-18T15:33:03+0530",
      "responses": [
        {
          "qId": "Q1",
          "aIdx": 2
        },
        {
          "qId": "Q2",
          "aIdx": 1
        }
      ]
    },
 {
      "id": "ebcd43a5-51f7-441c-9a5c-fcdff079344f",
      "sId": "",
      "attempted": 7,
      "correct": 6,
      "submittedAt": "2016-08-18T15:33:03+0530",
      "responses": [
        {
          "qId": "Q1",
          "aIdx": 1
        },
        {
          "qId": "Q2",
          "aIdx": 1
        }
      ]
    }

现在我想获得以下结果, 我想知道Q1的aIdx = 1和aIdx = 2被调用了多少次,同样对于Q2,例如在这种情况下我希望得到以下结果:

Q1 --> A1(aIdx=1) = 2
|----> A2(aIdx=2) = 1

Q2 --> A1(aIdx=1) = 2
|----> A2(aIdx=2) = 1

现在我正在尝试以下查询:

{
"size": 0,
"aggregations": {
"qId": {
  "terms": {
    "field": "responses.qId",
    "size": 0
  },
  "aggs": {
    "aIdx": {
      "terms": {
        "field": "responses.aIdx",
        "size": 0
      },
      "aggs": {
        "count_ans": {
          "value_count": {
            "field": "responses.aIdx"
          }
        }
      }
    }
  }
 }
}
}

但是这并没有给我想要的结果,我认为它给出了所有记录中aIdx的总数,但我想要为每个问题计算aIdx。

感谢任何帮助。

0 个答案:

没有答案