Skip to content

[Feature] Add scout builder variable on creating HitsIteratorAggregate #234

Open
@krio-rogue

Description

@krio-rogue

Thank you very much for your job.
Your package is very interesting and functional.
The ability to store data on different models similar in structure in one index due to the "__class_name" attribute is very useful. At the same time, this function changes the standard functionality of the scout package and will not be able to work with an already created database.

Is your feature request related to a problem? Please describe.
When testing different packages for scout + elastic without any callback and additional functions of the package, I expected to get the same search results

Describe the solution you'd like
Search result will contain models instance of target search model (not imported model).

Describe alternatives you've considered
In first i try use custom binding for HitsIteratorAggregate

class CustomIteratorAggregate implements IteratorAggregate
{
    protected static $builder;

    public static function setBuilder(Builder $builder)
    {
        static::$builder = $builder;
        
        return $builder;
    }

    public static function getBuilder()
    {
        return static::$builder;
    }

    public function getIterator(): Traversable
    {
        $builder = static::getBuilder();
        //....
        $hits = $builder->model->getScoutModelsByIds($builder, $objectIds);
        //....
    }
}
//using
$query = CustomIteratorAggregate::setBuilder(Products::search('car'));

But this is not a very good solution, because it is necessary to specify the model class each time and you cannot make requests to the search engine until the previous request is completed.

Perhaps this method will be more useful and will not break compatibility.

final class ElasticSearchEngine extends Engine
{
    //.....
    public function map(BaseBuilder $builder, $results, $model)
    {
        $hits = app()->makeWith(
            HitsIteratorAggregate::class,
            [
                'results'  => $results,
                'callback' => $builder->queryCallback,
                'builder' => $builder,
            ]
        );

        return new Collection($hits);
    }
//.....
class CustomIteratorAggregate implements IteratorAggregate
{
    //.....
    /**
     * @var Builder|null
     */
    protected $builder;

    public function __construct(array $results, callable $callback = null, Builder $builder = null)
    {
        $this->results = $results;
        $this->callback = $callback;
        $this->builder = $builder;
    }

    public function getIterator(): Traversable
    {
        //....
        $hits = $this->builder->model->getScoutModelsByIds($this->builder, $objectIds);
        //....
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions