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

Please add possibility to use Laravel Scout Builder ->options() method #84

Open
samjurriaans opened this issue Jan 8, 2024 · 0 comments

Comments

@samjurriaans
Copy link

samjurriaans commented Jan 8, 2024

Description

Currently we cannot use the Model::search('query')->options([]) feature, since the typesense engine class does not merge the builder options with the initial options. For example the Laravel scout algolia driver does enable this like this:

protected function performSearch(Builder $builder, array $options = [])
    {
        $algolia = $this->algolia->initIndex(
            $builder->index ?: $builder->model->searchableAs()
        );

        $options = array_merge($builder->options, $options);

        if ($builder->callback) {
            return call_user_func(
                $builder->callback,
                $algolia,
                $builder->query,
                $options
            );
        }

        return $algolia->search($builder->query, $options);
    }

Steps to reproduce

add options to the Builder->options() method and see that these options are not applied in the search query

Expected Behavior

The given options array in the options method on the Laravel Scout Builder class must be applied in the search query to typesense

Suggested Solution

Rewrite the current performSearch method of the Typesense engine class from:

protected function performSearch(Builder $builder, array $options = []): mixed
    {
        $documents = $this->typesense->getCollectionIndex($builder->model)
            ->getDocuments();
        if ($builder->callback) {
            return call_user_func($builder->callback, $documents, $builder->query, $options);
        }
        if(!$this->optionsMulti)
        {
            $documents = $this->typesense->getCollectionIndex($builder->model)
                ->getDocuments();
            if ($builder->callback) {
                return call_user_func($builder->callback, $documents, $builder->query, $options);
            }

            return $documents->search($options);
        } else {
            return $this->typesense->multiSearch(["searches" => $this->optionsMulti], $options);
        }
    }
TO:
protected function performSearch(Builder $builder, array $options = []): mixed
    {
        $options = array_merge($options, $builder->options); // newly added line

        $documents = $this->typesense->getCollectionIndex($builder->model)
            ->getDocuments();
        if ($builder->callback) {
            return call_user_func($builder->callback, $documents, $builder->query, $options);
        }
        if(!$this->optionsMulti)
        {
            $documents = $this->typesense->getCollectionIndex($builder->model)
                ->getDocuments();
            if ($builder->callback) {
                return call_user_func($builder->callback, $documents, $builder->query, $options);
            }

            return $documents->search($options);
        } else {
            return $this->typesense->multiSearch(["searches" => $this->optionsMulti], $options);
        }
    }
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

No branches or pull requests

1 participant