Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Elasticsearch driver for Laravel Scout

License

NotificationsYou must be signed in to change notification settings

thomasjsn/laravel-scout-elastic

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Software LicenseTravisPackagist

This package provides anElasticsearch driver for Laravel Scout.

Contents

Installation

You can install the package via composer:

composer require thomasjsn/laravel-scout-elastic

You must add the Scout service provider and the package service provider in your app.php config:

// config/app.php'providers' => [    ...Laravel\Scout\ScoutServiceProvider::class,    ...ScoutEngines\Elasticsearch\ElasticsearchProvider::class,],

Then you should publish the Elasticsearch configuration using thevendor:publish Artisan command.

php artisan vendor:publish --provider="ScoutEngines\Elasticsearch\ElasticsearchProvider"

Setting up Elasticsearch configuration

You must have a Elasticsearch server up and running, indices can be created with an Artisan command; see below.

After you've published the Laravel Scout package configuration:

// config/scout.php// Set your driver to elasticsearch'driver' =>env('SCOUT_DRIVER','elasticsearch'),

Usage

Creating Elasticsearch indexes with proper mapping

You may define custom mappings for Elasticsearch fields in the config. See examples in theconfig file.If you prefer storing mappings in models, you may create a static public methodmapping() in each particular model:

class Articleextends Model{// ...publicstaticfunctionmapping() {return ['location' => ['type' =>'geo_point'            ],        ];    }// ...}

And then use it in the config file:

'indices' => ['realestate' => ['settings' => ["number_of_shards" =>1,"number_of_replicas" =>0,        ],'mappings' => ['articles' => \App\Article::mapping(),        ],    ], ]

The document type, in this examplearticles must matchsearchableAs() for the respective model.

Elasticsearch can set default types to model fields on the first insert if you do not explicitly define them.However; sometimes the defaults are not what you're looking for, or you need to define additional mapping properties.

In that case, we strongly recommend creating indices with proper mappings before inserting any data.For that purpose, there is an Artisan command, calledelastic:make-indices {index} which creates an index based onthe settings in your configuration file.

To create all indices from your config just ignore the{index} parameter and run:

php artisan elastic:make-indices

If the index exists you will be asked if you want to delete and recreate it, or you can use the--force flag.

To get information about your existing Elasticsearch indices you may want to use the following command:

php artisan elastic:indices

Indexing data

You may follow instructions from theofficial Laravel Scout documentationto index your data.

Search

The package supports everything that is provided by Laravel Scout.

The Scoutsearch method used the default query method defined in the config file.

Sorting withorderBy() method:

$articles = Article::search($keywords)            ->orderBy('id','desc')            ->get();

Elastic specific

However, to use the extra Elasticsearch features included in this package, use traitElasticSearchableby adding it to your model instead ofSearchable:

class Articleextends Model{// use Searchable;use ElasticSearchable;// ...}

The package features:

  1. TheelasticSearch method,elasticSearch($method, $query, array $params = null):
$articles = Article::elasticSearch('multi_match',$q, ['fields' => ['title','content','tags'],'fuzziness' =>'auto','prefix_length' =>2,'operator' =>'AND'])->get();

Parameters are taken from the configuration, for the specific query method, if not supplied. But you may override them.

  1. A separate Elasticsearch index for each model.

If you have defined several indices in yourconfig file,you may choose which index a model belongs to by overridingsearchableWithin() method in your model:

publicfunctionsearchableWithin(){return'foobar';}

If you do not overridesearchableWithin() in your model, the first index from the config will be used.

Credits

License

The MIT License (MIT).

About

Elasticsearch driver for Laravel Scout

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP100.0%

[8]ページ先頭

©2009-2025 Movatter.jp