Create a new Index with mapping and settings in Elastic Search?
ES: Elastic Search
Prerequisites
In our previous articles on Elastic Search, we gained a solid understanding of what Elastic Search is and how it works. Now, we will delve into the Elastic Search APIs and learn how to create an index, update its mappings and settings. By understanding these APIs, we can more effectively use Elastic Search to optimize the search and analysis capabilities of our projects. Let’s take a closer look at how these APIs work and how they can be utilized.
Create Index
In ES, an index contains a schema and can have one or more shards and replicas. An ES index is divided into shards and each shard is an instance of a Lucene index.
There are different ways to create an index:
Below Example is based on latest version of ES. An index with two shards, each having one replica will be created with the name test_index
PUT /{index_name}/{type_name}
As above you can see while creating the index we have to define index_name
and type_name
.
Mandatory Fields: index_name
by default ES use _doc
as type name if we didn’t define anything. It is better to use default type name if we don’t have any collection partition criteria.
index => database
type => collection or table
PUT /test_index?pretty
{
"settings" : {
"number_of_shards" : 2,
"number_of_replicas" : 1
},
"mappings" : {
"properties" : {
"tags" : { "type" : "keyword" },
"updated_at" : { "type" : "date" }
}
}
}
In above example, we didn’t defined any type.
While creating the index you can define settings and your document properties data types.
It is good practice to define the settings and mapping of an Index wherever possible because if this is not done, ES tries to automatically guess the data type of fields at the time of indexing.
This automatic process may have disadvantages, such as mapping conflicts, duplicate data and incorrect data types being set in the index. If the fields are not known in advance, it’s better to use dynamic index templates.
Note: Wrong data types can cause issue while adding search or filtering functionality in querying.
If you want to update settings and mappings after creating the index then you have to perform following actions:
Create Index
PUT /{index_name}/
Close Index
POST /{index_name}/_close
Update Settings
PUT /{index_name}/_settings
{
max_ngram_diff: "50",
analysis: {
filter: {
autocomplete_filter: {
type: "ngram",
min_gram: "1",
max_gram: "20",
},
},
analyzer: {
autocomplete: {
filter: ["lowercase", "autocomplete_filter"],
type: "custom",
tokenizer: "whitespace",
},
},
},
number_of_replicas: "1",
}
Open Index
POST /{index_name}/_open
Update Mappings
PUT /{index_name}/_mappings
{
properties: {
id: { type: "text" },
make: {
type: "text",
analyzer: "autocomplete",
search_analyzer: "standard",
},
model: {
type: "text",
analyzer: "autocomplete",
search_analyzer: "standard",
},
type: { type: "keyword" },
},
}
Always define your mappings and settings while creating your index or just after creating the index. Else wrong mappings or automated mappings can cause issue in your search functionality.
If you are going to add any new field in your document, first update mappings for that property.
Some more useful Elastic Search index APIs:
Get Index Properties
GET /{index_name}/
Delete index
DELETE /{index_name}
List all Indices
GET /_cat/indices?v
I hope you enjoyed reading about some useful APIs about Elastic Search indices. If you found this article helpful or have any further questions, please don’t hesitate to reach out to me through the comments.
For more updates and insights on the latest tech trends, be sure to follow me on Twitter or LinkedIn. Thanks for reading, and I look forward to connecting with you on social media.
Twitter: https://twitter.com/geekfarmer_
Linkedin: https://www.linkedin.com/in/geekfarmer