- ElasticSearch Cookbook
- Alberto Paro
- 591字
- 2021-04-02 10:09:55
Managing your data
Unless you are using ElasticSearch as a search engine or a distributed data store, it's important to understand concepts on how ElasticSearch stores and manages your data.
Getting ready
To work with ElasticSearch data, a user must know basic concepts of data management and JSON that is the "lingua franca" for working with ElasticSearch data and services.
How it works...
Our main data container is called index (plural indices) and it can be considered as a database in the traditional SQL world. In an index, the data is grouped in data types called mappings in ElasticSearch. A mapping describes how the records are composed (called fields).
Every record, that must be stored in ElasticSearch, must be a JSON object.
Natively, ElasticSearch is a schema-less datastore. When you put records in it, during insert it processes the records, splits them into fields, and updates the schema to manage the inserted data.
To manage huge volumes of records, ElasticSearch uses the common approach to split an index into many shards so that they can be spread on several nodes. The shard management is transparent in usage—all the common record operations are managed automatically in the ElasticSearch application layer.
Every record is stored in only one shard. The sharding algorithm is based on record ID, so many operations that require loading and changing of records can be achieved without hitting all the shards.
The following schema compares ElasticSearch structure with SQL and MongoDB ones:

There's more...
ElasticSearch, internally, has rigid rules about how to execute operations to ensure safe operations on index/mapping/records. In ElasticSearch, the operations are divided as follows:
- Cluster operations: At cluster level all write ones are locked, first they are applied to the master node and then to the secondary one. The read operations are typically broadcasted.
- Index management operations: These operations follow the cluster pattern.
- Record operations: These operations are executed on single documents at shard level.
When a record is saved in ElasticSearch, the destination shard is chosen based on the following factors:
- The ID (unique identifier) of the record. If the ID is missing, it is autogenerated by ElasticSearch.
- If the routing or parent (covered while learning the parent/child mapping) parameters are defined, the correct shard is chosen by the hash of these parameters.
Splitting an index into shards allows you to store your data in different nodes, because ElasticSearch tries to do shard balancing.
Every shard can contain up to 2^32 records (about 4.2 billion records), so the real limit to shard size is its storage size.
Shards contain your data and during search process all the shards are used to calculate and retrieve results. ElasticSearch performance in big data scales horizontally with the number of shards.
All native records operations (such as index, search, update, and delete) are managed in shards.
The shard management is completely transparent to the user. Only an advanced user tends to change the default shard routing and management to cover their custom scenarios. A common custom scenario is the requirement to put customer data in the same shard to speed up his/her operations (search/index/analytics).
It's best practice not to have a too big shard (over 10 GB) to avoid poor performance in indexing due to continuous merge and resizing of index segments.
It's not good to oversize the number of shards to avoid poor search performance due to native distributed search (it works as MapReduce). Having a huge number of empty shards in an index consumes only memory.
See also
- Shard on Wikipedia http://en.wikipedia.org/wiki/Shard_(database_architecture)
- Mastering vRealize Operations Manager(Second Edition)
- 發布!設計與部署穩定的分布式系統(第2版)
- 大學計算機應用基礎實踐教程(Windows 7+Office 2013)
- 無蘋果不生活 OS X Mountain Lion隨身寶典
- 精解Windows 8
- Kubernetes網絡權威指南:基礎、原理與實踐
- 嵌入式Linux驅動程序和系統開發實例精講
- SharePoint 2013 應用開發實戰
- Python基礎教程(第3版)
- Windows Server 2019 Administration Fundamentals
- 突破平面3ds Max動畫設計與制作
- AutoCAD 2014中文版從入門到精通
- Vim 8文本處理實戰
- 從零開始學安裝與重裝系統
- 鴻蒙操作系統設計原理與架構