Javatpoint Logo
Javatpoint Logo

Elasticsearch Search APIs

Elasticsearch provides search API, which is used for searching the data across indexes and all types. It helps to search the data in Elasticsearch by executing the search query and get back the search result matched with the query. This API enables you to search the data within Elasticsearch. You can search the data in two ways -

  • Either by sending a GET request with a query that has a string parameter or
  • Using a POST request that consists query in the message body.

Look at each example of both types -

Using GET request

Execute the following query. It will display all the records present in new_student index.

Copy Code

Response

In this following response, you can see that two records are found after executing the above request.

Using POST request

Copy Code

Response

The response will be similar to the GET method request. Look in the screenshot below to see the output in elasticsearch plugin -

Elasticsearch Search APIs

In the above screenshot, you can see that two records have returned same as the GET request output.

Now, let's move to the types of search API, which is classified into three types:

  1. Multi-Index
  2. Multi-Type
  3. URI Search

Multi-index is a most searched API. However, we will discuss each search API in detail, along with examples. Before this, below are some request parameters which can be passed in a search operation -

Parameter Description
Q It is used to specify the query string.
Sort This parameter is used to sort the responses.
Fields It fetches the results from selective fields.
Lenient This parameter is used to ignore the format-based error by setting up the value to true.
from This parameter helps to specify the starting point of the index. The default value is 0.
Size Specifies the number of hits to return. By default, the size is 10.
Timeout It restricts the search time.
terminate_after It is responsible for restricting the response to a specific number of documents in each shard.

Now, let's discuss each API in detail -

1. Multi index

Multi index API is a type of search API that is used for searching documents in the indexes. It searches all the documents in some specific indices or all indices. For some specific indices search, specify the indexes name separated by comma (,) on which you want to perform the search operation. In case if you want to search in all indices, specify _all keyword instead of indexes name.

Multi Index API allows the user to search on two or more indexes at once.

For example, we will search for all the students whose course is Mass Communication in two different indices (student and new_student) using a single query.

Method: GET
API: _search
Index: student, new_student
Parameter: q

Execute the following query and search the documents.

Copy Code

Response

In this following response, you can see that two records are found after executing the above query.

See the screenshot below how query executes in elasticsearch plugin -

Elasticsearch Search APIs

2. Multi Type

Multi type search API provides the facility to search all the documents in a particular index across all types or in some specific types. In this below example, we will execute the following query to search the JSON documents from the "student" index under all types, where zip code is 30501.

Method: GET
API: _search
Index: student
Parameter: q

Execute the following query and search the documents.

Copy Code

Response

In this following response, you can see that one JSON document in student index whose zip is 30501.

See the screenshot below how query executes in elasticsearch plugin -

Elasticsearch Search APIs
Remember - In elasticsearch database, queries are case sensitive, e.g., _search and _Search both are different. Similarly, zip and Zip are treated differently in elasticsearch. So, be careful while creating queries.

3. URI Search

URI search is a Uniform Resource Identifier Search. It allows us to pass multiple request parameters in a search operation using a URI so that the request can be executed purely. Whenever you execute the search query using this mode, all the search options are not exposed. However, it is handy for curl tests.

Execute the following query and search the documents in student index.

Copy Code

Response

The above query will return all the documents present in new_student index.

See the screenshot below how URI search operation executes in elasticsearch plugin -

Elasticsearch Search APIs

Get multiple records by id

We can even search for multiple documents by id in a single query request, along with all these search operations. For which we need to specify the id of each document that we are looking for.

For Example

We will use POST API to retrieve multiple documents from an index. It is not enough; we also need to append _mget in request string and define document id query space to fetch multiple records.

Copy Code

Here, we are getting the document for id 02 and 04.

Response

You will get the output like the below response.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA