0
0
Elasticsearchquery~30 mins

Indexing a document (POST/PUT) in Elasticsearch - Mini Project: Build & Apply

Choose your learning style9 modes available
Indexing a Document in Elasticsearch
📖 Scenario: You work at a company that stores product information in Elasticsearch. You need to add new products to the Elasticsearch index so they can be searched later.
🎯 Goal: Learn how to index a document in Elasticsearch using the POST and PUT methods.
📋 What You'll Learn
Create a JSON document representing a product with specific fields
Set the index name in a variable
Use the POST method to add a new product document without specifying an ID
Use the PUT method to add a product document with a specific ID
Print the JSON response from Elasticsearch
💡 Why This Matters
🌍 Real World
Indexing documents is how search engines like Elasticsearch store data so users can find it quickly.
💼 Career
Knowing how to add and update documents in Elasticsearch is essential for backend developers and data engineers working with search and analytics.
Progress0 / 4 steps
1
Create the product document
Create a JSON object called product with these exact fields and values: "name": "Wireless Mouse", "price": 25.99, "in_stock": true.
Elasticsearch
Need a hint?

Use Python dictionary syntax to create the product object.

2
Set the index name
Create a variable called index_name and set it to the string "products".
Elasticsearch
Need a hint?

Assign the string "products" to the variable index_name.

3
Index the product using POST
Use the requests library to send a POST request to http://localhost:9200/{index_name}/_doc with the product JSON as the body. Store the response in a variable called response_post. Use json=product in the request.
Elasticsearch
Need a hint?

Use requests.post with the URL and json=product to send the data.

4
Index the product using PUT with ID and print response
Send a PUT request to http://localhost:9200/{index_name}/_doc/1001 with the product JSON as the body. Store the response in response_put. Then print the JSON response from response_put using print(response_put.json()).
Elasticsearch
Need a hint?

Use requests.put with the URL including 1001 as the document ID. Then print the JSON response.