Import into Algolia
When setting up Scout Extended, you probably have existing data that you would like to import.
Use the scout:import
Artisan command to import the data:
$
php artisan scout:import
The searchable
classes are automatically detected, but feel free to specify the searchable
class to import:
$
php artisan scout:import "App\Article"
Foe more information about importing data in a production environment, see Zero downtime re-imports.
Indexing issues
You may encounter indexing problems if you have cached configuration files, queue settings, connection errors, or record sizes.
Cached configuration files
If php artisan scout:import
doesn’t index your models, it’s possible that changes in your .env
or config/scout.php
file aren’t taking effect because they’re cached.
Clearing the cache may help:
1
2
3
4
php artisan cache:clear
php artisan route:clear
php artisan config:cache
composer dumpautoload
Queue settings
If queue
is set to true
in your config/scout.php
file, indexing jobs may be waiting to be processed by your queue worker.
To fix this issue, either run your queue worker and wait for it to process your jobs or set queue
to false
and index records synchronously.
Connection errors
Are you getting “Impossible to connect”, “Unable to connect”, or “Unreachable hosts” errors? First, make sure the issue isn’t at your end:
- Ensure you’re using the correct application ID and API key. Find these credentials on your Algolia dashboard.
- Check for recent changes in your code.
- Check the status of your data center provider.
If you’re using Firebase, you can only access Algolia from a paid Firebase tier.
If you can’t solve the problem yourself, contact the Algolia support team and provide them with the following information:
- The name of your framework integration (Laravel) and its version number
- A code snippet to reproduce the issue
- Error message or stack trace (if applicable)
- The name of the Algolia index that’s causing problems
- The exact UTC time of the event
- If you can’t connect to the Algolia API from your browser, send the output from community.algolia.com/diag/.
-
If you can’t connect to the Algolia API from your servers, send the output from the following command (run on any affected server):
Copy1
curl -sL https://algolia.com/downloads/diag.sh > ./diag.sh && sudo ./diag.sh YourApplicationID
Replace
YourApplicationID
with your Algolia application ID.
Record size
Any “Record at the position XX objectID=XX is too big” errors during indexing are because you’ve exceeded the size limit for records. Reduce the size of your records and try again.
Flushing and clearing
The scout:flush
Artisan command is the easiest way to flush the data from Algolia’s index:
$
php artisan scout:flush
Keep data in sync
Every time you save or update a model, Laravel emits an event. Scout listens for those events, informing your app to make an HTTP call to Algolia to update its index.
You don’t have to do anything else, you can use your searchable
class as usual. For example, in a controller you may have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class ArticleController extends Controller
{
/**
* Update the given article.
*
* @param Request $request
* @param string $id
* @return Response
*/
public function update(Request $request, $id)
{
Article::find(request('id'));
$article->title = request('title');
/**
* Scout will automatically persist the
* changes to your Algolia search index.
*/
$article->update();
}
}
It’s important to note that the data isn’t synced when using mass assignment methods, since the saved
and updated
events aren’t dispatched in that case:
1
2
3
4
5
6
/**
* Here, Scout will NOT persist the
* changes to your Algolia search index.
*/
Article::where('id', request('id'))
->update(['title' => request('title')]);
Conditional model sync
Sometimes you may want to sync a searchable
class just when something else is true. To do this, you can define a shouldBeSearchable
method on your searchable
class.
Scout Extended syncs your model if this method returns true
.
1
2
3
4
5
6
7
8
9
class Article extends Model
{
use Searchable;
public function shouldBeSearchable()
{
return $this->isPublished();
}
}
Pause indexing
Scout documentation shows a nice way to change a searchable model without triggering indexing calls using a callback.
You can also use a static method to turn off syncing. This is useful if you’re performing lots of SQL queries to change content. Once complete, you can sync the changes manually.
For example, in your DatabaseSeeder
class:
1
2
3
4
5
6
7
8
9
10
11
12
13
class DatabaseSeeder extends Seeder
{
public function run()
{
Article::disableSearchSyncing();
$this->call(ArticleSeeder::class);
Article::all()->searchable();
Article::enableSearchSyncing();
}
}