Elasticsearch index creation and access API through Postman

Imran Khan
4 min readSep 14, 2023

As part of this blog, we will be creating index, sync, fetch, delete data/document with Elasticsearch with the help of postman.

At the end of the blog, provided the link to download the Postman collection.

Setup Elasticsearch Instance

AEM and Elastic Search Integration

Elasticsearch CORS Access-Control-Allow-Origin issue

Elasticsearch ApiKey Creation

Elastic App Search

AEM and Elastic Search

Follow below steps to perform various CRUD operations:

Create Index

Index creation is similar to creation of table in SQL. Follow below step by step process to create index and update data.

  1. Open postman and create a brand new collection named as Practice to have our all API calls.
  2. Create below postman request having PUT method, API URL, username and password which we got as part of this blog.

3. Setup below headers under Headers tab

Postman-Token → <calculated when request is sent>
Content-Length → 0
Host → <calculated when request is sent>
User-Agent → PostmanRuntime/7.32.3
Accept → */*
Accept-Encoding → gzip, deflate, br
Connection → keep-alive

4. Select body tab having JSON option selected in dropdown and paste below code inside body section to provide index definition.

{
"settings": {
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 10
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
}
},
"mappings": {
"properties": {
"firstName": {
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "standard"
},
"lastName": {
"type": "text"
},
"age": {
"type": "integer"
}
}
}
}

As part of this index definition, firstName is of type as text and autocomplete. firstName field we are going to use for autocomplete search.

lastName and age are normal text fields.

5. Click on the send button will create an index with the name of practice-index-en

6. To verify the same hit url and click on Open button on home page.

7. Click on the below highlighted Enterprise Search option as shown below.

8. Selection of below indices option will help you to see created index as practice-index-en

GET/Fetch index details

Use same API URL with method type as GET and empty body will help us to get the index definition as shown below.

Delete Index

Update the method type to delete will help you to delete the created index in earlier step.

Sync/POST/Create Document in Elasticsearch

Use below postman call to sync/post/save data to Elasticsearch.

Set method type as PUT and enter below URL

https://45c67963f6e84668afa17a2cb292138c.us-central1.gcp.cloud.es.io:443/practice-index-en/_doc/doc1

Set body to below JSON object

{
"firstName": "Imran",
"khan" : "khan",
"age": 30
}

Click on Send button will POST/sync/save data in Elasticsearch, response code as 201 and result as created.

Let’s verify the same in cloud Elasticsearch, select indices and select Documents tab to see saved/created record.

Note: Same call can be used to update data within Elasticsearch as it is a PUT call.

Get/fetch document using id

Use same API URL with method type as GET and empty body will help us to get the document for id as doc1 mentioned as part of URL.

https://45c67963f6e84668afa17a2cb292138c.us-central1.gcp.cloud.es.io:443/practice-index-en/_doc/doc1

Search document based on Query parameter

Updating URL to below one will help us to fetch document from Elasticsearch. It will check all indexed fields having ‘khan’ as part of it and return same in the result.

https://25c75063f6e34368afa17a2cb292138c.us-central1.gcp.cloud.es.io:443/practice-index-en/_search?q=khan

Autocomplete Search

Autocomplete search returns result as soon as we starts typing and fetch all matched results.

https://25c75063f6e34368afa17a2cb292138c.us-central1.gcp.cloud.es.io:443/practice-index-en/_search

Below is the request body to fetch autocomplete data.

{
"query": {
"match": {
"firstName": {
"query": "im"
}
}
}
}

Response:

I hope you found out this article interesting and informative. Please clap and share it with your friends to spread the knowledge.

You can follow me for upcoming blogs follow.
Thank you !!!

--

--