Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Overview of Elasticsearch

What is Elasticsearch?

Elasticsearch is a distributed, RESTful search and analytics engine capable of solving a growing number of use cases. As the heart of the Elastic Stack, it centrally stores your data for lightning-fast search, fine-tuned relevancy, and powerful analytics that scale with ease.

Core Concepts

Before diving into Elasticsearch, it's important to understand some of its core concepts:

  • Index: An index is like a database in a relational database. It is a collection of documents that have similar characteristics.
  • Document: A document is a basic unit of information that can be indexed. It is expressed in JSON (JavaScript Object Notation).
  • Shard: Indices are divided into shards, which are individual instances of an index that can be distributed across the cluster.
  • Replica: For each shard, you can have one or more replicas. These are copies of the shard and are used for failover and redundancy.

Installing Elasticsearch

To get started with Elasticsearch, you'll need to install it. The official website provides detailed instructions for different operating systems:

For Ubuntu/Debian:

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
sudo apt-get install apt-transport-https
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list
sudo apt-get update && sudo apt-get install elasticsearch

For other operating systems, please refer to the official documentation.

Starting Elasticsearch

Once you have installed Elasticsearch, you can start it using the following command:

sudo systemctl start elasticsearch

To enable Elasticsearch to start automatically at boot time, use:

sudo systemctl enable elasticsearch

Basic Operations

Let's go through some basic operations like indexing, searching, and deleting documents.

Indexing a Document

To index a document, you can use the following CURL command:

curl -X POST "localhost:9200/my_index/_doc/1" -H 'Content-Type: application/json' -d'
{
  "name": "John Doe",
  "age": 30,
  "email": "john.doe@example.com"
}'

Searching for a Document

To search for a document, you can use the following CURL command:

curl -X GET "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "name": "John"
    }
  }
}'
{ "took": 30, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1.0, "hits": [ { "_index": "my_index", "_type": "_doc", "_id": "1", "_score": 1.0, "_source": { "name": "John Doe", "age": 30, "email": "john.doe@example.com" } } ] } }

Deleting a Document

To delete a document, you can use the following CURL command:

curl -X DELETE "localhost:9200/my_index/_doc/1"

Conclusion

This tutorial provided an overview of Elasticsearch, including its core concepts and some basic operations. For more detailed information, please refer to the official documentation.