Introduction to Basic Concepts in 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 so you can discover the expected and uncover the unexpected.
Basic Concepts
Before diving deeper into Elasticsearch, it's important to understand some of its basic concepts:
- Document: The basic unit of information that can be indexed. For example, a document could be a single user profile, a single log entry, or a single product description.
- Index: A collection of documents that have somewhat similar characteristics. An index is identified by a name, which is used to refer to the index when performing various operations within it.
- Shard: Each index is divided into shards. Shards are the basic building blocks of an index and allow for distributed storage and search capabilities.
- Cluster: A cluster is a collection of one or more nodes (servers) that together holds your entire data and provides federated indexing and search capabilities across all nodes.
Example: Creating an Index
Let's create a simple index in Elasticsearch. Below is an example of how you can create an index called users
:
Example: Adding a Document
Now that we have created an index, let's add a document to this index. Below is an example of how you can add a document to the users
index:
{
"name": "John Doe",
"age": 30,
"email": "johndoe@example.com"
}
Example: Searching for a Document
To search for documents in an index, you can use the following query. Below is an example of how you can search for documents in the users
index:
{
"query": {
"match": {
"name": "John"
}
}
}
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.2876821,
"hits": [
{
"_index": "users",
"_type": "_doc",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "John Doe",
"age": 30,
"email": "johndoe@example.com"
}
}
]
}
}