I've run into a weird issue with nested sorts. When specifying a sort as such:
"sort": [
{
"variations.price.regular": {
"mode": "min",
"order": "asc",
"nested": {
"path": "variations.price"
}
}
}
]
It works as expected and sort values like "sort" : [ 500 ]
are returned. However when specifying a nested sort with a filter
as such:
"sort": [
{
"variations.price.regular": {
"mode": "min",
"order": "asc",
"nested": {
"path": "variations.price",
"filter": {
"term": {
"variations.inventory.shop_id": 1
}
}
}
}
}
]
It does not work as expected and sort values like "sort": [ 9223372036854775807 ]
are returned.
Below is a full reproduction of this issue as a Kibana script:
DELETE /debug
PUT /debug
PUT /debug/_mapping/_doc
{
"properties": {
"name": {
"type": "text"
},
"variations": {
"type": "nested",
"properties": {
"name": {
"type": "text"
},
"inventory": {
"type": "nested",
"properties": {
"quantity": {
"type": "integer"
},
"shop_id": {
"type": "keyword"
}
}
},
"price": {
"type": "nested",
"properties": {
"regular": {
"type": "integer"
},
"shop_id": {
"type": "keyword"
}
}
}
}
}
}
}
PUT /debug/_doc/1
{
"name": "Product One",
"variations": [
{
"name": "Product One - Variation One",
"inventory": [
{
"quantity": 100,
"shop_id": 1
},
{
"quantity": 0,
"shop_id": 2
}
],
"price": [
{
"regular": 1000,
"shop_id": 1
},
{
"regular": 1000,
"shop_id": 2
}
]
},
{
"name": "Product One - Variation Two",
"inventory": [
{
"quantity": 0,
"shop_id": 1
},
{
"quantity": 100,
"shop_id": 2
}
],
"price": [
{
"regular": 2000,
"shop_id": 1
},
{
"regular": 2000,
"shop_id": 2
}
]
}
]
}
PUT /debug/_doc/2
{
"name": "Product Two",
"variations": [
{
"name": "Product Two - Variation One",
"inventory": [
{
"quantity": 0,
"shop_id": 1
},
{
"quantity": 100,
"shop_id": 2
}
],
"price": [
{
"regular": 3000,
"shop_id": 1
},
{
"regular": 3000,
"shop_id": 2
}
]
},
{
"name": "Product Two - Variation Two",
"inventory": [
{
"quantity": 0,
"shop_id": 1
},
{
"quantity": 100,
"shop_id": 2
}
],
"price": [
{
"regular": 4000,
"shop_id": 1
},
{
"regular": 4000,
"shop_id": 2
}
]
}
]
}
PUT /debug/_doc/3
{
"name": "Product Three",
"variations": [
{
"name": "Product Three - Variation One",
"inventory": [
{
"quantity": 100,
"shop_id": 1
},
{
"quantity": 100,
"shop_id": 2
}
],
"price": [
{
"regular": 500,
"shop_id": 1
},
{
"regular": 500,
"shop_id": 2
}
]
},
{
"name": "Product Three - Variation Two",
"inventory": [
{
"quantity": 0,
"shop_id": 1
},
{
"quantity": 0,
"shop_id": 2
}
],
"price": [
{
"regular": 6000,
"shop_id": 1
},
{
"regular": 6000,
"shop_id": 2
}
]
}
]
}
POST /debug/_search
{
"query": {
"nested": {
"path": "variations",
"query": {
"bool": {
"filter": [
{
"nested": {
"path": "variations.inventory",
"query": {
"bool": {
"filter": [
{
"term": {
"variations.inventory.shop_id": 1
}
},
{
"range": {
"variations.inventory.quantity": {
"gte": 1
}
}
}
]
}
}
}
}
]
}
}
}
},
"sort": [
{
"variations.price.regular": {
"mode": "min",
"order": "asc",
"nested": {
"path": "variations.price",
"filter": {
"bool": {
"filter": [
{
"term": {
"variations.inventory.shop_id": 1
}
},
{
"range": {
"variations.inventory.quantity": {
"gte": 1
}
}
}
]
}
}
}
}
}
]
}
POST /debug/_search
{
"query": {
"nested": {
"path": "variations",
"query": {
"bool": {
"filter": [
{
"nested": {
"path": "variations.inventory",
"query": {
"bool": {
"filter": [
{
"term": {
"variations.inventory.shop_id": 1
}
},
{
"range": {
"variations.inventory.quantity": {
"gte": 1
}
}
}
]
}
}
}
}
]
}
}
}
},
"sort": [
{
"variations.price.regular": {
"mode": "min",
"order": "asc",
"nested": {
"path": "variations.price"
}
}
}
]
}
Am I doing something wrong when adding the filter within the nested sort? The issue seems similar to Sort is out of order - using Nested Filter but there were no responses on that thread.
All tests run with Elasticsearch and Kibana 6.8.3 docker containers.