Elasticsearch Creating an Index with Mapping in Elasticsearch

By Opster Team

Updated: Jul 23, 2023

| 2 min read

Introduction 

In Elasticsearch, an index is a collection of documents that have similar characteristics. For instance, you can have an index for customer data, another index for product catalog, and so on. Each index is identified by a unique name which is used to refer to the index while performing indexing, search, update, and delete operations.

Mapping, on the other hand, is the process of defining how a document and its fields are stored and indexed. It is like the schema definition in a relational database. Mapping allows you to define how each field should be treated by Elasticsearch, whether it should be tokenized and filtered, or whether it should be a specific type like integer, date, etc.

In this article, we will delve into the process of creating an index with mapping in Elasticsearch. If you want to learn about Elasticsearch index – how to create, list, query and delete indices, check out this guide.

Step 1: Define the Mapping

Before creating an index, you need to define the mapping. The mapping is defined as a JSON object, which includes the properties for each field in the index. Here is an example of a mapping for an index that stores blog posts:

son
{
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "content": { "type": "text" },
      "author": { "type": "keyword" },
      "date": { "type": "date" },
      "tags": { "type": "keyword" }
    }
  }
}

In this mapping, we have five fields: `title`, `content`, `author`, `date`, and `tags`. The `title` and `content` fields are of type `text`, which means they will be full-text searchable. The `date` field is of type `date`, and the `author` and `tags` fields are of type `keyword`, which means they will be indexed as a whole and not analyzed.

Step 2: Create the Index with the Mapping

Once you have defined the mapping, you can create the index with the mapping. This is done using the `PUT` request to the Elasticsearch server. Here is an example of how to create an index named `blog` with the above mapping:

bash
curl -X PUT "localhost:9200/blog?pretty" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "content": { "type": "text" },
      "author": { "type": "keyword" },
      "date": { "type": "date" },
      "tags": { "type": "keyword" }
    }
  }
}
'

In this command, `localhost:9200/blog?pretty` is the URL of the Elasticsearch server and the name of the index. The `-H ‘Content-Type: application/json’` part specifies the content type of the request body. The `-d’…’` part is the request body, which includes the mapping we defined earlier.

Step 3: Verify the Index Creation

After creating the index, you can verify its creation by sending a `GET` request to the Elasticsearch server. Here is how to do it:

bash
curl -X GET "localhost:9200/blog?pretty"

This command will return the information about the `blog` index, including its mapping.

Conclusion 

In conclusion, creating an index with mapping in Elasticsearch involves defining the mapping as a JSON object, creating the index with the mapping using a `PUT` request, and verifying the index creation with a `GET` request. This process allows you to have full control over how your data is stored and indexed in Elasticsearch, which can greatly improve the performance and relevancy of your search operations.