Elasticsearch - Grouping aggregation - 2 fields -


my mapping is

{ "myapp": {     "mappings": {         "attempts": {             "properties": {                 "answers": {                     "properties": {                         "question_id": {                             "type": "long"                         },                         "status": {                             "type": "long"                         }                     }                 },                 "exam_id": {                     "type": "long"                 }             }         }     } } } 

and want group question_id , status

i want know every question_id how many has status 1 or 2

p.s. 2 attempts can have same questions

first of all, need update mapping , make answers nested field. not making nested make answers lose correlation between question_id field , status field.

{ "myapp": {     "mappings": {         "attempts": {             "properties": {                 "answers": {                     "type":"nested", <-- update here                     "properties": {                         "question_id": {                             "type": "long"                         },                         "status": {                             "type": "long"                         }                     }                 },                 "exam_id": {                     "type": "long"                 }             }         }     } } } 

you can use status in sub-aggregation shown below

"aggs": {     "nested_qid_agg": {       "nested": {         "path": "answers"       },       "aggs": {         "qid": {           "terms": {             "field": "answers.question_id",             "size": 0           },           "aggs": {             "status": {               "terms": {                 "field": "answers.status",                 "size": 0               }             }           }         }       }     }   } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -