How can you customize boost and filters with Laravel Scout and ElasticSearch?

I am building search integration with Laravel Scout and Elasticsearch. I am trying to figure out how I can refine my queries for promotion.

Is it possible to do this with Laravel Scout or do I need to return to using the PHP ElasticSearch library directly?

+5
source share
2 answers

Laravel Scout does not support the advanced features of Elasticsearch. I ended up using Scout to create and update an index based on model events, but I switched my search to using the ElasticSearch library directly.

+1
source

In fact, this can simply be done using a special Scout Engine.

Let us call it ElasticqueryEngine, for example, and extend it by default with ElasticsearchEngine:

<?php namespace App\Libs\Scout\Engines; use Laravel\Scout\Builder; use Laravel\Scout\Engines\ElasticsearchEngine; class ElasticqueryEngine extends ElasticsearchEngine { /** * Perform the given search on the engine. * * @param Builder $query * @param array $options * @return mixed */ protected function performSearch(Builder $query, array $options = []) { if (!is_array($query->query)) { return parent::performSearch($query, $options); } $searchQuery = [ 'index' => $this->index, 'type' => $query->model->searchableAs(), 'body' => [ 'query' => $query->query, ], ]; if (array_key_exists('size', $options)) { $searchQuery = array_merge($searchQuery, [ 'size' => $options['size'], ]); } if (array_key_exists('from', $options)) { $searchQuery = array_merge($searchQuery, [ 'from' => $options['from'], ]); } return $this->elasticsearch->search($searchQuery); } } 

Add a new service provider to register a new ElasticqueryEngine (or do this with any of the existing service providers):

 <?php namespace App\Providers; use Laravel\Scout\EngineManager; use Illuminate\Support\ServiceProvider; use Elasticsearch\ClientBuilder as Elasticsearch; use App\Libs\Scout\Engines\ElasticqueryEngine; class ElasticqueryServiceProvider extends ServiceProvider { /** * Perform post-registration booting of services. * * @return void */ public function boot() { resolve(EngineManager::class)->extend('elasticquery', function () { return new ElasticqueryEngine( Elasticsearch::fromConfig(config('scout.elasticsearch.config')), config('scout.elasticsearch.index') ); }); } /** * Register bindings in the container. * * @return void */ public function register() { // } } 

Remember to add a new service provider in config / app.php:

 'providers' => [ // ... Laravel\Scout\ScoutServiceProvider::class, App\Providers\ElasticqueryServiceProvider::class, ], 

And change the driver to "elastic stuff" in config / scout.php or .env (SCOUT_DRIVER = elastic)

In the end, you can search for any queries from https://www.elastic.co/guide/en/elasticsearch/reference/current/full-text-queries.html :

 $query = [ 'simple_query_string' => [ 'query' => 'findme', 'fields' => [ 'title^5', 'description', ], ], ]; $posts = Posts::search($query)->get(); // also you can use default ElasticsearchEngine query $posts = Posts::search('findme')->where('user_id', 1)->get(); 
+3
source

All Articles