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

[BUG] Possibly unexpected behaviour, when using additional callback in search #250

Open
vgaldikas opened this issue Jun 15, 2023 · 3 comments · May be fixed by #251
Open

[BUG] Possibly unexpected behaviour, when using additional callback in search #250

vgaldikas opened this issue Jun 15, 2023 · 3 comments · May be fixed by #251
Labels
bug Something isn't working

Comments

@vgaldikas
Copy link

Describe the bug

Search breaks when you use callback.

Example: when you I use like this:

$result = Product::search(
            'iphone',
        )->get();

Everything is ok. But when you use search with additional callback, like so:

$result = Product::search(
            null,
            function (
                Client $client,
            ) use ($search) {
                return $client->search(['index' => $this->index->value, 'body' => $search->toArray()]);
            }
        )->get();

I get the following error:

Matchish\ScoutElasticSearch\ElasticSearch\EloquentHitsIteratorAggregate::__construct(): Argument #1 ($results) must be of type array, Elastic\Elasticsearch\Response\Elasticsearch given

To Reproduce
I think bug description is clear enough about how to reproduce

Expected behavior
In both cases it should return a collection with appropriate models

Additional context
Is it possible that I am trying to do this the wrong way?

Version
Versions of Laravel, Scout, and the package.

"matchish/laravel-scout-elasticsearch": "^6.0.2",
Laravel Framework 10.2.0

@vgaldikas vgaldikas added the bug Something isn't working label Jun 15, 2023
@vgaldikas vgaldikas changed the title [BUG] [BUG] Possibly unexpected behaviour, when using additional callback in search Jun 15, 2023
@vgaldikas
Copy link
Author

Ok I think I have found the issue. It seems to stem from here:

https://github.com/matchish/laravel-scout-elasticsearch/blob/master/src/Engines/ElasticSearchEngine.php#L196

When call back is passed, asArray is not called if no callback then asArray is called.

@matchish is there a reason why it is done this way, or is this a bug? If the latter, im happy to make a pull request

@vgaldikas vgaldikas reopened this Jun 15, 2023
@matchish
Copy link
Owner

Hey) feel free to open the pr. But it breaking changes so probably I'll wait until will be merged to master. For now could be solved by calling toArray in callback I think

@vgaldikas
Copy link
Author

@matchish yes, calling asArray in the callback does indeed seem to work as well. But I reckon that it's better that the underlying function returns same thing, regardless of if callback argument was used or not :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants