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

Scout lazyMap method not implemented, returns no results #298

Closed
bakerkretzmar opened this issue Jan 29, 2022 · 1 comment · Fixed by #307
Closed

Scout lazyMap method not implemented, returns no results #298

bakerkretzmar opened this issue Jan 29, 2022 · 1 comment · Fixed by #307

Comments

@bakerkretzmar
Copy link
Contributor

  • Laravel version: 8.81.0
  • Algolia Scout Extended version: 2.0.0
  • Algolia Client Version: 3.2.0
  • Language Version: 8.1.2

Description

Laravel Scout has a companion method to map, lazyMap, which uses a generator under the hood to keep memory usage low. That lazyMap method doesn't play nicely with Scout Extended, in basically the same way as the underlying issue in #279—it uses Algolia's objectID to retrieve models, which it expects to be the actual model key, but which with Scout Extended installed is actually something like App\Event::20.

The map method does this too, but Scout Extended overrides it and accounts for the primary key thing inside the ModelsResolver class.

I can't switch to using the map method because the code calling lazyMap is inside Laravel Nova, in the global search code. I'll file an issue there too.

Ideally Scout Extended would also override lazyMap, with functionality similar to map but returning a generator or lazy collection if possible. I'll try to take a crack at a PR for that in the next week or two.

In the meantime, for anyone else running into this, I managed to work around it by overriding the Laravel\Nova\Query\Builder class:

// In the `register` method of `app/Providers/NovaServiceProvider.php`

use App\Nova\Builder;
use Laravel\Nova\Contracts\QueryBuilder;

$this->app->bind(QueryBuilder::class, fn ($app, $parameters) => new Builder(...$parameters));

// `app/Nova/Builder.php`

use Illuminate\Support\LazyCollection;
use Laravel\Nova\Query\Builder as Nova;

class Builder extends Nova
{
    public function cursor()
    {
        $queryBuilder = $this->applyQueryCallbacks($this->queryBuilder);

        // Remove or comment out this part
        // if (method_exists($queryBuilder, 'cursor')) {
        //     return $queryBuilder->cursor();
        // }

        return LazyCollection::make(function () use ($queryBuilder) {
            yield from $queryBuilder->get()
                ->each(function ($result) {
                    yield $result;
                });
        });
    }
}
@DevinCodes
Copy link
Contributor

Hi @bakerkretzmar ,

Thank you for reporting this. Feel free to assign me to your PR once it's open so I can review and release 🙂

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants