我在索引my_index
中有两种类型。一个是另一个的父母。该子名称为calendar
,父名称为stock
。一个stock
可以有多个calendar
条目,但不是反之亦然。我需要计算在一系列日期(大于或等于和小于或等于)中有库存的天数。我该怎么办?
stock
条目或多或少看起来像这样:
{
"_index": "my_index",
"_type": "stock",
"_id": "21321",
"_score": 1,
"_source": {
"description": "Screwdriver",
"size": "big",
"some_characteristic1": null,
"some_characteristic2": "Good",
"id": 21321,
...
}
}
calendar
条目或多或少看起来像这样:
{
"_index": "my_index",
"_type": "calendar",
"_id": "21321-20161129",
"_score": 1,
"_routing": "21321",
"_parent": "21321",
"_source": {
"available": true,
"date": "2016-11-29",
"prices": {
"price": 2150
}
}
}, ...
此查询的结果应该或多或少看起来像这样:
{
"description": "Screwdriver",
"size": "big",
"some_characteristic1": null,
"some_characteristic2": "Good",
"id": 21321,
"available_days_in_february": {
"count": 12,
"total_count": 28
}
}
sql中的类似查询可能如下所示:
SELECT s.*, sum(c.available = true) as count, count(c.available) as total_count
FROM stock as s
LEFT JOIN calendar as c ON s.id = c._parent
WHERE c.date >= 2017-02-01 AND c.date <= 2017-02-28