簡體   English   中英

elasticsearch:嵌套對象上的聚合

[英]elasticsearch: aggregations on nested objects

我有一些類似的文檔(樣本是_mapping中的嵌套對象):

"_source": {
   "samples": [
      {
         "name": "A",
         "d": 0.1
      },
      {
         "name": "B",
         "d": 0.2
      },
      ... A THOUSAND SAMPLES
      {
         "name": "N",
         "d": 197
      }
   ],
   "Date": "2015-10-01T01:06:59+0000",
   "fwot": "ENGINE1"
}

"_source": {
   "samples": [
      {
         "name": "A",
         "d": 0.2
      },
      {
         "name": "C",
         "d": 10032
      },
      ... A THOUSAND SAMPLES NOT NECESSARILY THE SAME FOR ALL THE DOCUMENTS
      {
         "name": "N",
         "d": 292
      }
   ],
   "Date": "2015-10-01T01:07:59+0000",
   "fwot": "ENGINE1"
}

我想對N進行匯總統計,以按樣本A的存儲桶制作直方圖

我首先對文檔進行過濾以獲取相關文檔

        {
          "nested": {
            "path": "samples", 
            "query": {
              "bool": {
                "should": [ 
                  { "match": { "samples.name": "A" }},
                  { "match": { "samples.name": "N"}}
                ]
          }}}
        }

但是現在,聚合部分可能是什么?

"aggs": {
    "samples": {
        "nested": {
            "path": "samples"
        },
        "aggs": {
            "n_stats_by_a": {
              "histogram": { 
              // ONLY ON SAMPLES WHERE name='A'
                "field":    "samples.d",
                "interval": 0.1 
              }
              //DO A METRICS ON samples.d ONLY ON SAMPLES WHERE name='N'
            }
        }
    }
}

我想要類似的東西作為輸出

"aggregations": {
  "samples": {
     "doc_count": 57716,
     "n_stats_by_a": {
        "doc_count": 177,
        "agg_histogram": {
           "buckets": [
              {
                 "key": "A_0.0_TO_0.1",
                 "avg_N": 182.332 //average of N when A between 0 and 0.1
              },
              {
                 "key": "A_0.1_TO_0.2",
                 "avg_N": 198.332 //average of N when A between 0.1 and 0.2
              }
              ...
           ]
        }
     }
  }

}

嘗試這個:

{
"size": 0,
"aggs": {
  "samples": {
     "nested": {
        "path": "samples"
     },
     "aggs": {
        "n_stats_by_a": {
           "filter": {
              "term": {
                 "samples.name": "A"
              }
           },
           "aggs": {
              "agg_histogram": {
                 "histogram": {
                    "field": "samples.d",
                    "interval": 0.1
                 },
           "aggs" : {
            "avg_value" : { "avg" : {"field" : "samples.d"} } 
                }
              }
           }
        },
        "n_stats_by_d": {
           "filter": {
              "term": {
                 "samples.name": "N"
              }
           },
           "aggs": {
              "agg_histogram": {
                 "histogram": {
                    "field": "samples.d",
                    "interval": 0.1
                 }
               }
             }
          }
        }
      }
    }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM