Download the PHP package god-jay/scout-elasticsearch without Composer
On this page you can find all versions of the php package god-jay/scout-elasticsearch. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package scout-elasticsearch
god-jay scout-elasticsearch
Use elasticsearch as easy as using Eloquent ORM in your laravel application.
English | 简体中文
Contents
- Installation
- Configuration
- Usage
- Create elasticsearch index
- Import the given model into the search index
- Flush all of the model's records from the index
- Adding Records
- Updating Records
- Removing Records
- Searching
- Advanced Usage
Installation
You can install the package via composer:
After installing the package, you should publish the Scout configuration using the vendor:publish Artisan command. This command will publish the scout.php configuration file to your config directory:
Then add
Docker compose run es + kibana
If you don't have your own es service, you may install and run es + kibana with docker compose:
-
You should install docker compose first: install docker compose
-
Then run the command in the root of this directory:
-
You can browse
http://localhost:5601
to visit kibana. - To stop docker containers, run the command in the root of this directory:
in your .env file.
Configuration
Assuming there is a posts
table and a Post Model, the simplified table may looks like:
id | title | content | created_at |
---|---|---|---|
1 | 标题 | 文本内容 | 2020-01-01 01:01:01 |
Use GodJay\ScoutElasticsearch\Searchable in your model:
Add searchableAs function in the model:
Usage
Create elasticsearch index
Add getElasticMapping function in the model,
then run php artisan elastic:create-index "App\Models\Post"
For more details, see Create index API
The elasticsearch index will be like:
Import the given model into the search index
If there already exist many rows in your table, and you want to import the rows to elasticsearch,
Add toSearchableArray function in the model, then run php artisan scout:import "App\Models\Post"
After import the rows from table above, the elasticsearch index will be like:
Flush all of the model's records from the index
Run php artisan scout:flush "App\Models\Post"
Adding Records
Once you have added the Searchable trait to a model, all you need to do is save a model instance and it will automatically be added to your search index.
Updating Records
To update a searchable model, you only need to update the model instance's properties and save the model to your database.
Removing Records
To remove a record from your index, delete the model from the database. This form of removal is even compatible with soft deleted models:
Searching
Base:
Paginate:
Highlight:
The search result will be:
Advanced Usage
ES script sort:
Debug:
The result will be:
The json string of $debug['query_params']
will be: