Managing elastic search document

Ankita
5 min readDec 7, 2020

For basics on Elastic search and to go through the basic terminology , go through my previous story.

Creating Index with default settings

Creating Index with settings of replica and shards

Delete Index

Modifying the settings of cluster

Adding a document with auto generated id to an index.

Adding a document with predefined id

Creating a document with explicit routing

Shard is determined by the below formula :-

shard_num = hash(_routing) % num_primary_shards

So what if we change the number of shards ?

All documents needs to be reindexed. As the routing of already indexed document will not be correct.

Updating a document

How update internally works ?

Updates retrieves the documents , adds or modifies field and replaces the document.

What if you want to increment/decrement the counter in a document. Rather than retrieving the document, incrementing/decrementing counter and then updating the document. This two trips can be done in one go by using the scripted updates.

Upsert :- It updates the document if it already exists or index a new document with the fields under the upsert field.

Replacing the document is done by using PUT api

Deleting a document with a timeout :- So this call waits till the timeout is over for the document to be deleted from primary shard.

Optimistic concurrency control

Primary term and sequence no

Global and local checkpoints

The global checkpoint is a sequence number for which we know that all active shards histories are aligned at least up to it.

The local checkpoint is a sequence number for which all lower sequence numbers were processed on the shard copy.

How elastic search reads and writes data ?

Reading is done by any of the nodes in the replication group.

Writing is done only on primary shard and later added asynchronously to replicas.

Update by query

“conflicts” : “proceed”

Delete by query

Bulk processing

Analysis

Analyse api

Mapping

Data types in elastic search

Index template

Mutlifields

We can index a same field in two different ways. In the example below city field could be mapped as a text field for full-text search, and as a keyword field for exact matching or sorting or aggregation.

Dynamic mapping

By default, fields can be added dynamically to a document by just by indexing a document containing the new field.

But we can configure the dynamic mapping of an index. This is done by setting the dynamic parameter to

true

Newly detected fields are added to the mapping. (default)

false

Newly detected fields are ignored. These fields will not be indexed so will not be searchable but will still appear in the _source field of returned hits. These fields will not be added to the mapping, new fields must be added explicitly.

strict

If new fields are detected, an exception is thrown and the document is rejected. New fields must be explicitly added to the mapping.

Next : https://ankita4priya.medium.com/elastic-search-search-queries-f94f188488a6

--

--

Ankita

Senior Developer at Twitter. Tech Enthusiast. Learner. If I can so can you !