我有以下映射:http://pastebin.com/FgtC5aaw(包括样本数据)
{
"columns": [
{
"name": "First column",
"position": 1
},
{
"name": "Second column",
"position": 2
},
{
"name": "Third column",
"position": 3
}
],
"name": "Database name"
}
我想要的是"数据库"文档,但每个列的列按列排序按降序排列。所以:
{
"name": "Database name",
"columns": [
{
"name": "Third column",
"position": 3
},
{
"name": "Second column",
"position": 2
},
{
"name": "First column",
"position": 1
}
]
}
我尝试使用" sort" on" columns.position",但我认为它只会排序所有"数据库"它上面的文档的嵌套列。
Elasticsearch可以得到这样的结果吗?
答案 0 :(得分:2)
如果您事先知道,您将始终需要按nested
的降序检索列columns.position
对象,您只需使用columns
列表索引数据库文档已正确排序。
如果情况并非如此,则有一种方法可以使用inner_hits
对嵌套的columns
列表进行排序:
curl -XPOST localhost:9200/databases/database/_search -d '{
"_source": false, <---- set this to true to get database fields too
"query": {
"nested": {
"path": "columns",
"query": {
"match_all": {}
},
"inner_hits": {
"sort": {
"columns.position": "desc"
}
}
}
}
}'
将产生:
{
...
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [ {
"_index" : "databases",
"_type" : "database",
"_id" : "1",
"_score" : 1.0,
"inner_hits" : {
"columns" : {
"hits" : {
"total" : 3,
"max_score" : null,
"hits" : [ {
"_index" : "databases",
"_type" : "database",
"_id" : "1",
"_nested" : {
"field" : "columns",
"offset" : 2
},
"_score" : null,
"_source":{"name":"Third column","position":3},
"sort" : [ 3 ]
}, {
"_index" : "databases",
"_type" : "database",
"_id" : "1",
"_nested" : {
"field" : "columns",
"offset" : 1
},
"_score" : null,
"_source":{"name":"Second column","position":2},
"sort" : [ 2 ]
}, {
"_index" : "databases",
"_type" : "database",
"_id" : "1",
"_nested" : {
"field" : "columns",
"offset" : 0
},
"_score" : null,
"_source":{"name":"First column","position":1},
"sort" : [ 1 ]
} ]
}
}
}
} ]
}
}