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 54 forks source link

[STU-167] Undefined array key error in SearchableBehavior.php when replicaIndex is true #303

Closed mike-moreau closed 3 months ago

mike-moreau commented 5 months ago

I am running Scout 4.1.0 on Craft 4.9.7 and configuring a Replica Index.

My /config/scout.php file looks like this:

<?php

use craft\helpers\App;
use craft\elements\Entry;
use rias\scout\ScoutIndex;
use rias\scout\IndexSettings;
use craft\elements\db\EntryQuery;
use Modules\Serialization\Transformer;

function createSettings(): IndexSettings
{
    $settings = IndexSettings::create()
        // ...settings methods...
        ->alternativesAsExact(ALTERNATIVES_AS_EXACT);

    return $settings;
}

return [
    'indices' => [
        ScoutIndex::create('Site (English)')
            ->elementType(Entry::class)
            ->transformer([Transformer::class, 'scout'])
            ->criteria(fn (EntryQuery $query) => $query->site('default')->status('live'))
            ->indexSettings(
                createSettings()
                    ->replicas(['virtual(Site (English) (Sorted))'])
            ),
        ScoutIndex::create('Site (English) (Sorted)')
            ->replicaIndex(true)
            // ->criteria(fn (EntryQuery $query) => $query->id(0))
            ->indexSettings(createSettings(true)),
        ScoutIndex::create('Site (Spanish)')
            ->elementType(Entry::class)
            ->transformer([Transformer::class, 'scout'])
            ->criteria(fn (EntryQuery $query) => $query->site('spanish')->status('live'))
            ->indexSettings(
                createSettings()
                    ->replicas(['virtual(Site (Spanish) (Sorted))'])
            ),
        ScoutIndex::create('Site (Spanish) (Sorted)')
            ->replicaIndex(true)
            // ->criteria(fn (EntryQuery $query) => $query->id(0))
            ->indexSettings(createSettings(true)),
    ],
];

Based off this merged PR and the documentation, I expected the Replica Indexes to not require criteria, like this example:

        ScoutIndex::create('Site (English) (Sorted)')
            ->replicaIndex(true)
            ->indexSettings(createSettings(true)),

However, I get the following error when saving an entry:

PHP Warning – [yii\base\ErrorException](https://www.yiiframework.com/doc-2.0/yii-base-errorexception.html)
Undefined array key 0
In .../vendor/studioespresso/craft-scout/src/behaviors/SearchableBehavior.php

Line 88 && ($criteriaSiteIds[0] === '*' || in_array((int)$this->owner->siteId, $sit
Screenshot 2024-06-12 at 7 42 22 AM

If I add criteria to the Replica Indexes config, the error goes away. I'm using $query->id(0) to ensure an empty Element Query, but would prefer not to have any criteria at all.

        ScoutIndex::create('Site (English) (Sorted)')
            ->replicaIndex(true)
            ->criteria(fn (EntryQuery $query) => $query->id(0))
            ->indexSettings(createSettings(true)),

Am I misunderstanding the documentation for Replica Indexes or is this a bug perhaps?

STU-167

mike-moreau commented 5 months ago

I added a PR for this on Craft 4. https://github.com/studioespresso/craft-scout/pull/304

Not sure if the same issue exists in Craft 5.

janhenckens commented 4 months ago

Fixed in 4.1.1, will leave this open to make sure I don't forget to the Craft 5 branch

janhenckens commented 3 months ago

Out for Craft 5 in 5.0.1.