Skip to content

MeiliSearchEngine does not work with custom Scout keys #653

@DarronEngelbrechtEdge

Description

@DarronEngelbrechtEdge
  • Scout Version: 9.4.10
  • Scout Driver: MeiliSearch
  • Laravel Version: 9.21.6
  • PHP Version: 8.1.9
  • Database Driver & Version: MySQL 8.0
  • SDK Version (If using a third-party service): N/A
  • Meilisearch CLI Version (If using Meilisearch): meilisearch-http 0.28.0

Description:

When using custom index keys with getScoutKey() in a Searchable model the MeiliSearchEngine does not correct add the custom key to index nor delete entries from the index.

  1. The update() method in the MeiliSearchEngine class uses array_merge() to merge the Scout key index with the toSearchableArray() data but references the Scout key first so the custom key gets overwritten by $searchableData unless a custom key is used.
           return array_merge(
               // [$model->getKeyName() => $model->getScoutKey()],
                $searchableData,
                $model->scoutMetadata(),
                [$model->getKeyName() => $model->getScoutKey()]
            );
  1. RemoveFromSearch job makes a RemoveableScoutCollection which runs the models through getScoutKey() and passes the result to the the delete() method in MeiliSearchEngine which then runs getScountKey() a second time.
    ie.
    /**
     * Remove the given model from the index.
     *
     * @param  \Illuminate\Database\Eloquent\Collection  $models
     * @return void
     */
    public function delete($models)
    {
        $index = $this->meilisearch->index($models->first()->searchableAs());

        $index->deleteDocuments(
            // $models->map->getScoutKey()
            //     ->values()
            //     ->all()
            $models->pluck($models->first()->getKeyName())->toArray()
        );
    }

PS. We may also want to look at replacing getKeyName() with getScoutKeyName() in this MeiliSearchEngine class?

Steps To Reproduce:

  1. Create a searchable model which overrides the getScoutKey method to prepend a something...
    /**
     * Get the custom search index key
     *
     * @return void
     */
    public function getScoutKey()
    {
        return 1 . $this->getKey();
    }
  1. Create and ->save() the model, notice in Meilisearch the ID is not prepended with 1.

  2. Delete the model, notice the model is not removed from Meilisearch.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions