laravel-scout-typesense-driver icon indicating copy to clipboard operation
laravel-scout-typesense-driver copied to clipboard

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

Open samjurriaans opened this issue 1 year ago • 0 comments
trafficstars

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);
        }
    }

samjurriaans avatar Jan 08 '24 13:01 samjurriaans