English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Elasticsearch Search API

This API is used to search for content in Elasticsearch. Users can search by sending a GET request with a query string as a parameter, or they can publish the query in the body of the published request. The search API is mainly for multi-index and multi-type.

Multi-index

Elasticsearch allows us to search all indexes or certain specific indexes for existing documents. For example, if we need to search for all documents with names containing 'central', we can perform the following operation:

GET /_all/_search?q=city:paprola

When running the above code, we get the following response-

{
   "took": 33,
   "timed_out": false,
   "_shards": {
      "total": 7,
      "successful": 7,
      "skipped": 0,
      "failed": 0
   },
   "hits": {
      "total": {
         "value": 1,
         "relation": "eq"
      },
      "max_score": 0.9808292,
      "hits": [
         {
            "_index": "schools",
            "_type": "school",
            "_id": ""5",
            "_score": 0.9808292,
            "_source": {"}}
               "name": "Central School"
               "description": "CBSE Affiliation",
               "street": "Nagan",
               "city": "paprola",
               "state": "HP",
               "zip": ""176115",
               "location": [
                  31.8955385,
                  76.8380405
               ],
               "fees": 2200,
               "tags": [
                  "Senior Secondary",
                  "beautiful campus"
               ],
               "rating": ""3.3"
            }
         }
      ]
   }
}

URI search

Many parameters can be passed in the search operation using Uniform Resource Identifier-

Serial numberParameters and descriptions
1

Q

This parameter is used to specify the query string

2

lenient

This parameter is used to specify the query string. If this parameter is set to true, it can ignore Formatbased errors. By default, it is false.

3

fields

This parameter is used to specify the query string

4

sort

We can use this parameter to get the sorted results, the possible values of this parameter are fieldName, fieldName:asc/ fieldName:desc

5

timeout

We can use this parameter to limit the search time, and the response only contains hits within the specified time. By default, there is no timeout

6

terminate_after

We can limit the response to a specified number of documents per shard, and the query will terminate early when reaching the shard. By default, there is no termin_after.

7

from

The starting index of the number of hits to be returned. The default is 0.

8

size

It represents the number of hits to be returned, the default value is10.

Request body search

We can also use query DSL in the request body to specify the query, and many examples have been given in the previous chapters. Here is an instance of such a case-

POST /schools/_search
{
   "query": {
      "query_string": {
         "query": "up"
      }
   }
}

When running the above code, we get the following response-

{
   "took": 11,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "skipped": 0,
      "failed": 0
   },
   "hits": {
      "total": {
         "value": 1,
         "relation": "eq"
      },
      "max_score": 0.47000363,
      "hits": [
         {
            "_index": "schools",
            "_type": "school",
            "_id": ""4",
            "_score": 0.47000363,
            "_source": {"}}
               "name": "City Best School",
               "description": "ICSE",
               "street": "West End",
               "city": "Meerut",
               "state": "UP",
               "zip": ""250002",
               "location": [
                  28.9926174,
                  77.692485
               ],
               "fees": 3500,
               "tags": [
                  "fully computerized"
               ],
               "rating": ""4.5"
            }
         }
      ]
   }
}