Elasticsearch guide for Viblo development
How to work with Elasticsearch on Viblo
First, you need to know which .php files contain the code involved in working with search and indexing so that your changes to the search by model are correct.
PostSearchable
,UserSearchable
,CompanySearchable
,TagSearchable
- These classes contain atransform
method that prepares data for indexing in ESSearcher
- The class contains the logic of constructing a query to ES for searching by modelsParser
- The class contains the logic of parsing the search query of the user in the form of a site searchPostSearch
,TagSearch
,UserSearch
,CompanySearch
- Classes-tramsformers are used to construct a response to a user's search query
Secondly, it is necessary to familiarize yourself with the official documentation on the following points in order to have a basic understanding of what is described below.
- Reference to the official documentation of the index management
- Reference to the official documentation of the mapping management
Console Commands
** Attention! ** The configuration files that are used in these commands are located in
private/
path.
private/index.mapping.json
- this file contains config for ES for searching by modelPost
private/search.mapping.json
- this file contains config for ES for searching by modelsUser
,Tag
,Company
Import data from DB to Elasticsearch
- Script path:
app/Console/Commands/ElasticReindex.php
- Command Signature:
es:reindex {model?} {--all} {--chunk=500}
- Parameters:
model
- Classname for importing model--all
- Import all models instead of specific model--chunk
- Batch size in the loop while importing models
- Examples:
- Reindex only
Post
model from DB with defaults parameters
es:reindex Post
- Reindex all models, such as
Post
,User
,Tag
,Company
, with batch size 1000
es:reindex --all --chunk=1000
Import new ES mapping and reindex Models
- Script path:
app/Console/Commands/ElasticImportMapping.php
- Command Signature:
es:importMapping {model=Post} {--force} {--chunk=500}
- Parameters:
model
- Classname for importing model--force
- Drops all data from ES for specific model and forcing importing from DB to avoid out of sync between ES storage and DB--chunk
- Batch size in the loop while importing models
- Examples:
- Re-creation mapping for
Post
model from DB with defaults parameters
es:importMapping Post
- Re-creation of model
User
with forced removal of the index and re-import of data and batch size 1000. Available models for re-creation mapping isPost
,User
,Tag
,Company
es:reindex User --force --chunk=1000
Removing the index from Elasticsearch
- Script path:
app/Console/Commands/ElasticDeleteIndex.php
- Command Signature:
es:delete {index}
- Parameters:
index
- Index name to remove from Elasticsearch
- Examples:
- Index name for
Post
model
es:delete posts
- Index name for
User
,Tag
,Company
will be with_search
suffix
es:delete users_search
es:delete tags_search
es:delete companies_search
Please write to me if you need more information in the document or help. skype: insane.z0rg
All rights reserved