Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Indexing Documents in Elasticsearch

Introduction

Elasticsearch is a powerful search and analytics engine that allows you to index, search, and analyze large volumes of data quickly and in near real-time. In this tutorial, we will walk you through the process of indexing documents into Elasticsearch. Indexing is the process of adding data to Elasticsearch so it can be searched and analyzed.

Setting Up Elasticsearch

Before you can index documents, you need to have an Elasticsearch instance running. You can download and install Elasticsearch from the official website or use a hosted service like Elasticsearch Service. For this tutorial, we'll assume you have Elasticsearch running locally on http://localhost:9200.

Creating an Index

In Elasticsearch, data is organized into indices. An index is like a database in a relational database system. Let's create an index named "my_index":

Using the command line:

curl -X PUT "localhost:9200/my_index"
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "my_index"
}
                

Indexing a Document

To index a document in Elasticsearch, you use the _doc endpoint. A document is a JSON object that contains the data you want to index. Here's an example of indexing a document:

Using the command line:

curl -X POST "localhost:9200/my_index/_doc/1" -H 'Content-Type: application/json' -d'
{
  "title": "Elasticsearch Basics",
  "author": "John Doe",
  "content": "This is an introductory article about Elasticsearch."
}'
{
  "_index": "my_index",
  "_type": "_doc",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}
                

Retrieving a Document

Once a document is indexed, you can retrieve it using its ID. Here's how to retrieve the document we just indexed:

Using the command line:

curl -X GET "localhost:9200/my_index/_doc/1"
{
  "_index": "my_index",
  "_type": "_doc",
  "_id": "1",
  "_version": 1,
  "_seq_no": 0,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "title": "Elasticsearch Basics",
    "author": "John Doe",
    "content": "This is an introductory article about Elasticsearch."
  }
}
                

Updating a Document

You can update an existing document using the _update endpoint. Here's how to update the content of our document:

Using the command line:

curl -X POST "localhost:9200/my_index/_update/1" -H 'Content-Type: application/json' -d'
{
  "doc": {
    "content": "This is an updated article about Elasticsearch."
  }
}'
{
  "_index": "my_index",
  "_type": "_doc",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 1,
  "_primary_term": 1
}
                

Deleting a Document

If you no longer need a document, you can delete it using the _doc endpoint with the DELETE method. Here's how to delete the document:

Using the command line:

curl -X DELETE "localhost:9200/my_index/_doc/1"
{
  "_index": "my_index",
  "_type": "_doc",
  "_id": "1",
  "_version": 3,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 2,
  "_primary_term": 1
}
                

Conclusion

In this tutorial, we covered the basics of indexing documents in Elasticsearch. We walked through creating an index, indexing a document, retrieving, updating, and deleting a document. Elasticsearch provides a powerful and flexible way to manage and search your data.