Example of Elasticsearch Populate
Suppose you have a dataset containing information about products in an e-commerce store. Each document in the dataset represents a product and contains attributes such as product name, description, price, and category. You want to make this data searchable using Elasticsearch. Here’s how you can populate an Elasticsearch index with this dataset
Creating an Index
First, let’s create an index named products and define mappings to specify the structure of our documents.
PUT /products
{
"mappings": {
"properties": {
"name": { "type": "text" },
"description": { "type": "text" },
"price": { "type": "float" },
"category": { "type": "keyword" }
}
}
}
This code creates an index named products and defines mappings for the properties name, description, price, and category. The name and description fields are set as text type for full-text search capabilities, while price is set as a float type for numerical data, and category is set as a keyword type for exact match searching.
Creating a Document
Next, we’ll add a product document to the products index. This document represents a specific product with its attributes.
POST /products/_doc/1
{
"name": "Laptop",
"description": "High-performance laptop with SSD storage.",
"price": 1200.50,
"category": "Electronics"
}
This code adds a document with an ID of 1 to the products index. The document contains fields for name, description, price, and category, each with corresponding values representing a laptop product.
Reading a Document
We can retrieve the details of the laptop document by its ID.
GET /products/_doc/1
Output:
{
"_index": "products",
"_type": "_doc",
"_id": "1",
"_source": {
"name": "Laptop",
"description": "High-performance laptop with SSD storage.",
"price": 1200.50,
"category": "Electronics"
}
}
This output displays the details of the laptop document stored in the products index, including its name, description, price, and category.
Updating a Document
Suppose the price of the laptop changes. We can update the document accordingly.
POST /products/_update/1
{
"doc": {
"price": 1300.00
}
}
This code updates the price of the laptop document with ID 1 to 1300.00.
Deleting a Document
Finally, remove the laptop document from the products index.
DELETE /products/_doc/1
This code deletes the document with ID 1 from the products index.
Elasticsearch Populate
Elasticsearch stands as a powerhouse tool for managing large volumes of data swiftly, offering robust features for indexing, searching, and analyzing data. Among its arsenal of capabilities lies the “populate” feature, a vital function for efficiently managing index data.
In this article, we’ll delve into Elasticsearch’s populate feature, exploring its purpose, functionality, and how to wield it effectively through practical examples.