【发布时间】:2017-04-10 13:06:00
【问题描述】:
使用弹性搜索 5.2。
接下来是我的数据格式:
{
"_id": "635636",
"_index": "test",
"_source": {
"ad_id": 9368,
"body": 1,
"drivetrain": 1,
"engine_capacity": 1,
"fuel_type": 1,
"has_exchange": false,
"id": 635636,
"manufacturer_id": 12,
"model_id": 10,
"odometer_state": 110000,
"price_byn": 22802,
"price_usd": 12000,
"source": 2,
"source_date": "2016-10-06",
"source_id": "12194309",
"state": 2,
"state_date": "2017-03-07",
"transmission_type": 1,
"year": 2012
},
"_type": "ads",
"_version": 4,
"found": true
}
我正在尝试按日期获取最小/最大/平均聚合。使用下一个查询:
{
"size":0,
"aggs":{
"avg_price_per_day":{
"date_histogram":{
"field":"state_date",
"interval":"day"
},
"aggs":{
"prices":{
"avg":{
"field":"price_usd"
}
}
}
},
"max_price_per_day":{
"date_histogram":{
"field":"state_date",
"interval":"day"
},
"aggs":{
"prices":{
"max":{
"field":"price_usd"
}
}
}
},
"min_price_per_day":{
"date_histogram":{
"field":"state_date",
"interval":"day"
},
"aggs":{
"prices":{
"min":{
"field":"price_usd"
}
}
}
},
"max_daily_price":{
"max_bucket":{
"buckets_path":"max_price_per_day>prices"
}
},
"min_daily_price":{
"min_bucket":{
"buckets_path":"min_price_per_day>prices"
}
},
"avg_daily_price":{
"max_bucket":{
"buckets_path":"avg_price_per_day>prices"
}
}
},
"query":{
"bool":{
"filter":[
{
"range":{
"price_usd":{
"gt":0
}
}
},
{
"term":{
"manufacturer_id":{
"value":11,
"boost":1
}
}
},
{
"term":{
"model_id":{
"value":7,
"boost":1
}
}
}
]
}
}
}
但它只返回一个月的聚合:
{
"took":23,
"timed_out":false,
"_shards":{
"total":1,
"successful":1,
"failed":0
},
"hits":{
"total":6046,
"max_score":0.0,
"hits":[
]
},
"aggregations":{
"avg_price_per_day":{
"buckets":[
{
"key_as_string":"2017-01-02",
"key":1483315200000,
"doc_count":1494,
"prices":{
"value":4431.045515394913
}
},
{
"key_as_string":"2017-01-09",
"key":1483920000000,
"doc_count":0,
"prices":{
"value":null
}
},
{
"key_as_string":"2017-01-16",
"key":1484524800000,
"doc_count":840,
"prices":{
"value":4299.322619047619
}
},
{
"key_as_string":"2017-01-23",
"key":1485129600000,
"doc_count":3712,
"prices":{
"value":4383.441540948276
}
}
]
},
"max_price_per_day":{
"buckets":[
{
"key_as_string":"2017-01-02",
"key":1483315200000,
"doc_count":1494,
"prices":{
"value":45000.0
}
},
{
"key_as_string":"2017-01-09",
"key":1483920000000,
"doc_count":0,
"prices":{
"value":null
}
},
{
"key_as_string":"2017-01-16",
"key":1484524800000,
"doc_count":840,
"prices":{
"value":15500.0
}
},
{
"key_as_string":"2017-01-23",
"key":1485129600000,
"doc_count":3712,
"prices":{
"value":45000.0
}
}
]
},
"min_price_per_day":{
"buckets":[
{
"key_as_string":"2017-01-02",
"key":1483315200000,
"doc_count":1494,
"prices":{
"value":110.0
}
},
{
"key_as_string":"2017-01-09",
"key":1483920000000,
"doc_count":0,
"prices":{
"value":null
}
},
{
"key_as_string":"2017-01-16",
"key":1484524800000,
"doc_count":840,
"prices":{
"value":200.0
}
},
{
"key_as_string":"2017-01-23",
"key":1485129600000,
"doc_count":3712,
"prices":{
"value":200.0
}
}
]
},
"max_daily_price":{
"value":45000.0,
"keys":[
"2017-01-02",
"2017-01-23"
]
},
"min_daily_price":{
"value":110.0,
"keys":[
"2017-01-02"
]
},
"avg_daily_price":{
"value":4431.045515394913,
"keys":[
"2017-01-02"
]
}
}
}
我还为 2 月和 3 月的数据编制了索引,但它不包含在聚合中。如何将它们全部包含在内?
更新
curl -XPOST localhost:9200/avtostat/ads/_search -d '{"query":{"bool":{"filter":[{"range":{"state_date":{"gt":"2017-02-01"}}},{"range":{"price_usd":{"gt":0}}},{"term":{"manufacturer_id":{"value":11,"boost":1}}},{"term":{"model_id":{"value":7,"boost":1}}}]}}}'
{
"took":166,
"timed_out":false,
"_shards":{
"total":1,
"successful":1,
"failed":0
},
"hits":{
"total":6046,
"max_score":0.0,
"hits":[
{
"_index":"avtostat",
"_type":"ads",
"_id":"272894",
"_score":0.0,
"_source":{
"id":272894,
"ad_id":111602,
"manufacturer_id":11,
"model_id":7,
"fuel_type":3,
"engine_capacity":1.6,
"transmission_type":2,
"year":1999,
"body":6,
"drivetrain":1,
"state":2,
"odometer_state":303000,
"has_exchange":true,
"price_byn":4816,
"price_usd":2500,
"state_date":"2017-02-05",
"source":1,
"source_id":"3215650",
"source_date":"2017-02-05"
}
},
...
]
}
}
【问题讨论】:
-
您确定您有与您的查询相匹配的其他月份的文件吗?您的查询返回
"hits":{ "total":6046匹配文档,这些文档与每个存储桶中的文档总和相匹配。尝试使用extended_boundsandmin_doc_count: 0来查看您期望的存储桶是否可能是空的? -
@AndreiStefan 谢谢你的回答。我已经更新了我的问题,你可以看到数据存在。但即使对于这个过滤器 - 6046 次点击。我很困惑
-
鉴于您查询中的过滤器,我认为您提供的两个文档都不会匹配,只有其中一个会匹配。你有
manufacturer_id: 11,但是这两个文件中只有一个有它。您对bool查询的意图是什么? -
也许您想使用
should语句而不是filter? -
@AndreiStefan 第一项只是为了演示数据格式。我需要按日期汇总制造商和型号 ID 的最小/最大/平均价格。将过滤器更改为必须或应该不会影响任何事情。
标签: elasticsearch