Skip to content

How To Pass Mappings Of Elasticsearch Index Using Postman

API Testing Blog

Defining Elasticsearch Indices and Mappings with Postman

Postman offers a powerful way to interact with Elasticsearch APIs, including the ability to define and modify index mappings. Here’s a comprehensive guide on how to pass mappings of Elasticsearch indices using Postman.

Understanding Elasticsearch Indices and Mappings

An Elasticsearch index is a logical container for documents. Each document is a JSON object containing data. Mappings define the structure of these documents, specifying the data types, indexing strategies, and other properties for each field.

Creating a New Index with Mappings

  1. Setup:

    • Install the Postman app and create a new request.
    • Set the HTTP Method to PUT.
    • Enter the Elasticsearch endpoint URL: http://<your_elasticsearch_host>:<port>/<index_name>
    • Replace <your_elasticsearch_host>, <port> and <index_name> with the appropriate values.
  2. Body:

    • In the “Body” tab, select “Raw” and choose “JSON” formatting.
    • Paste the following JSON code into the body, replacing placeholders with your desired fields and data types:
    {
    "mappings": {
    "properties": {
    "title": {
    "type": "text"
    },
    "author": {
    "type": "keyword"
    },
    "published_date": {
    "type": "date"
    },
    "content": {
    "type": "text"
    }
    }
    }
    }
  3. Send the Request:

    • Click the “Send” button.
    • Successfully created indices will return a 200 status code.

Example: Creating a “books” index with mappings

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

Here, the mapping defines the fields and their types. text fields are for text data with analysis enabled, while keyword fields are for exact matches. The date field represents a date.

Updating Existing Index Mappings

  1. Setup:

    • Use the PUT method with the Elasticsearch endpoint URL for the specific index.
  2. Body:

    • Only provide the mappings changes you want to apply in the JSON body:
    {
    "mappings": {
    "properties": {
    "category": {
    "type": "keyword"
    }
    }
    }
    }
  3. Send the Request:

    • Click “Send”.
    • Observe the response status code (200 indicates success).

Retrieving Index Mappings

  1. Setup:
    • Use the GET method and append /_mapping to the Elasticsearch endpoint for the specific index.
  2. Send the Request:
    • The response body will contain the complete mappings definition of the index.

Managing the Index: Deleting an Index

  1. Setup:
    • Use the DELETE method and append the index name to the Elasticsearch endpoint: http://<your_elasticsearch_host>:<port>/<index_name>
  2. Send the Request:
    • A 200 status code indicates successful deletion of the index.

Using Postman Collections for Elasticsearch Testing

Organize your Elasticsearch tests with Postman Collections. Create a collection for interacting with your indices:

  1. Create a Collection:
    • In Postman, click ”+ Create” and choose “Collection”.
  2. Add Requests:
    • Add requests for creating, updating, retrieving, and deleting indices, as outlined above.
  3. Organize with Folders:
    • Group related requests within folders for better organization.

Postman Environment Variables for Dynamic Testing

Use environment variables in Postman to make your testing more flexible:

  1. Create an Environment:
    • Navigate to “Environments” and click “Add”.
    • Define variables for host, port, index names, and any other values that could change.
  2. Use Environment Variables:
    • Use {{variable_name}} syntax to access environment variables in your request URLs and bodies.

Advanced Mapping Options with Postman

Postman allows you to leverage the full power of Elasticsearch mappings through the JSON body of your requests. Explore and customize the following features:

  • Analyzers: Control how text data is indexed, including stop word removal, stemming, and tokenization.
  • Data Types: Utilize various data types like float, boolean, geo_point, and more to accurately represent your data.
  • Nested Objects: Structure your documents to have nested objects with their own mappings.
  • Custom Analyzers: Create custom analyzers with specific rules for your data.

By mastering the techniques presented here, you gain a powerful tool for efficiently and confidently testing your Elasticsearch implementations using Postman.

API Testing Blog