elasticsearch ElasticSearch聚合的SQl等价相关查询
我有一个编写聚合的用例,如果用SQL编写,可以使用相关查询实现聚合 我有一个名为listings的索引,其中属性/列是ListDate、ListPrice、SoldDate、SoldPrice和OffMarketDate ListDate不可为空,但SoldDate、SoldPrice和OffMarketDate可以为空 我想根据以下要求从上述索引中聚合统计数据elasticsearch ElasticSearch聚合的SQl等价相关查询,elasticsearch,kibana,correlated-subquery,elasticsearch,Kibana,Correlated Subquery,我有一个编写聚合的用例,如果用SQL编写,可以使用相关查询实现聚合 我有一个名为listings的索引,其中属性/列是ListDate、ListPrice、SoldDate、SoldPrice和OffMarketDate ListDate不可为空,但SoldDate、SoldPrice和OffMarketDate可以为空 我想根据以下要求从上述索引中聚合统计数据 我想有每月的统计数据,我看到可以通过 日期组织分类 自 DateHistorograMaggregation,我想找到以下列表: 示
- 我想有每月的统计数据,我看到可以通过 日期组织分类
- 自
DateHistorograMaggregation,我想找到以下列表:
示例:对于2019年1月,获取(ListDate<2019年2月1日)和(SoldDate为null或SoldDate的所有列表。请查看以下详细信息和信息,了解如何解决此问题: 映射: 请注意,我根据您的问题构建了上述映射 样本文件: 请注意,我没有在文档5和文档6中添加
和soldDate
,因为这比使用offMarketDate
值更好 请求查询: 因此,我为您的用例提出了以下查询 同样为了聚合,假设我已经计算了null
在2020年1月的月份listDate
- (
在2020年1月soldDate
之前
或null
)和soldDate
- (
2020年1月之前的offMarketDate
或null
)offMarketDate
上面的查询非常容易阅读和解释。我建议阅读以下我使用过的不同查询:POST listings/_search { "query": { "bool": { "must": [ { "range": { "listDate": { "gte": "2020-01-01", "lte": "2020-02-01" } } }, { "bool": { "should": [ { "range": { "soldDate": { "lte": "2020-01-01" } } }, { "bool": { "must_not": [ { "exists": { "field": "soldDate" } } ] } } ], "minimum_should_match": 1 } }, { "bool": { "should": [ { "range": { "offMarketDate": { "lte": "2020-01-01" } } }, { "bool": { "must_not": [ { "exists": { "field": "offMarketDate" } } ] } } ], "minimum_should_match": 1 } } ] } }, "aggs": { "my_histogram": { "date_histogram": { "field": "listDate", "calendar_interval": "month" }, "aggs": { "total_sales_price": { "sum": { "field": "soldPrice" } } } } } }
- 验证字段是否存在
将以1、2和5
的正确聚合和显示soldPrice
希望有帮助!请查看以下详细信息和信息,了解如何解决此问题: 映射: 请注意,我根据您的问题构建了上述映射 样本文件: 请注意,我没有在文档5和文档6中添加
和soldDate
,因为这比使用offMarketDate
值更好 请求查询: 因此,我为您的用例提出了以下查询 同样为了聚合,假设我已经计算了null
在2020年1月的月份listDate
- (
在2020年1月soldDate
之前
或null
)和soldDate
- (
2020年1月之前的offMarketDate
或null
)offMarketDate
上面的查询非常容易阅读和解释。我建议阅读以下我使用过的不同查询:POST listings/_search { "query": { "bool": { "must": [ { "range": { "listDate": { "gte": "2020-01-01", "lte": "2020-02-01" } } }, { "bool": { "should": [ { "range": { "soldDate": { "lte": "2020-01-01" } } }, { "bool": { "must_not": [ { "exists": { "field": "soldDate" } } ] } } ], "minimum_should_match": 1 } }, { "bool": { "should": [ { "range": { "offMarketDate": { "lte": "2020-01-01" } } }, { "bool": { "must_not": [ { "exists": { "field": "offMarketDate" } } ] } } ], "minimum_should_match": 1 } } ] } }, "aggs": { "my_histogram": { "date_histogram": { "field": "listDate", "calendar_interval": "month" }, "aggs": { "total_sales_price": { "sum": { "field": "soldPrice" } } } } } }
- 验证字段是否存在
将以1、2和5
的正确聚合和显示soldPrice
希望有帮助!也欢迎使用SOF,如果您认为您的问题已得到解决,请点击答案开头的
键和/或向上箭头
点击下方的
接受答案。谢谢您选择忍者-Kamal。这是一个很好的答案。它适用于给定的一个月。我看到您首先查询一月份的列表,然后添加该月份的聚合,我的问题稍微超出了这一点。假设数据跨越2019年的整个月份。我想要一个自动跨越2019年所有12个月的查询,每个月,aggr根据我提供的逻辑生成数据并运行聚合。我不想每月从我的应用程序调用一个查询。另外,欢迎使用SOF,如果您认为您的查询已得到解决,请点击答案开头的灰色勾号
向上箭头
键和/或
接受
answe,自由地向上投票
答案r单击其下方的
。谢谢你选择了ES Ninja-Kamal。这是一个很好的答案。它适用于给定的一个月。我看到你首先查询了一月份的列表,并添加了该月份的聚合,我的问题稍微超出了这一点。假设数据跨越了整个月份2019年。我需要一个自动跨越2019年所有12个月的查询,每个月根据我提供的逻辑聚合数据并运行聚合。我不想从我的应用程序中每月调用1个查询。灰色勾号
POST listings/_search { "query": { "bool": { "must": [ { "range": { "listDate": { "gte": "2020-01-01", "lte": "2020-02-01" } } }, { "bool": { "should": [ { "range": { "soldDate": { "lte": "2020-01-01" } } }, { "bool": { "must_not": [ { "exists": { "field": "soldDate" } } ] } } ], "minimum_should_match": 1 } }, { "bool": { "should": [ { "range": { "offMarketDate": { "lte": "2020-01-01" } } }, { "bool": { "must_not": [ { "exists": { "field": "offMarketDate" } } ] } } ], "minimum_should_match": 1 } } ] } }, "aggs": { "my_histogram": { "date_histogram": { "field": "listDate", "calendar_interval": "month" }, "aggs": { "total_sales_price": { "sum": { "field": "soldPrice" } } } } } }
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 3.0, "hits" : [ { "_index" : "listings", "_type" : "_doc", "_id" : "1", "_score" : 3.0, "_source" : { "listDate" : "2020-01-01", "listPrice" : "100.00", "soldDate" : "2019-12-25", "soldPrice" : "120.00", "offMarketDate" : "2019-12-20" } }, { "_index" : "listings", "_type" : "_doc", "_id" : "2", "_score" : 3.0, "_source" : { "listDate" : "2020-01-01", "listPrice" : "100.00", "soldDate" : "2019-12-24", "soldPrice" : "122.00", "offMarketDate" : "2019-12-20" } }, { "_index" : "listings", "_type" : "_doc", "_id" : "5", "_score" : 1.0, "_source" : { "listDate" : "2020-01-25", "listPrice" : "120.00" } } ] }, "aggregations" : { "my_histogram" : { "buckets" : [ { "key_as_string" : "2020-01-01T00:00:00.000Z", "key" : 1577836800000, "doc_count" : 3, "total_sales_price" : { "value" : 242.0 } } ] } } }