-
Notifications
You must be signed in to change notification settings - Fork 343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
orderBy doesn't work #223
Comments
Hi @calvinmuller, Algolia doesn't not allow ordering at query time. The data are ordered while being indexed, which makes the query much faster. You will need to create a replicas in your Algolia dashboard and sort this index by name. Then with Scout, you can use the $products = Product::search('')
->within('products_by_name')
->paginate(); |
nailed it, thanks! |
Hi, sorry to post on this closed issue but I've tried @julienbourdeau solution and the results are always ordered by IDs... Anything on this? |
@peterpan666 Thanks for bring that to our attention. I just have added your issue into my todo list, and I will check it soon as possible. |
Wow thanks @nunomaduro I've hesitated to send you an email but thanks for noticing this issue 😊 Maybe should I open another issue? |
@peterpan666 Just made a quick test with @julienbourdeau solution and everything works as expected. Could you share the code you are using? |
Hi @nunomaduro, happy new year and thanks for the reply! I've managed to do some tests, here are the results. I've got a model called
On Algolia, I've one Now I want to load them like so
So the first line will fetch records from database and pluck the ids, the second line will fetch records from Algolia and display the ids. We can see that the ids are the same but not ordered the same way. And it's also true if I load the records from a different index
Am i missing something? |
No worries, I am here to help you. First, please check if |
Thanks @nunomaduro ! Yes it is, the raw result from Algolia are ok and in the right order, I think the problem comes up when Laravel load the records from my database, the ids are then just ordered ASC... Here is a screenshot from one Algolia record |
I see. I will run a quick test tomorrow, and open a pull request to fix this issue. |
Ok great! I've got a workaround for this but I wasn't sure that was the greatest way to do this or if I was missing something... What I've done now is rewriting the
The downside with this is the |
That's not good. The solution is just use the same algorith that I have done for Scout Extended: After resolving the models from the database, just order them using the $ids of Algolia. |
@peterpan666 Just created an issue for this: #341. |
Awesome! Thanks a lot @nunomaduro ! |
If you are using Meilisearch you can do this:
|
The Meilisearch offers sorting and it works nice. Ordering requires setting "orderable" attributes before querying. I sent PR with direct implementation of orderBy : |
Got it to work by wrapping it into a query:
|
Hi there,
I'm using scout with algolia to index a large database of products, however when trying to sort the results, i'm not getting the desired results.
Anything i'm missing for this to return results ordered by name?
The text was updated successfully, but these errors were encountered: