Hi!
I have a collection of entities and I need to group them by RelationId and select entity with biggest Revision in each group. Result should be ordered by "title" and support paging. Is it possible to do this with ElasticSearch?
Data
RelationId | Title | Revision
-------------------------------
1 | Abx | 1
1 | Zbc | 2
2 | Text | 1
or json
[{
"relationId": 1,
"title": "Abx",
"revision": 1
}, {
"relationId": 1,
"title": "Zbc",
"revision": 2
}, {
"relationId": 2,
"title": "Text",
"revision": 1
}]
Expected result ordered by Title
RelationId | Title | Revision
-------------------------------
2 | Text | 1
1 | Zbc | 2
I tried "collapse", but result is not sorted correctly
{
"collapse": {
"field": "relationId",
"inner_hits": {
"name": "most_recent",
"size": 1,
"sort": [
{
"revision": "desc"
}
]
}
},
"sort": [
{
"title": {
"order": "asc"
}
}
]
}
and got this (from inner_hits). Inner_hits contains correct values but overall result set is not sorted correctly
RelationId | Title | Revision
-------------------------------
1 | Zbc | 2
2 | Text | 1
result
{
"hits" : {
"hits" : [
{
"_index" : "index",
"_type" : "_doc",
"_id" : "e2de1644-0f56-4d38-a6fc-f6552901ad51",
"fields" : {
"relationId" : [
1
]
},
"sort" : [
"Abx"
],
"inner_hits" : {
"most_recent" : {
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "index",
"_type" : "_doc",
"_id" : "653b6ef4-bfa3-4137-a3c5-1a43f63195c2",
"_score" : null,
"_source" : {
"id" : "653b6ef4-bfa3-4137-a3c5-1a43f63195c2",
"revision" : 2,
"title" : "Zbc",
"relationId" : 1
},
"sort" : [
2.0
]
}
]
}
}
}
},
{
"_index" : "index",
"_type" : "_doc",
"_id" : "0d150de3-95b0-4911-9bf6-623d7ce9997d",
"_score" : null,
"fields" : {
"relationId" : [
2
]
},
"sort" : [
"Text"
],
"inner_hits" : {
"most_recent" : {
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "index",
"_type" : "_doc",
"_id" : "0d150de3-95b0-4911-9bf6-623d7ce9997d",
"_score" : null,
"_source" : {
"id" : "0d150de3-95b0-4911-9bf6-623d7ce9997d",
"revision" : 1,
"title" : "Text",
"relationId" : 2
},
"sort" : [
1.0
]
}
]
}
}
}
}
]
}
}