-
Notifications
You must be signed in to change notification settings - Fork 31
Index Data Into Meilisearch
Once you configured what entities/documents you want to index in Meilisearch, you are ready to send data. In the following section, we consider the following configuration.
meilisearch:
indices:
- name: posts
class: App\Entity\Post
- name: comments
class: App\Entity\Comment
Once your indices
config is ready, you can use the built-in console command to batch import all existing data.
# Import all indices
php bin/console meili:import
# Choose what indices to re-index by passing the index name
php bin/console meili:import --indices=posts,comments
# Choose a batch size to index your data. In this example, your documents will be added 1000 by 1000.
php bin/console meili:import --batch-size 1000
# Import setting a timeout
php bin/console meili:import --response-timeout=<timeout in ms>
Before re-indexing everything, you may want to clear the index first, see how to remove data.
Also, if you want to only create indexes without importing your data to Meilisearch:
# Create all indexes (based on the existing models)
PHP bin/console meili:create
# Create the post and comments indexes
php bin/console meili:create --indices=posts,comments
To index any entities/documents in your code, you will need to use the SearchService
. You need to pass it the ObjectManager
associated with your objects and the objects to index. Objects can be a single entity or document, an array of entities/documents or even an array of different entities/documents as long as they are using the same ObjectManager
.
$searchService->index($objectManager, $post);
$searchService->index($objectManager, $posts);
$searchService->index($objectManager, $postsAndComments);
You may want to completely clear your indices (before re-indexing for example), you can use the meili:clear
or meili:delete
command.
# Clear all indices
php bin/console meili:clear
# Choose what indices to clear by passing the index name
php bin/console meili:clear --indices=posts,comments
# Remove the index
php bin/console meili:delete --indices=posts,comments
The same way you index data, you can use the remove
method to delete entries from the M eiliSearch index.
$searchService->remove($objectManager, $post);
$searchService->remove($objectManager, $posts);
$searchService->remove($objectManager, $postsAndComments);
By default, the bundle listens to the following Doctrine events: postPersist
, postUpdate
, preRemove
. Every time data are inserted, updated or deleted via Doctrine, your Meilisearch index will stay in sync.
You can easily modify which events the bundle subscribes to via the doctrineSubscribedEvents
config key.
You can unsubscribe from all events by passing an empty array. This can become very handy if you are working with a queue (like RabbitMQ) or if you don't want to call Meilisearch in your dev environment.
# Only insert new data (no update, no deletion)
meilisearch:
doctrineSubscribedEvents: ['postPersist']
# Unsubscribe from all events
meilisearch:
doctrineSubscribedEvents: []
Most of the time, there are some of your items that you don't want to index. For instance, you may want to only index a post if it's published.
In your configuration, you can specify when a post should be indexed via the index_if
key. Because we rely on the PropertyAccess component you can pass a method name, a class property name, or even a nested key in a property array.
The property must evaluate to true
to index the entity and false to bypass indexing. If you’re updating an entity via doctrine and this property evaluates to false, the entity will be removed.
meilisearch:
indices:
- name: posts
class: App\Entity\Post
index_if: isPublished
In this case, isPublished
could be a method or a class property.
With a method:
class Post
{
public function isPublished()
{
return !is_null($this->published_at);
}
}
With a property:
class Post
{
public $isPublished = true;
}
meilisearch:
indices:
- name: posts
class: App\Entity\Post
index_if: config.indexable
In this case, the bundle will read this value.
class Post
{
public $config = ['indexable' => false];
}