我已將此檔案編入索引(索引:customer-v1,型別:customer,_id:123):
GET customers-v1/customer/123
這導致:
{
"_index" : "customers-v1",
"_type" : "customer",
"_id" : "123",
...
"_source" : {
"name": ...
"age": ...
"payments": [
{"month": "2021-01", "amount": 10},
{"month": "2021-02", "amount": 100},
{"month": "2021-03", "amount": 30},
{"month": "2021-04", "amount": 56},
]
}
}
現在我需要通過腳本回傳一個計算欄位(連同原始資料),它必須回傳欄位上的最后一項 payments
uj5u.com熱心網友回復:
您可以scripted_fields在搜索查詢中使用以查找陣列的最后一個元素。
POST customers-v1/_search
{
"stored_fields": [
"_source"
],
"script_fields": {
"lastPayment": {
"script": {
"source": "def payments = params['_source']['payments']; return payments[payments.length - 1];",
"lang": "painless"
}
}
}
}
上面的查詢將獲取源以及陣列的最后一個元素:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "customers-v1",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "...",
"age": "...",
"payments": [
{
"month": "2021-01",
"amount": 10
},
{
"month": "2021-02",
"amount": 100
},
{
"month": "2021-03",
"amount": 30
},
{
"month": "2021-04",
"amount": 56
}
]
},
"fields": {
"lastPayment": [
{
"amount": 56,
"month": "2021-04"
}
]
}
}
]
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/335551.html
標籤:弹性搜索
下一篇:同一欄位的彈性搜索多個過濾器值
