Hi, I’m having trouble with searching and sorting documents on a nested number field and was hoping someone could help.
My documents have a field from_price
, and it has a subfield amount
, which is a non-integer number field. I’ve indexed it as a Number
type with dynamic mapping disabled (search index snippet below) but I still can’t get sorting, or the range operator to work. However, sorting and range do both work on a top-level field (popularity
, an integer in this case).
When trying to sort, the documents returned are completely unchanged whether I put sort: {"from_price.amount": 1}
or sort: {"from_price.amount": -1}
.
When using the range operator with from_price.amount
I don’t get any documents returned at all.
Neither of these queries results in an error.
Below are a snippet of the search index and two search queries, one for sorting and the other for the range operator. In both queries, when changing from_price.amount
to popularity
both sort and range work. I’m aware that I can use $sort rather than the Atlas-specific sort, but I’m not sure if there’s a similar workaround for the range operator (specifically gte and lte).
Search index snippet (I have other fields indexed but this is what from_price
, and popularity
look like):
"mappings": {
"dynamic": false,
"fields": {
"from_price": {
"fields": {
"amount": {
"type": "number"
}
},
"type": "document"
},
"popularity": {
"type": "number"
}
}
}
Example aggregation for range operator:
[
{
$search: {
index: "search",
compound: {
must: [
{
range: {
gte: 1,
path: "from_price.amount",
},
},
{
text: {
query: ["US", "EU", "AE", "GB"],
path: "country_code",
},
},
],
},
},
},
]
Example aggregation for sort operator:
[
{
$search: {
index: "search",
compound: {
must: [
{
text: {
query: ["US", "EU", "AE", "GB"],
path: "country_code",
},
},
],
},
sort: {"from_price.amount": 1}
},
},
]