Introduction
Creating an index in Elasticsearch helps organize and store your data so you can search it quickly and easily.
Creating an index in Elasticsearch helps organize and store your data so you can search it quickly and easily.
PUT /index_name
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"field1": { "type": "text" },
"field2": { "type": "keyword" }
}
}
}PUT method to create a new index with a chosen name.settings like shards and replicas, and mappings to specify data types.PUT /books
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"properties": {
"title": { "type": "text" },
"author": { "type": "keyword" },
"year": { "type": "integer" }
}
}
}PUT /movies
{
"mappings": {
"properties": {
"name": { "type": "text" },
"genre": { "type": "keyword" },
"release_date": { "type": "date" }
}
}
}This query creates an index called library with three fields: title, author, and published_year. It uses 1 shard and no replicas for simplicity.
PUT /library
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"title": { "type": "text" },
"author": { "type": "keyword" },
"published_year": { "type": "integer" }
}
}
}Index names must be lowercase and cannot contain spaces.
Shards split your data for faster search; replicas provide copies for safety.
Mapping defines how each field is stored and searched.
Creating an index organizes your data for fast searching.
Use PUT /index_name with settings and mappings to create it.
Choose field types carefully to match your data.
products in Elasticsearch?{
"settings": {"number_of_shards": 1},
"mappings": {
"properties": {
"name": {"type": "text"},
"price": {"type": "float"}
}
}
}price field in the index?PUT /library
{
"settings": {
"number_of_shards": "two"
},
"mappings": {
"properties": {
"title": {"type": "text"}
}
}
}users with a field age that should only accept whole numbers and be searchable. Which mapping is correct?Pass the test and this pattern moves unknown