studioespresso / craft-scout

Craft Scout provides a simple solution for adding full-text search to your entries. Scout will automatically keep your search indexes in sync with your entries.
MIT License
81 stars 55 forks source link

[STU-58] Indexing jobs sent to queue runner only run from primary site #266

Closed benlobach closed 10 months ago

benlobach commented 1 year ago

Hello!

We've encountered an bug when using multi-site in CraftCMS 4 where entries enter the queue when they are saved, but they don't make it into Algolia. Disabling the queue and/or pushing the sync through the utilities works perfectly. Digging into the jobs code, we found an element query that doesn't include all sites, and that often keeps other sites from indexing when using the queue runner.

https://github.com/studioespresso/craft-scout/blob/16f13633b62fe1f11f558af4bc20f2841756c209/src/jobs/IndexElement.php#L18C7-L18C7

Changing this line as follows appears to correct the issue: $element = Craft::$app->getElements()->getElementById($this->id, NULL, "*"); // Parameters: ID, Type, and Site ID

https://docs.craftcms.com/api/v4/craft-services-elements.html#method-getelementbyid

Thanks for taking a look, we truly appreciate the work you're putting into this plugin!

CraftCMS Version: 4.4.17 Scout Version: 3.1.2

STU-58

janhenckens commented 1 year ago

Hey @benlobach, thanks for reporting this. A colleague of mine reported a similar issue in #270 (where the indexing job doesn't fire when an item is save from a non-primary site).

Changing the getElementById code doesn't seem to fix it on my end - but I don't specify any site in my Scout configuration. Could you share the criteria you have in scout.php? Thanks!

benlobach commented 1 year ago

Hey @janhenckens, thanks for looking into it! We are specifying the site in both locations, and it appears to be working with this configuration along with the getElementbyId change.

->criteria(function (\craft\elements\db\EntryQuery $query) { return $query->siteId(4)->type(['articles']); })

janhenckens commented 1 year ago

hey @benlobach , a fix for this is out in 3.1.3. Can you give that a try? Thanks!

benlobach commented 1 year ago

Thank you, @janhenckens!

mathg commented 11 months ago

On my end I still have this issue. When saving an Entry not in a primary site, the after save hook for indexing gets triggered but it only indexes the Entry from the primary site. The relations are saved correctly in all sites.

The Refresh buttons index them properly.

Edit: If I change the 'queue' param to false in config/scout.php it works properly. It looks like the queue is ran from the primary site and doesn't load the element properly when indexing the element.

janhenckens commented 11 months ago

@mathg Thanks for adding that, I'll try to reproduce and get back to you over the weekend!

janhenckens commented 11 months ago

Hey all, I just released an update that includes a fix for this. Could you update and give it a try? Thanks!

mathg commented 11 months ago

@janhenckens it works properly on my end now, thanks!