![](/img/trans.png)
[英]Elastic search query to filter documents based on the top value of nested array
[英]Sort elastic search query based on a nested key value array
我有一個json
{
"uniqueKey": "918084",
"dataValue": {
"metadata": {
"timestamps": [{
"key": "startTime",
"value": "2017-02-07T18:00:00-06:00"
}, {
"key": "processedTime",
"value": "2017-02-07T18:05:00-06:00"
}]
}
}
}
我必須編寫一個查詢以對startTime進行排序。 有誰能在彈性搜索中為此編寫查詢。 dataValue是一個嵌套字段。
您可以對 nested_path使用sort 。
假設您具有以下映射:
{
"test-so": {
"mappings": {
"with-dates": {
"properties": {
"datavalue": {
"type": "nested",
"properties": {
"metadata": {
"type": "nested",
"properties": {
"timestamps": {
"type": "nested",
"properties": {
"key": {
"type": "keyword"
},
"value": {
"type": "date",
"format": "date_optional_time"
}
}
}
}
}
}
},
"uniquekey": {
"type": "keyword"
}
}
}
}
}
}
您可以按以下方式使用排序 :
GET /test-so/with-dates/_search
{
"query": {
"nested": {
"path": "datavalue.metadata.timestamps",
"query": {
"term": {
"datavalue.metadata.timestamps.key": {
"value": "startTime"
}
}
}
}
},
"sort": [
{
"datavalue.metadata.timestamps.value": {
"order": "desc",
"nested_path": "datavalue.metadata.timestamps"
}
}
]
}
這將返回startTime上按日期排序的文檔。
希望有幫助,也許映射不完全相同。 (注意:在Elastic 5.1上測試)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.