meilisearch / meilisearch-laravel-scout

MeiliSearch integration for Laravel Scout
https://github.com/meilisearch/MeiliSearch
MIT License
465 stars 43 forks source link

Cannot use object of type MeiliSearch\Search\SearchResult as array #87

Closed francoism90 closed 3 years ago

francoism90 commented 3 years ago

I'm trying to add a custom search as seen in the example:

return Book::search('prince', function (Indexes $meilisearch, $query, $options) {
            $options['limit'] = 10000;

            return $meilisearch->search($query, $options);
        })->get();

This always returns the following error:

[2021-01-29 13:53:27] local.ERROR: Cannot use object of type MeiliSearch\Search\SearchResult as array {"userId":1,"exception":"[object] (Error(code: 0): Cannot use object of type MeiliSearch\\Search\\SearchResult as array at /srv/http/project/api/vendor/meilisearch/meilisearch-laravel-scout/src/Engines/MeilisearchEngine.php:175)

I must be doing something wrong, does it expect to have filters set?

Thanks!

olivM commented 3 years ago

i had the exact same problem this morning i submitted a PR for it : https://github.com/meilisearch/meilisearch-laravel-scout/pull/86

mmachatschek commented 3 years ago

@francoism90 this happens when you call return $meilisearch->search($query, $options); in your custom search query.

If you call Model::search()->get() directly, the MeilisearchEngine::class calls the ->rawSearch() method and therefore returns an array instead of an SearchResult object. So the fix for this is calling:

return Book::search('prince', function (Indexes $meilisearch, $query, $options) {
            $options['limit'] = 10000;

            return $meilisearch->rawSearch($query, $options);
        })->get();
shokme commented 3 years ago

Hello guys, Indeed for custom search you should use ->rawSearch() I will update the doc accordingly. this package work with array a recent change to the php sdk use a SeachResult class and I don't think I will change the behaviour soon.

francoism90 commented 3 years ago

@mmachatschek @shokme Thanks! This fixed the issue. :)

Just on question: is it possible to build an query using ->orWhere? I would to search for multiple terms and receive the data using one query.

// Table: 
{ name: 'Foo' },
{ name: 'Foo Bar' },
{ name: 'Foo 2'},
{ name: 'Bar 2'},
..

// Results:
return Book::search('prince', function (Indexes $meilisearch, $query, $options) {
            $options['limit'] = 10000;
            $options['filters'] = "name = 'foo' OR name = 'bar'";

            return $meilisearch->rawSearch('', $options);
        })->get();

This doesn't seem to return any documents, is this normal? Should you use multiple queries instead?

mmachatschek commented 3 years ago

@francoism90 as far as I can tell, there is no such feature neither in laravel/scout nor in this package. This would be something that should be provided via a macro in your app for your specific usecase

curquiza commented 3 years ago

Hello everyone! Thanks @mmachatschek for your help on this 😄 And I hope @francoism90 and @olivM this fix will solve your issue, I'm going to do a release right now, let me know!

curquiza commented 3 years ago

the v0.12.3 is out on Packagist! https://packagist.org/packages/meilisearch/meilisearch-laravel-scout