Laravel Scout does not respect Algolia's order - laravel

I try to use Laravel Scout with Algolia as my search engine.
I use it like this
MyModel::search('my query')->paginate()->pluck('id');
Problem is, Algolia returns the correct order let's say some records with ids 3, 1, 2 but when Laravel fetches the records on the database, the ids are like 1, 2, 3 (ordered by id).
Am I using Scout wrong?

Regarding the issue #341 on the scout repository, it seems to be a scout problem being investigated.
EDIT
The issue is now fixed as of PR #369 ( version > 7.0)

You have to edit vendor file in your laravel project:
vendor/laravel/scout/src/Searchable.php
and edit return statement of getScoutModelsByIds() function into:
$ids_ordered = implode(',', $ids);
return $query->whereIn(
$this->getScoutKeyName(), $ids
)->orderByRaw(DB::raw("FIELD(id, $ids_ordered)"))->get();

Related

How to paginate the query results when using Laravel eloquent relationships

I have the following query which retrieves the child configuration records (confch) associated with the parent configuration (confp) and it works fine.
$data["items"] = Confp::find(decrypt($type))->Confch;
I just need to paginate the results.
adding ->paginate(10) would definitly not work as it does not exist in the resulting collection. I found nothing helping in the laravel documentation.
How can I do that please?
Try this instead of above code.
$confp = Confp::find(decrypt($type));
$data["items"] = $confp->setRelation('Confch',$confp->Confch()->paginate(10));

How to check eloquent queries in laravel 5?

Im trying to check the eloquent queries to check for n+1 problems.
Im using this "Event::listen('illuminate.query', function($sql){
var_dump($sql);
});" on top of web.php routes file but no query appears. It seems that is because of the version of laravel, that is laravel 5.
Do you know in this version how to properly check the eloquent queries?
Add this to AppServiceProvider::boot() (documentation):
DB::listen(function ($query) {
var_dump($query);
});
try this library dude https://github.com/barryvdh/laravel-debugbar , you will know all of your query

Live Search Using Laravel

I can't find any good plugins for a live search using Laravel, does one exist? I've found a few jQuery autocomplete plugins but I'm looking for something like this: https://github.com/iranianpep/ajax-live-search
I tried implementing the above yesterday (for about 10 hours) and couldn't get it to work due to the MySQL queries they use vs Laravel's implementation
any thoughts or information is appreciated
I also tried using a large Laravel query to receive data (I want to search more than one column) but using the
User::where('name', 'like', $query)->orWhere('username', 'like', $query)->etc.
format didn't even match the name for me. Is there such a thing as a more advanced algorithm to search?
Thanks
Zach
Laravel Scout is your answer.
Laravel Scout provides a simple, driver based solution for adding full-text search to your Eloquent models. Using model observers, Scout will automatically keep your search indexes in sync with your Eloquent records.
Currently, Scout ships with an Algolia driver; however, writing custom drivers is simple and you are free to extend Scout with your own search implementations.
I would personally recommend Algolia as well, as it is very sophisticated, has a free entry level, and is better than any search you could come up with by yourself.
You can set the ranking yourself in their backend, it will regard typos made in your search, and so much more. Plus it is cloud powered!
With Laravel Scout any searching is made a breeze just like:
$users = App\User::search('Chris')->get();
What's really cool:
$users = App\User::search('Chrs')->get();
Both $users return the same results. Even with a typo.
For more information you can read up in the official Laravel Scout documentation.
If you are a member of Laracasts, check out these screencasts. And even if you are not, it's really cheap and you will learn a lot on Laravel Scout and live search!

Algolia save only strings using Laravel 5.4

I use Laravel 5.4 and i integrate algolia with my project, but I have some problems with data type of columns.
If I run the command from the localhost, status columns (and others) appear like integer in algolia database, but if I run the same artisan command from the production environment, the status column is now string, and I can't use ->where('status', 1) in my code, because algolia can use only integers for where clauses.
Is there a problem with my database? But is the same database from my localhost, same mysql version...
I don't know what could be the reason of this, but I think I know a good work around.
Let me also say that Algolia can have string as a where clause, but it's an intentional limitation of Laravel Scout.
You can use the toSearchableArray in your model to customize the array sent to Algolia. I'd use it to cast the data.
https://laravel.com/docs/5.5/scout#configuring-searchable-data
You should have something like this:
public function toSearchableArray()
{
$data = $this->toArray();
$data['attribute'] = (int) $data['attribute'];
return $data;
}

AlgoliaException in Client.php line 992: Index product_data does not exist

I just added laravel scout and configured everything as per documentation but when I just tested the query return \App\ProductData::search('')->get(); it returns me following error
AlgoliaException in Client.php line 992:
Index product_data does not exist
and I have no clue why this error is being generated. Moreover, I am not seeing any indexes or records in my algolia dashboard. Any help would be much appreciated.
Thanks
Have you correctly followed the instructions? You need to add a few things to your models in order to use Scout.
According to your error, algolia cannot find the model index. In ProductData, override the method from the Searchable trait:
public function searchableAs()
{
return 'product_data_index';
}
For those people who have this problem.
If you follow instruction on Laravel official hompage then successfully imported index and facing this error. Don't worry just because you've just imported index and Algolia hasn't finish it work. Wait for a while. If still can't, flush your index, restart your Laravel app and import Scout index again.
Enjoy :)
As Jan Petr suggests, it seems like you forgot to reindex your data first.
php artisan scout:import "App\ProductData"
After that you should see your data in Algolia's dashboard.

Resources