Closed 1eeing closed 3 years ago
SORT
sorts RESULT
first by _score
and then by _id
. In the example above all each item in RESULT
has the same _score
value of 2.2, so sorting will have no effect.
What do you want to sort by?
SORT
sortsRESULT
first by_score
and then by_id
. In the example above all each item inRESULT
has the same_score
value of 2.2, so sorting will have no effect.What do you want to sort by?
I want to sort by price, but I don't know how to do it. Could you please give me an example?
Something like this maybe? ->
QUERY({
SEARCH: [{
FIELD: 'price'
}]
}, {
SORT: {
TYPE: 'NUMERIC',
DIRECTION: 'DESCENDING',
FIELD: '_match.price'
}
}
Or, to use your original query ->
db.QUERY({
SEARCH: ['make:Volvo', 'drivetrain:Petrol', { FIELD: 'price' }] // <- search for price
}, {
SORT: {
TYPE: 'NUMERIC',
DIRECTION: 'DESCENDING',
FIELD: '_match.price', // <- prefix 'price' with '_match.'
},
})
EDIT- added { FIELD: 'price' }
to QUERY
OK, I used a combination of the two to solve the problem. Because I found that if there is no price in the SEARCH, then there is no price in the results, and it cannot be sorted correctly. Thank you.
Yes- its not totally intuitive- this is probably something that could be explained better in the docs
@fergiemcdowall It seems the above API has changed and the docs are not updated. Reading the tests it seems you can now only sort based on what you specified in SCORE, is that correct?
Yes, the API for SORT
was updated in version 3, and it pretty much works as you say @robgietema
I have just updated the docs to make them a bit clearer. They weren't wrong as far as I can see, but they needed some clarification- SORT
defaults to the contents of _score
in every result, and SCORE.FIELD
will only work if it is present in _match
in every result.
DEMO as follows:
When I did not pass in SORT, the result is as follows:
But when I pass in, the result is still the same:
I don't know what to do. Please help.