0

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 a transform method that prepares data for indexing in ES
  • Searcher - The class contains the logic of constructing a query to ES for searching by models
  • Parser - The class contains the logic of parsing the search query of the user in the form of a site search
  • PostSearch, 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.

  1. Reference to the official documentation of the index management
  2. 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.

  1. private/index.mapping.json - this file contains config for ES for searching by model Post
  2. private/search.mapping.json - this file contains config for ES for searching by models User, 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:
  1. Reindex only Post model from DB with defaults parameters
es:reindex Post
  1. 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:
  1. Re-creation mapping for Post model from DB with defaults parameters
es:importMapping Post
  1. 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 is Post, 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:
  1. Index name for Post model
es:delete posts
  1. 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

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí