0
假設以下stockInWarehouse
模式:在Elasticsearch中選擇TOP + GROUP BY + SHORT?
{
product_db: {
mappings: {
stockInWarehouse: {
properties: {
sku: {
type: "string"
},
arrivalTime: {
type: "date",
format: "dateOptionalTime"
}
}
}
}
}
}
的數據stockInWarehouse
樣子:
{
"hits": {
"total": 5,
"hits": [
{
"_index": "product_db",
"_type": "stockInWarehouse",
"_id": "1",
"_source": {
"sku": "item 1",
"arrivalTime": "2015-11-11T19:00:10.231Z"
}
},
{
"_index": "product_db",
"_type": "stockInWarehouse",
"_id": "2",
"_source": {
"sku": "item 2",
"arrivalTime": "2015-11-12T19:00:10.231Z"
}
},
{
"_index": "product_db",
"_type": "stockInWarehouse",
"_id": "3",
"_source": {
"sku": "item 1",
"arrivalTime": "2015-11-12T19:35:10.231Z"
}
},
{
"_index": "product_db",
"_type": "stockInWarehouse",
"_id": "4",
"_source": {
"sku": "item 1",
"arrivalTime": "2015-11-13T19:56:10.231Z"
}
},
{
"_index": "product_db",
"_type": "stockInWarehouse",
"_id": "5",
"_source": {
"sku": "item 3",
"arrivalTime": "2015-11-15T19:56:10.231Z"
}
}
]
}
}
我所試圖做的是通過arrivalTime取TOP文件(也就是最近的文檔)但我希望他們是排序其他字段(sku)和限制可用sku。預期的結果是這樣的:
{
"hits": {
"total": 3,
"hits": [
{
"_index": "product_db",
"_type": "stockInWarehouse",
"_id": "5",
"_source": {
"sku": "item 3",
"arrivalTime": "2015-11-15T19:56:10.231Z"
}
},
{
"_index": "product_db",
"_type": "stockInWarehouse",
"_id": "4",
"_source": {
"sku": "item 1",
"arrivalTime": "2015-11-13T19:56:10.231Z"
}
},
{
"_index": "product_db",
"_type": "stockInWarehouse",
"_id": "2",
"_source": {
"sku": "item 2",
"arrivalTime": "2015-11-12T19:00:10.231Z"
}
}
]
}
}
如果我排序arrivalTime
,結果SKU列表將包含item 3, item 1, item 1, item 2, item 1
(一式兩份)。如果我按sku
排序,則結果列表將不會反映正確的到達時間順序。
這種類型的查詢可能在Elasticsearch中嗎?我怎樣才能存檔這個?
我的猜測是,你可以通過使用一個桶聚合按sku進行分組,以及按到達時間排序的標準排序。你試過這個嗎? – Phil