laravel / scout

Laravel Scout provides a driver based solution to searching your Eloquent models.
https://laravel.com/docs/scout
MIT License
1.55k stars 331 forks source link

pass additional parameters on search method #198

Closed marksparrish closed 6 years ago

marksparrish commented 7 years ago

I would like to be able to pass an additional parameter on search method.

Currently you only pass the string you want to search for - $orders = App\Order::search('Star Trek')->get(); However it would be nice to pass an additional parameter after the string -

$params = [
    '_source' => ['person_id', 'physical_address'],
    'body' => [
        'query' => [
            'multi_match' => [
                'query' => $query,
                'type' => 'cross_fields',
                'operator' => 'and',
                'fields' => ['physical_address'],

            ],
        ]
    ]
];

$orders = App\Order::search('Star Trek', $params)->get();
marksparrish commented 7 years ago

Looks like this may work better - https://stackoverflow.com/questions/39541603/how-can-you-set-boosts-and-filters-with-laravel-scout-and-elasticsearch.

Create your own by forking/extending the current driver.

trideout commented 7 years ago

+1 here. Can't use Scout because of the very limited where system, I'd love to be able to set a much greater number of filters.

julienbourdeau commented 7 years ago

You can achieve this with the callback. I have checked for Elastic, but it should be similar to what we do with Algolia:

$moreOptions = [....];

Model::search($query, function ($algolia, $query, $options) use ($moreOptions) {

    $options = array_merge($options, $moreOptions);

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

If your Scout is up to date, you can create a macro to keep it simple. You can find an example here.

Although, I agree it would be great to have a with method to do it easily.

Model::search($q)->with($extra)->get();
trideout commented 7 years ago

@julienbourdeau that will work great for the time being, but I would love to see the filters get more functionality like the query statements in Eloquent. Mapping an array to $key.'='.$value seems lazy.

driesvints commented 6 years ago

You could now use https://github.com/algolia/scout-extended which has this functionality.