Elasticsearch Basic Concepts

Rachadian Novansyah
JDS Engineering
Published in
4 min readMar 11, 2023
Source: https://commons.wikimedia.org/wiki/File:Elasticsearch_logo.svg

Elasticsearch is a free and open-source distributed search and analytics engine. Elasticsearch can be used to search various types of documents, from text documents to numeric data.

Elasticsearch is a very important technology in managing data in the digital era. By using Elasticsearch, we can access and manage data quickly and efficiently. Elasticsearch allows its users to search data quickly, easily, and accurately.

Here are the important concepts in Elasticsearch:

  • Index: Elasticsearch data store.
  • Document: Data stored in Index.
  • Field: Part of the document that has a certain data type.
  • Query: Command to search data in Elasticsearch.

In addition, Elasticsearch also has features such as:

  • Full-text search: Search based on keywords in the text.
  • Aggregations: Grouping data based on certain criteria.
  • Geo search: Search based on geographic location.
  • Machine learning: Data analysis using machine learning techniques.

Elasticsearch can be integrated with various types of applications, such as web, mobile, and desktop applications. Elasticsearch can also be used for purposes such as indexing, searching, and data analysis. One of the advantages of Elasticsearch is its ability to perform very fast data searches. It’s because Elasticsearch uses inverted index technology which allows data to be indexed and stored in a format that is very fast to access. Also, Elasticsearch has a caching feature that allows searching for the same data to be done more quickly because the data is already stored in the cache.

Here are some sample queries that can be used in Elasticsearch:

  • Term Query
GET /my_index/_search
{
"query": {
"term": {
"name": "Martin Odegaard"
}
}
}

The query above will look for data with the name “Martin Odegaard” in the “name” field.

  • Match Query:
GET /my_index/_search
{
"query": {
"match": {
"description": "description of basics elasticsearch."
}
}
}

The query above will look for data that has the keyword “description of basics elasticsearch” in the “description” field.

  • Range Query:
GET /my_index/_search
{
"query": {
"range": {
"price": {
"gte": 1000,
"lte": 5000
}
}
}
}

The query above will look for data with a price between 1000 to 5000 in the “price” field.

  • Wildcard Query:
GET /my_index/_search
{
"query": {
"wildcard": {
"name": "M*"
}
}
}

The query above will look for data with the field “name” starting with the letter “M”.

  • Bool Query:
GET /my_index/_search
{
"query": {
"bool": {
"must": [
{ "match": { "description": "elasticsearch" }},
{ "range": { "price": { "gte": 1000, "lte": 5000 }}}
]
}
}
}

The query above will search for data with the keyword “elasticsearch” in the “description” field and prices between 1000 to 5000 in the “price” field by using the logical AND operation.

In others, Elasticsearch also has more complex queries such as nested queries, filtered queries and many more. All types of queries can be used to find more specific and accurate data.

How to Create Indexes and Documents in Elasticsearch

To create an index on Elasticsearch, you can use thePUT. Below is an example of using the PUT API to create a new index:

PUT /nama_index
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}

In the example above, nama_indexis the name of the index you want to create. Parameter settings are used to set the index configuration, such as the number of shards and replicas.

After the index has been successfully created, you can add documents to the index using the POSTAPI. The following is an example of using the POST API to add a new document to the index:

POST /nama_index/_doc
{
"name": "Martin Odegaard",
"age": 26,
"address": "Jl. Encep Kartawirya No. 88"
}

In the example above, nama_index is the name of the index to which you want to add a new document. The _doc parameter is used to specify the type of document you want to add. In addition, there are three fields added to the document, namely name, age, and address.

To update an existing document, you can use the POST API by specifying the ID of the document you want to update. The following is an example of using the POST API to update a document:

POST /nama_index/_doc/1
{
"name": "Bukayo Saka",
"age": 25,
"address": "Jl. Citeureup No. 456"
}

In the example above, 1 is the ID of the document to be updated. There are three fields that are updated in the document, namelyname, age, and address.

To delete a document, you can use the DELETE API by specifying the ID of the document you want to delete. The following is an example of using the DELETE API to delete a document:

DELETE /nama_index/_doc/1

In the example above, 1 is the ID of the document you want to delete.

Conclusion

Elasticsearch is a powerful technology for managing and analyzing data. It allows users to search data quickly and efficiently and offers features such as full-text search, aggregations, geo search, and machine learning. Elasticsearch uses inverted index technology and caching to perform fast data searches. There are different types of queries that can be used in Elasticsearch, and creating indexes and documents can be done using the PUT and POST APIs. Updating and deleting documents can also be done using the POST and DELETE APIs.

Next, InsyaAllah I will share my experience using Elasticsearch in the product that I made, stay tuned and happy coding!

--

--

Rachadian Novansyah
JDS Engineering

Here's my story journey as Backend Engineer at Jabar Digital Service.