|
1 |
| -# meilisearch |
2 |
| - |
| 1 | +# Meilisearch Connect |
3 | 2 |
|
| 3 | +Meilisearch Connect enables seamless data synchronization between Craft CMS and Meilisearch, allowing your Craft entries |
| 4 | +and custom fields to be indexed and searched efficiently. |
4 | 5 |
|
5 | 6 | ## Requirements
|
6 | 7 |
|
@@ -28,3 +29,97 @@ composer require fostercommerce/craft-meilisearch
|
28 | 29 | # tell Craft to install the plugin
|
29 | 30 | ./craft plugin/install meilisearch
|
30 | 31 | ```
|
| 32 | + |
| 33 | +## Configuration |
| 34 | + |
| 35 | +### With a meilisearch-connect.php config file |
| 36 | + |
| 37 | +The easiest way to configure the plugin using the config file is to use the `IndexBuilder` and `IndexSettingsBuilder` |
| 38 | +utility classes. |
| 39 | + |
| 40 | +Take a look at the example [config.php](src/config.php) file. |
| 41 | + |
| 42 | +## Usage |
| 43 | + |
| 44 | +### Ensuring data is up-to-date |
| 45 | + |
| 46 | +There are two recommended ways to keep data in Meilisearch indices up-to-date with your data in Craft: |
| 47 | + |
| 48 | +- Running the sync/all or sync/index commands on a schedule, for example, via crontab, or |
| 49 | +- If you're indexing Craft Elements or Entries, updating the data for that item using a save or delete event. |
| 50 | + |
| 51 | +#### `Element::EVENT_AFTER_SAVE` |
| 52 | + |
| 53 | +```php |
| 54 | +Event::on( |
| 55 | + Product::class, |
| 56 | + Element::EVENT_AFTER_SAVE, |
| 57 | + static function (\craft\events\ModelEvent $event) { |
| 58 | + if ( |
| 59 | + ! ElementHelper::isDraft($event->sender) && |
| 60 | + ! $event->sender->resaving && |
| 61 | + ! ElementHelper::isRevision($event->sender) |
| 62 | + ) { |
| 63 | + $item = $event->sender; |
| 64 | + $status = $item->getStatus(); |
| 65 | + if ($status === Entry::STATUS_LIVE) { |
| 66 | + // If an entry is live, then we can add it to the index |
| 67 | + Queue::push(new SyncJob([ |
| 68 | + 'indexName' => 'pages', |
| 69 | + 'identifier' => $item->id, |
| 70 | + ])); |
| 71 | + } else { |
| 72 | + // Otherwise, we should make sure that it is not in the index |
| 73 | + Queue::push(new DeleteJob([ |
| 74 | + 'indexName' => 'pages', |
| 75 | + 'identifier' => $item->id, |
| 76 | + ])); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +); |
| 81 | +``` |
| 82 | + |
| 83 | +#### `Element::EVENT_AFTER_DELETE` |
| 84 | + |
| 85 | +```php |
| 86 | +Event::on( |
| 87 | + Product::class, |
| 88 | + Element::EVENT_AFTER_DELETE, |
| 89 | + static function (\craft\events\Event $event) { |
| 90 | + $item = $event->sender; |
| 91 | + Queue::push(new DeleteJob([ |
| 92 | + 'identifier' => $item->id, |
| 93 | + ])); |
| 94 | + } |
| 95 | +); |
| 96 | +``` |
| 97 | + |
| 98 | +## Console Commands |
| 99 | + |
| 100 | +### `meilisearch-connect/sync/settings` |
| 101 | + |
| 102 | +Synchronize settings for all indices. |
| 103 | + |
| 104 | +**Note** that this should be run whenever the `settings` key for an index changes. Running this after a deployment is |
| 105 | +usually a good idea. |
| 106 | + |
| 107 | +### `meilisearch-connect/sync/index <index name>` |
| 108 | + |
| 109 | +Synchronize data for the given index |
| 110 | + |
| 111 | +### `meilisearch-connect/sync/all (default)` |
| 112 | + |
| 113 | +Synchronizes data to all indices. |
| 114 | + |
| 115 | +### `meilisearch-connect/sync/flush <index name>` |
| 116 | + |
| 117 | +Flushes data for the given index. |
| 118 | + |
| 119 | +### `meilisearch-connect/sync/flush-all` |
| 120 | + |
| 121 | +Flushes data for all indices. |
| 122 | + |
| 123 | +### `meilisearch-connect/sync/refresh-all` |
| 124 | + |
| 125 | +Flush and synchronize data for all indices. |
0 commit comments