Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Comprehensive Elasticsearch Tutorial

Introduction to Elasticsearch

Elasticsearch is a distributed, RESTful search and analytics engine capable of addressing a growing number of use cases. As the heart of the Elastic Stack, it centrally stores your data so you can discover the expected and uncover the unexpected.

Installation

Follow these steps to install Elasticsearch on your system:

1. Download and install the public signing key:

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

2. Add the repository definition:

sudo sh -c 'echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" > /etc/apt/sources.list.d/elastic-7.x.list'

3. Update your package list and install Elasticsearch:

sudo apt-get update && sudo apt-get install elasticsearch

4. Start and enable the Elasticsearch service:

sudo systemctl start elasticsearch && sudo systemctl enable elasticsearch

Basic Concepts

Understanding the basic concepts of Elasticsearch is crucial:

  • Index: A collection of documents that have similar characteristics.
  • Document: A basic unit of information that can be indexed.
  • Shard: A horizontal partition of data in an index.
  • Replica: A copy of a shard for redundancy and high availability.

Creating an Index

To create an index in Elasticsearch, use the following command:

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

Adding a Document

Add a document to the index using the following command:

curl -X POST "localhost:9200/my_index/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "user": "john_doe",
  "post_date": "2023-10-05T14:12:12",
  "message": "Elasticsearch is awesome!"
}'
{
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

Searching for Documents

Search for documents using the following command:

curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "message": "Elasticsearch"
    }
  }
}'
{
  "took" : 30,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "user" : "john_doe",
          "post_date" : "2023-10-05T14:12:12",
          "message" : "Elasticsearch is awesome!"
        }
      }
    ]
  }
}

Updating a Document

Update a document using the following command:

curl -X POST "localhost:9200/my_index/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "doc": {
    "message": "Elasticsearch is super awesome!"
  }
}'
{
  "_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

Delete a document using the following command:

curl -X DELETE "localhost:9200/my_index/_doc/1?pretty"
{
  "_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

Elasticsearch is a powerful tool for searching, analyzing, and visualizing data in real-time. This tutorial covers the basics of installing Elasticsearch, creating an index, adding, searching, updating, and deleting documents. With these fundamental concepts, you can start exploring more advanced features and configurations of Elasticsearch to suit your specific needs.