Sti3bas / laravel-scout-array-driver

Array driver for Laravel Scout
MIT License
89 stars 8 forks source link

Implement support for Array properties #8

Closed arondeparon closed 2 years ago

arondeparon commented 2 years ago

What does this pull request fix?

Laravel Scout has built-in support for array properties, but the array driver (ironically 😝 ) does not support searching properties with array values.

This pull request implements support for simple, flat arrays and will allow you to index and search models like this:

public function toSearchableArray(): array
{
    return [
        'id' => $this->id,
        'categories' => [5, 6, 7]
    ];
}

Searching:

$model->search()->where('category', 6);
Sti3bas commented 2 years ago

@ArondeParon hmm, what is your use case? Maybe you could share your real world example?

arondeparon commented 2 years ago

@Sti3bas my use case is similar to the description of this issue: I have certain fields that contain more than one property value. For example, a product that can have more than one category attached to it.

ElasticSearch natively handles array properties by searching each value, but the array driver does not seem to support this.

Sti3bas commented 2 years ago

@ArondeParon it seems like your test depends on search implementation of the driver, if I understand you correctly. I think you should avoid that, because all these (ElasticSearch, MeiliSearch, Algolia etc.) drivers might return different results for the same query. You should only focus on testing your app's logic and all search results should be mocked.

Array driver was never intended to replicate search functionality of other drivers.

arondeparon commented 2 years ago

Fair enough. I have switched to integration testing to test my logic, so this use case is no longer required for me.

I am still a bit in doubt though; afaik, this is "default" Scout behavior which should be supported by any implementing drivers.