簡體   English   中英

Elasticsearch聚合查詢

[英]Elasticsearch aggregation query

我有一組存儲在elasticsearch中的文檔,它們看起來像這樣:

{
  "id": "12312312",
  "timestamp": "2015-11-01T00:00:00.000",
  "unit": {
    "id": "123456",
    "name": "unit-4"
  },
  "samples": [
    {
      "value": 244.05435180062133,
      "aggregation": "M",
      "type": {
        "name": "SomeName1",
        "display": "Some name 1"
      }
    },
    {
      "value": 251.19450064653438,
      "aggregation": "I",
      "type": {
        "name": "SomeName2",
        "display": "Some name 2"
      }
    },
    ...
  ]
}

我想針對它運行的聚合查詢該查詢返回的計數unit.id每桶財產samples.value ,查詢應根據samples.type.namesamples.aggregation 我產生了這樣的東西:

{
  "query": {
    "bool": {
      "must": [{
        "range": {
          "timestamp": {
            "gte": "2015-11-01T00:00:00.000",
            "lte": "2015-11-30T23:59:59.999",
            "format": "date_hour_minute_second_fraction"
          }
        }
      }, {
        "nested": {
          "path": "samples",
          "query": {
            "bool": {
              "must": [{
                "match": {
                  "samples.type.name": "SomeName1"
                }
              }]
            }
          }
        }
      }]
    }
  },
  "aggs": {
    "0": {
      "nested": {
        "path": "samples"
      },
      "aggs": {
        "1": {
          "histogram": {
            "field": "samples.value",
            "interval": 10
          }
        }
      }
    }
  }
}

我正在查詢http://localhost:9200/dc/sample/_search?search_type=count&pretty 但這會返回示例數組中嵌套文檔的數量。 但是我需要計算每個存儲桶的不同unit.id ...

你們能幫我嗎?

編輯:添加映射

{
  "dc" : {
    "mappings" : {
      "sample" : {
        "unit" : {
          "properties" : {
            "name" : {
              "type" : "string"
            }}},
        "samples" : {
          "type" : "nested",
          "properties" : {
            "aggregation" : {
              "type" : "string"
            },
            "type" : {
              "properties" : {
                "display" : {
                  "type" : "string"
                },
                "name" : {
                  "type" : "string"
                }
              }
            },
            "value" : {
              "type" : "double"
            }
          }
        },
        "timestamp" : {
          "type" : "date",
          "format" : "strict_date_optional_time||epoch_millis"
        }}}}}
}

編輯我將嘗試改寫它...我想獲取“ histogram_samples_value”定義的每個存儲桶的單位數。 這意味着該計數的總和應為單位總數。 為了測試它,我編寫了一個查詢,該查詢僅過濾一個單位(許多具有不同樣本值的文檔)-除一個“ histogram_samples_value”存儲桶外,所有存儲桶均應包含count = 0,而一個存儲桶應包含count = 1。

我認為您可以通過反向嵌套聚合來獲得所需的結果,如下所示:

POST /test_index/_search
{
   "size": 0,
   "aggs": {
      "nested_samples": {
         "nested": {
            "path": "samples"
         },
         "aggs": {
            "histogram_samples_value": {
               "histogram": {
                  "field": "samples.value",
                  "interval": 10
               },
               "aggs": {
                  "reverse_nested_doc": {
                     "reverse_nested": {},
                     "aggs": {
                        "terms_unit_id": {
                           "terms": {
                              "field": "unit.id"
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
}

這是我用來測試的一些代碼:

http://sense.qbox.io/gist/e93dbddbbc4a841af5d9ce687a543a2914457d31

暫無
暫無

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

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