'Elasticsearch Sorting on Multiple Nested Fields with Nested Filtering
Given the following data with nested objects (members within teams), I need to sort objects on multiple nested fields, first by height, then by weight. This all needs to respect the filtering that is done on other nested fields (just position in this example).
Data
PUT sample
{
"mappings": {
"dynamic": "strict",
"properties": {
"teamId": { "type": "keyword", "index": true, "doc_values": true },
"members": {
"type": "nested",
"properties": {
"memberId": { "type": "keyword", "index": true, "doc_values": true },
"position": { "type": "keyword", "index": true, "doc_values": true},
"height": { "type": "integer", "index": true, "doc_values": true},
"weight": { "type": "integer", "index": true, "doc_values": true}
}
}
}
}
}
PUT sample/_doc/1
{
"teamId" : "A"
, "members" :
[
{ "memberId" : "A1_X" , "position": "X", "height": 70, "weight": 195}
, { "memberId" : "A2_Y" , "position": "Y", "height": 70, "weight": 170}
, { "memberId" : "A3_Z" , "position": "Z", "height": 75, "weight": 210}
]
}
PUT sample/_doc/2
{
"teamId" : "B"
, "members" :
[
{ "memberId" : "B1_Z" , "position": "Z", "height": 80, "weight": 220 }
, { "memberId" : "B2_X" , "position": "X", "height": 75, "weight": 190 }
, { "memberId" : "B3_X" , "position": "X", "height": 70, "weight": 200 }
, { "memberId" : "B4_Y" , "position": "Y", "height": 70, "weight": 170 }
]
}
PUT sample/_doc/3
{
"teamId" : "C"
, "members" :
[
{ "memberId" : "C1_Y" , "position": "Y", "height": 70, "weight": 190 }
, { "memberId" : "C2_X" , "position": "X", "height": 75, "weight": 180 }
, { "memberId" : "C3_Z" , "position": "Z", "height": 75, "weight": 225 }
]
}
Query
POST sample/_search?filter_path=hits.hits.inner_hits.members.hits.hits._source.height,hits.hits.inner_hits.members.hits.hits._source.weight,hits.hits.sort,hits.hits._source.teamId
{
"size" : 3
, "track_total_hits" : true
, "query" : { "bool" : { "filter" : [
{ "match_all" : { } }
, { "nested": { "path": "members" , "query": { "bool": { "must": [
//nested filters
{ "term" : { "members.position" : "X" } }
] } }
, "inner_hits" : { "size" : 3 }
} }
]}}
, "sort": [
{ "members.height": { "order": "asc" , "nested": { "path": "members", "filter": { "bool": { "must": [
//copy all nested filters below
{ "term" : { "members.position" : "X" } }
] } } } } }
, { "members.weight": { "order": "asc" , "nested": { "path": "members", "filter": { "bool": { "must": [
//copy all nested filters below
{ "term" : { "members.position" : "X" } }
] } } } } }
, { "teamId": { "order": "asc" } }
]
}
Results
{
"hits" : {
"hits" : [
{
"_source" : {
"teamId" : "B"
},
"sort" : [
70,
190,
"B"
],
"inner_hits" : {
"members" : {
"hits" : {
"hits" : [
{
"_source" : {
"weight" : 190,
"height" : 75
}
},
{
"_source" : {
"weight" : 200,
"height" : 70
}
}
]
}
}
}
},
{
"_source" : {
"teamId" : "A"
},
"sort" : [
70,
195,
"A"
],
"inner_hits" : {
"members" : {
"hits" : {
"hits" : [
{
"_source" : {
"weight" : 195,
"height" : 70
}
}
]
}
}
}
},
{
"_source" : {
"teamId" : "C"
},
"sort" : [
75,
180,
"C"
],
"inner_hits" : {
"members" : {
"hits" : {
"hits" : [
{
"_source" : {
"weight" : 180,
"height" : 75
}
}
]
}
}
}
}
]
}
}
So in the query above, I'm trying to
- Find teams that have members with position X.
- Sort the teams based on height then weight (both ascending) of such members, where the height and weight used must be from the same members.
The query is close, but has 2 problems I'd like to fix.
- It requires that I copy all the nested filters for each sorting field. Is there a syntax that won't require me to do this?
- The height it uses to sort by may not be from the same member as the weight that it uses to sort by. In the results above, you can see that it puts team B first because it uses the height of 70 from B3_X and the weight of 190 from B2_X, which is not what I want. I want it to use the height of 70 from B3_X and the weight of 200 from the same member.
(Note that a hack I want to avoid is concatenating the 2 sorting fields into a single string or integer. Although this would solve the problem as stated, it creates other problems in the real world scenario I'm basing this example on.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
