Агрегирующий массив объектов в эластичном поиске - PullRequest
0 голосов
/ 13 декабря 2018

Я пытаюсь выяснить, что я делаю неправильно, я собрал следующее, "Подмножество данных" , "Текущий запрос" , "Текущий вывод" & "Требуемый вывод" , если требуется больше, пожалуйста, дайте мне знать.

Так выглядят мои объекты данных

[{
  "survey_answers": [
    {
      "id": "9ca01568e8dbb247", // As they are, this is the key to groupBy
      "option_answer": 5, // Represent the index of the choosen option
      "type": "OPINION_SCALE" // Opinion scales are 0-10 (meaning elleven options)
    },
    {
      "id": "ba37125ec32b2a99",
      "option_answer": 3,
      "type": "LABELED_QUESTIONS" // Labeled questions are 0-x (they can change it from survey to survey)
    }
  ],
  "survey_id": "test"
},
{
  "survey_answers": [
    {
      "id": "9ca01568e8dbb247",
      "option_answer": 0,
      "type": "OPINION_SCALE"
    },
    {
      "id": "ba37125ec32b2a99",
      "option_answer": 3,
      "type": "LABELED_QUESTIONS"
    }
  ],
  "survey_id": "test"
}]

Так выглядит мой текущий объект запроса / агг

{
  "query": {
    "match": {
      "survey_id": "test"
    }
  },
  "aggs": {
    "agg_survey_answers": {
      "terms": {
        "field": "survey_answers.id.keyword"
      },
      "aggs": {
        "agg_option_answer": {
          "terms": {
            "field": "survey_answers.option_answer"
          }
        }
      }
    }
  }
}

Текущий вывод

{
  "agg_survey_answers": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 0,
    "buckets": [{
      "key": "9ca01568e8dbb247",
      "doc_count": 2,
      "agg_option_answer": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [{
          "key": 3,
          "doc_count": 2
        }, {
          "key": 0,
          "doc_count": 1
        }, {
          "key": 5,
          "doc_count": 1
        }]
      }
    }, {
      "key": "ba37125ec32b2a99",
      "doc_count": 2,
      "agg_option_answer": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [{
          "key": 3,
          "doc_count": 2
        }, {
          "key": 0,
          "doc_count": 1
        }, {
          "key": 5,
          "doc_count": 1
        }]
      }
    }]
  }
}

Требуемый вывод

{
  "agg_survey_answers": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 0,
    "buckets": [{
      "key": "9ca01568e8dbb247",
      "doc_count": 2,
      "agg_option_answer": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [{
          "key": 0,
          "doc_count": 1
        }, {
          "key": 5,
          "doc_count": 1
        }]
      }
    }, {
      "key": "ba37125ec32b2a99",
      "doc_count": 2,
      "agg_option_answer": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [{
          "key": 3,
          "doc_count": 2
        }]
      }
    }]
  }
}
...