2016-08-08 19 views
0

私のマッピングがElasticsearch - グループ化集合 - 2つの分野

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

であると私はすべてのために知ってほしいquestion_idとステータス

によってグループ化するには、多くはステータス1または2

を持っているかquestion_id

PS 2回の試行で同じ質問が出る可能性があります

答えて

1

まず、マッピングを更新してanswersnestedフィールドにする必要があります。それをnestedにしないと、question_idフィールドとstatusフィールドの間の相関が失われます。

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

"aggs": { 
    "nested_qid_agg": { 
     "nested": { 
     "path": "answers" 
     }, 
     "aggs": { 
     "qid": { 
      "terms": { 
      "field": "answers.question_id", 
      "size": 0 
      }, 
      "aggs": { 
      "status": { 
       "terms": { 
       "field": "answers.status", 
       "size": 0 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
以下のようにあなたは、サブ凝集の状態を使用することができます
関連する問題