2016-08-08 24 views
0

Meine Mapping istElasticsearch - Gruppierung Aggregation - 2 Felder

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

und ich möchte Gruppe von question_id und Status

ich für jeden wissen wollen question_id wie viele Status 1 oder 2

PS 2 Versuche können die gleichen Fragen haben

Antwort

1

Zunächst müssen Sie Ihr Mapping aktualisieren und answers ein Feld nested machen. Nicht macht es nested wird die Antworten zu verlieren die Korrelation zwischen question_id Feld und status Feld.

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

Sie Status in einer Unter Aggregation unter

wie gezeigt
"aggs": { 
    "nested_qid_agg": { 
     "nested": { 
     "path": "answers" 
     }, 
     "aggs": { 
     "qid": { 
      "terms": { 
      "field": "answers.question_id", 
      "size": 0 
      }, 
      "aggs": { 
      "status": { 
       "terms": { 
       "field": "answers.status", 
       "size": 0 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
verwenden können