Meilisearch-laravel-scout: Skip searching by ID

Created on 6 Apr 2021  ·  5Comments  ·  Source: meilisearch/meilisearch-laravel-scout

Hello,

When creating toSearchableArray for a specific model, if there isn't ID included in final array, I can't import the data using php artisan scout:import "App\Book". But if I include ID, then it becomes available to search by ID also, which is behaviour I would like to escape. What is the right way to disable searching by ID?

Example code:

    public function toSearchableArray()
    {
        $array = $this->toArray();

        foreach(array_keys($array) as $key) {
            //if I unset ID also, I won't be able to import table data
            if(in_array($key, ['id', 'title', 'name'])) continue;
            unset($array[$key]);
        }

        return $array;
    }

Most helpful comment

Hey @milosevicn,

the ->toSearchableArray() defines the stored data within MeiliSearch for a specific document (object). Omitting the id will result in non-discoverable elements within the search because by default the id is the primary key used for identifing a document.

To prevent MeiliSearch from searching through all fields, consider extending the functionallity of this package and setting the searchAttributes within MeiliSearch. You can find a tutorial to do this here.

All 5 comments

Hey @milosevicn,

the ->toSearchableArray() defines the stored data within MeiliSearch for a specific document (object). Omitting the id will result in non-discoverable elements within the search because by default the id is the primary key used for identifing a document.

To prevent MeiliSearch from searching through all fields, consider extending the functionallity of this package and setting the searchAttributes within MeiliSearch. You can find a tutorial to do this here.

@mmachatschek Sorry to disturb you, but I didn't want to open new issue for this. It might be related to my question up there.
When I'm adding documents using php artisan scout:import command, everything works as it's supposed to, but when I am doing the same using /indexes/:index_uid/documents route (->addDocuments()) it does not respect toSearchableArray() rules. So if I try adding the whole model, all columns are going to be added.
I would probably be able overcoming this by selecting specific columns while doing query, but is there a way to make the function addDocuments() itself be aware of toSearchableArray()?

Hi @milosevicn,

this package basically is a wrapper for the MeiliSearch PHP library and extends Laravel Scout.

When adding documents "manually", all the magic that Scout provides e.g. adding new objects to the index or updating existing ones with specific fields etc. gets lost because you skip that steps.

Is there a specific intention you have by adding documents manually?

Thanks for replying @mmachatschek

I have multitenancy app - two separate apps (landlord and tenant). Because of this, I am not able to use predefined Laravel Scout functionality to update docs automatically once something is added to database. That's why I'm using a webhook to inform my tenant app when something happened in database actually on landlord (on created/updated/deleted). I want to add those docs once I catch a webhook on tenant

@milosevicn I think you could just do a $model->touch(). This updates the updated_at column of the model and the updated event will be triggered on the tenant app. The Scout event listener will then trigger a update on the index. Would this work out for you?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Cannonb4ll picture Cannonb4ll  ·  6Comments

dreamcog picture dreamcog  ·  14Comments

Kladislav picture Kladislav  ·  8Comments

desaintflorent picture desaintflorent  ·  8Comments

husonghua picture husonghua  ·  5Comments