【问题标题】:Elasticsearch aggregation queryElasticsearch 聚合查询
【发布时间】:2015-12-16 10:11:17
【问题描述】:

我有一个存储在 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"
      }
    },
    ...
  ]
}

我想针对它运行一个聚合查询,该查询将为属性 samples.value 的每个存储桶返回 unit.id 的计数, 查询应基于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 。

【问题讨论】:

  • 你能添加你的映射吗?这会让事情变得更容易。
  • 添加了映射,如有必要,我什至可以更改文档结构 - 我预计最多有 1 亿个。该索引中的文档。
  • 这似乎更好。也许我误解了你的问题,但你为什么使用histogram 聚合?您的要求似乎根本不需要它。另外,您能否添加最小预期输出?

标签: java elasticsearch nosql


【解决方案1】:

我认为你可以通过reverse nested aggregation 得到你想要的,就像这样:

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

【讨论】:

  • 嘿,感谢您的示例,但是此查询返回的直方图存储桶中填充了基于“unit.id”术语的存储桶,并且它们对于每个“直方图”存储桶都是相同的。这不正是我想要的。我会尝试改写它......我想获得由“histogram_samples_value”定义的每个桶的单位数。这意味着这些计数的总和应该是单位总数。为了测试它,我写了一个查询,它只过滤一个样本——除了一个“histogram_samples_value”桶之外的所有桶都应该包含 count=0,一个桶应该包含 count = 1
  • 我可能不得不以某种方式将过滤器聚合与基数聚合一起使用。
猜你喜欢
  • 2022-01-18
  • 2014-10-22
  • 2014-11-06
  • 2018-12-11
  • 2019-12-06
  • 2018-10-01
  • 2014-01-05
  • 2019-11-10
  • 1970-01-01
相关资源
最近更新 更多