Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mike-moreau opened this issue Jun 12, 2024 · 3 comments
Assignees
Labels
Craft 4 linear Created by Linear-GitHub Sync

Comments

@mike-moreau
Copy link

mike-moreau commented Jun 12, 2024

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
Copy link
Author

I added a PR for this on Craft 4. #304

Not sure if the same issue exists in Craft 5.

janhenckens added a commit that referenced this issue Jul 15, 2024
@janhenckens janhenckens self-assigned this Jul 15, 2024
@janhenckens
Copy link
Member

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

@janhenckens janhenckens added linear Created by Linear-GitHub Sync and removed pending release labels Jul 15, 2024
@janhenckens janhenckens changed the title Undefined array key error in SearchableBehavior.php when replicaIndex is true [STU-167] Undefined array key error in SearchableBehavior.php when replicaIndex is true Jul 15, 2024
@janhenckens
Copy link
Member

Out for Craft 5 in 5.0.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Craft 4 linear Created by Linear-GitHub Sync
Projects
None yet
Development

No branches or pull requests

2 participants