Elastic Search

What is Search API in Elasticsearch?

Elasticsearch is a popular search engine to fetch data quickly from the analytics database using APIs inside the queries. It enables the user to get useful information from a huge pool of data in real-time from the Kibana User Interface. The information can be used to make improved decisions using the pictorial representation of the information.

This guide will explain the search API in Elasticsearch.

What is the Search API in Elasticsearch?

Elasticsearch search engine can be used to fetch data using search query strings inside the curl command. GET request is used to apply a search API which is used to fetch data from the database quickly and efficiently. Search API is a powerful tool as it contains many features to search required data from big data and display that on the screen.

How to Use Search API in Elasticsearch?

Copy the path of the bin file from the computer and use the following command to head inside it:

cd C:\Users\Lenovo\Documents\elasticsearch-8.8.2\bin

 
Inside the bin directory, use this command to run the Elasticsearch service:

elasticsearch.bat

 

Verify the Elasticsearch Connection

Open the terminal and paste the following command to verify the Elasticsearch connection after typing the user password for “elastic” user:

curl localhost:9200 -u elastic:<password>

 

Get Elasticsearch Aliases

Type the following command to get the indexes available on the Elasticsearch user to get the name of the required index:

curl http://localhost:9200/_aliases?pretty=true -u elastic:<password>

 

Get Data From Elasticsearch Index

Use the GET request to fetch data from the “demo-index” index with type “_doc” and id 1:

curl -X GET "localhost:9200/demo-index/_doc/1?pretty" -u elastic:<password>

 

Search API To Get an Index

The following command uses a GET request with the Search API to fetch the data from the “user” where “id” is 1. The “q” keyword refers to the query strings and the “pretty” term is used to display the output in a readable format:

curl -X GET "localhost:9200/_search?q=user.id:1&size=0&terminate_after=1&pretty" -u elastic:<password>

 

Simply use the name of the index in the above command to display the data from “demo-index”:

curl -X GET "localhost:9200/demo-index/_search?pretty" -u elastic:<password>

 

Let’s head to the next section that explain the searching API process in detail:

How to Search API Using Kibana UI?

Search API can also be used from the Kibana User Interface. For this, open the terminal and head inside the “bin” directory of Kibana from the local system and use the following command to connect to it:

kibana.bat

 

Use the following address in the web browser and log in to the Kibana by entering the username with the user password:

localhost:5601/

 

From the Kibana page, open the “Dev Tools” console by typing it from the search bar:


Use the GET request from the “demo-index” index using the search API:

GET /demo-index/_search

 

That’s all about the search API in Elasticsearch.

Conclusion

Search API in Elasticsearch is used to get data from the Elasticsearch database from its indexes using query string commands. The user can fetch data with the search API in Elasticsearch using the command line interface after connecting to Elasticsearch or Kibana User Interface. This guide has explained the search API and its use with two methods.

About the author

Talha Mahmood

As a technical author, I am eager to learn about writing and technology. I have a degree in computer science which gives me a deep understanding of technical concepts and the ability to communicate them to a variety of audiences effectively.