2016-08-19 7 views
1

私は弾性検索には新しく、助けを求めています。 基本的に私は私の弾性検索でいくつかの200万書類を持っており、文書は以下のようになります。弾性検索:特定の分野の集計合計

{ 
    "_index": "flipkart", 
    "_type": "PSAD_ThirdParty", 
    "_id": "430001_MAM_2016-02-04", 
    "_version": 1, 
    "_score": 1, 
    "_source": { 
    "metrics": [ 
     { 
     "id": "Metric1", 
     "value": 70 
     }, 
     { 
     "id": "Metric2", 
     "value": 90 
     }, 
     { 
     "id": "Metric3", 
     "value": 120 
     } 
    ], 
    "primary": true, 
    "ticketId": 1, 
    "pliId": 206, 
    "bookedNumbers": 15000, 
    "ut": 1454567400000, 
    "startDate": 1451629800000, 
    "endDate": 1464589800000, 
    "tz": "EST" 
    } 
} 

私は集計クエリ条件以下を満たす書きたい:"_index"に基づいて

1)まず、クエリを、 "_type"および"pliId"。 2)metrics.id = "Metric1"に基づいてmetrics.valueで集計を行います。

基本的には、いくつかのフィールドに基づいてレコードを照会し、メトリックIDに基づいて特定のメトリクス値に合計を集計する必要があります。 私の質問を正しく手伝ってください。

+0

私は、SQL内でグループに似ていますが、間違った結果、以下のようなものを試してみました:{ "クエリ":{ は、 "フィルタ":{ "フィルタ":{ 「BOOL ":{ "必須":[ {" という用語は、 "{" pliId」:206}} //無視して他の検索基準 ] }}} }、 "aggs":{ "by_metrics":{ "という用語":{ "フィールド": "metrics.id" }、 "aggs":{ "total_delivery":{ "和":{ "フィールド":「メトリクス.VALUE」 }} }} }} – Renukaradhya

+0

は 'metrics'フィールドは' nested'型であるかどうか? –

答えて

1

あなたmetricsフィールドがタイプnestedであることが必要である:あなたがidニーズ上記参照として

"metrics": { 
     "type": "nested", 
     "properties": { 
     "id": { 
      "type": "string", 
      "index": "not_analyzed" 
     } 
     } 
    } 

あなたは大文字を意味し、Metric1が一致する場合は、not_analyzedされるように。あなただけmetrics.id = "Metric1"集計をしたい場合は

はその後、あなたはこのようなものが必要:

{ 
    "query": { 
    "filtered": { 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "term": { 
       "pliId": 206 
       } 
      } 
      ] 
     } 
     } 
    } 
    }, 
    "aggs": { 
    "by_metrics": { 
     "nested": { 
     "path": "metrics" 
     }, 
     "aggs": { 
     "metric1_only": { 
      "filter": { 
      "bool": { 
       "must": [ 
       { 
        "term": { 
        "metrics.id": { 
         "value": "Metric1" 
        } 
        } 
       } 
       ] 
      } 
      }, 
      "aggs": { 
      "by_metric_id": { 
       "terms": { 
       "field": "metrics.id" 
       }, 
       "aggs": { 
       "total_delivery": { 
        "sum": { 
        "field": "metrics.value" 
        } 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 
+0

こんにちはAndrei、ここでは、 "metrics"フィールドはObjectタイプであり、 "Nested"フィールドではありません。それをオブジェクトとして定義する私の側からのバグ。それを指摘してくれてありがとう。可能であれば、それを "ネスト"と定義するために今修正することはできますか? Elastic Searchに既に存在するデータはどうなりますか? – Renukaradhya

+0

このタイプのマッピングは、オンザフライでは変更できません。索引を再作成して文書の索引を再作成する必要があります。 –

+0

私はインデックスを再作成し、クエリを実行し、それは働いた。助けてくれてありがとう。 – Renukaradhya

0

作成した新しいインデックス: 方法:PUT、 URL:http://localhost:9200/google/

ボディ:

{ 
     "mappings": { 
     "PSAD_Primary": { 
      "properties": { 
      "metrics": { 
       "type": "nested", 
       "properties": { 
      "id": { 
       "type": "string", 
       "index": "not_analyzed" 
      }, 
      "value": { 
       "type": "integer", 
       "index": "not_analyzed" 
      } 
      } 
      } 
      } 
     } 
     } 
    } 

その後、私は約20万のドキュメントを挿入し、クエリを実行して、それは働いた。

応答:

{ 
    "took": 34, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 1, 
    "max_score": 1, 
    "hits": [ 
     { 
     "_index": "google", 
     "_type": "PSAD_Primary", 
     "_id": "383701291_MAM_2016-01-06", 
     "_score": 1, 
     "_source": { 
      "metrics": [ 
      { 
       "id": "Metric1", 
       "value": 70 
      }, 
      { 
       "id": "Metric2", 
       "value": 90 
      }, 
      { 
       "id": "Metric3", 
       "value": 120 
      } 
      ], 
      "primary": true, 
      "ticketId": 1, 
      "pliId": 221244, 
      "bookedNumbers": 15000, 
      "ut": 1452061800000, 
      "startDate": 1451629800000, 
      "endDate": 1464589800000, 
      "tz": "EST" 
     } 
     } 
    ] 
    }, 
    "aggregations": { 
    "by_metrics": { 
     "doc_count": 3, 
     "metric1_only": { 
     "doc_count": 1, 
     "by_metric_id": { 
      "doc_count_error_upper_bound": 0, 
      "sum_other_doc_count": 0, 
      "buckets": [ 
      { 
       "key": "Metric1", 
       "doc_count": 1, 
       "total_delivery": { 
       "value": 70 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 
関連する問題