...

Search Engine - Elastic Search

Back to Course

Lesson Description


Lession - #133 ES Search APIs


Elasticsearch Search APIs

Elasticsearch gives search API, which is utilized for looking through the information across files and various kinds. It assists with looking through the information in Elasticsearch by executing the pursuit inquiry and get back the output coordinated with the question. This API empowers you to look through the information inside Elasticsearch. You can look through the information in two ways - Either by sending a GET demand with a question that has a string boundary or Utilizing a POST demand that comprises question in the message body. Take a gander at every illustration of the two types -

Using GET request

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

GET /new_student/  
_search   
{  
     "match_all": { }  
}  


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

{   
"took": 93,  
"timed_out": false,  
"_shards": {  
"total":2,  
"successful":1,  
"skipped":0,  
"failed": 0  
},  
"hits": {  
                   "total": {  
 "value": 2,  
 "relation": "eq"  
          },  
"max_score": 1,  
"hits": [  
   {  
         "index": "new_student",  
         "type": "_doc",  
         "id": "01",  
         "score": 1,  
         "_seq_no": 0,  
         "_primary_term": 1,  
         "found": true,  
         "_source": {  
  "name ": "Alen Paul",  
  "gender": "Male",  
"phone": "9876543210",  
"street": "Malen Park",   
"city": "California",   
"country": "United States",   
"zip": "94025"  
   }  
         }  
         {  
          "index": "new_student",  
         "type": "_doc",  
         "id": "01",  
         "score": 1,  
         "_seq_no": 0,  
         "_primary_term": 1,  
         "found": true,  
         "_source": {  
  "name ": "Alen Paul",  
  "gender": "Male",  
"phone": "9876543210",  
"street": "Malen Park",   
"city": "California",   
"country": "United States",   
"zip": "94025"  
}  
        }  
     ]  
  }  
}  


Using POST request Copy Code

POST  
_search   
{  
  "query ": {  
     "match_all": { }  
  }  
}  


Response The response will be like the GET technique demand. Thoroughly search in the screen capture beneath to see the result in elasticsearch module -



Presently, how about we move to the sorts of search API, which is grouped into three kinds: Multi-Index Multi-Type URI Search Multi-file is a most looked through API. Nonetheless, we will examine each search API exhaustively, alongside models. Prior to this, beneath are some solicitation boundaries which can be passed in a pursuit activity -

Parameter Description
Q It is utilized to determine the question string.
Sort This parameter is utilized to sort the reactions
Fields It brings the outcomes from specific fields.
Lenient This parameter is utilized to disregard the arrangement based mistake by setting up the worth to valid.
from This parameter assists with determining the beginning stage of the list. The default esteem is 0.
Size Specifies the quantity of hits to return. Naturally, the size is 10.
Timeout It restricts the search time.
terminate_after It is answerable for limiting the reaction to a particular number of records in every shard.


1. Multi list

Multi list API is a kind of search API that is utilized for looking through archives in the files. It look through every one of the reports in some particular files or all records. For some particular files search, indicate the lists name isolated by comma (,>
on which you need to play out the hunt activity For instance, we will look for every one of the understudies whose course is Mass Communication in two different files (understudy and new_student>
utilizing a solitary question. Method: GET API: _search Index: student, new_student Parameter: q


GET /student, new_student/  
_search?q=course:Mass Communication  
{  
  
}  


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

{   
"took": 102,  
"timed_out": false,  
"_shards": {  
"total": 2,  
"successful": 2,  
"skipped": 0,  
"failed": 0  
},  
"hits": {  
                   "total": {  
 "value": 2,  
 "relation": "eq"  
          },  
"max_score": 1.9333868,  
"hits": [  
   {  
         "index": "student",  
         "type": "_doc",  
         "id": "01",  
         "score": 1.9333868,  
         "_source": {  
 "name ": "Denial Parygen",  
 "dob": "07/Aug/1998",  
 "course": "Mass Communication",  
 "Addmission year": "2018",  
 "email": "denial@gmail.com",  
 "street": "3511 Rodney Street",   
 "state": "Missouri",   
 "country": "United States",   
 "zip": "62208"  
   }  
         },  
         {  
         "index": "new_student",  
         "type": "_doc",  
         "id": "03",  
         "score": 1.8132977,  
         "_source": {  
 "name ": "Lerry Page",  
 "dob": "07/Aug/1996",  
  "course": "Mass Communication",  
  "Addmission year": "2017",  
  "email": "Lerypg@gmail.com",  
  "street": "3511 Rodney Street",   
  "state": "Missouri",   
  "country": "United States",    
  "zip": "62208"  
}  
        }  
     ]  
  }  
}  




2. Multi Type

Multi type search API gives the office to look through every one of the reports in a specific record across numerous kinds or in a few explicit sorts. In this underneath model, we will execute the accompanying inquiry to look through the JSON reports from the "understudy" file under numerous types, where postal district is 30501. Technique: GET Programming interface: _search File: understudy Boundary: q Execute the accompanying question and search the reports.
Copy Code

GET /student, new_student/  
_search?q=zip:30501  
{ }  

Response In this following reaction, you can see that one JSON record in understudy list whose zip is 30501.

{   
"took": 511,  
"timed_out": false,  
"_shards": {  
"total": 1,  
"successful": 1,  
"skipped": 0,  
"failed": 0  
},  
"hits": {  
                   "total": {  
 "value": 1,  
 "relation": "eq"  
          },  
"max_score": 1.2039728,  
"hits": [  
   {  
         "index": "student",  
         "type": "_doc",  
         "id": "02",  
         "score": 1.2039728,  
         "_source": {  
 "name ": "Alex Paul",  
 "dob": "24/May/1994",  
 "course": "MSW",  
 "Addmission year": "2016",  
 "email": "alexl@gmail.com",  
 "street": "2253 Hillview Drive",   
 "state": "Georgia",   
 "country": "United States",   
 "zip": "30501"  
   }  
           }  
       ]  
   }  
}  




3. URI Search

URI search is a Uniform Resource Identifier Search. It permits us to pass numerous solicitation boundaries in a hunt activity utilizing a URI so the solicitation can be executed absolutely. At the point when you execute the pursuit question utilizing this mode, all the hunt choices are not uncovered. Notwithstanding, it is convenient for twist tests. Execute the accompanying question and search the archives in understudy list.

GET /student/  
_search  
{   
"query": {  
    "query_string": {  
      "query": "up"  
    }  
  }  
}  


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

{   
"took": 244,  
"timed_out": false,  
"_shards": {  
"total": 1,  
"successful": 1,  
"skipped": 0,  
"failed": 0  
},  
"hits": {  
                   "total": {  
 "value": 3,  
 "relation": "eq"  
          },  
"max_score": 1,  
"hits": [  
   {  
         "index": "new_student",  
         "type": "_doc",  
         "id": "03",  
         "score": 1,  
         "_source": {  
 "name ": "Lerry Page",  
 "dob": "07/Aug/1996",  
 "course": "Mass Communication",  
 "Addmission year": "2017",  
 "email": "Lerrypg@gmail.com",  
 "street": "3511 Rodney Street",   
 "state": "Missouri",   
 "country": "United States",   
 "zip": "62208"  
   }  
            },  
 {  
         "index": "new_student",  
         "type": "_doc",  
         "id": "02",  
         "score": 1,  
         "_source": {  
 "name ": "Jass Fernandiz",  
 "dob": "07/Aug/1996",  
 "course": "Bcom (H>
", "Addmission year": "2019", "email": "jassf@gmail.com", "street": "4225 Ersel Street", "state": "Texas", "country": "United States", "zip": "77063" } }, { "index": "new_student", "type": "_doc", "id": "01", "score": 1, "_source": { "name ": "Camila Cabello", "dob": "09/Feb/1999", "course": "MSW", "Addmission year": "2018", "email": "camilacab@gmail.com", "street": "729 Monroe Street", "state": "Hauston", "country": "United States", "zip": "77063" } }, ] } }






Get various records by id

We might in fact look for various reports by id in a solitary inquiry demand, alongside this multitude of search activities. For which we want to determine the id of each report that we are searching for. For Example We will utilize POST API to recover numerous archives from a list. It isn't sufficient; we additionally need to affix _mget in demand string and characterize archive id question space to bring numerous records.

OST student/ _mget/  
{  
  "docs": [  
   {    
       "_index": "new_student", "_type": "_doc", "_id": "02"  
   },  
   {    
       "_index": "new_student", "_type": "_doc", "_id": "04"  
   }  
  ]  
} 



Response

You will get the output like the below response.

"docs": [  
{   
"index": "student",  
"type": "_doc",  
"id": "02",  
"version": 1,  
"_seq_no": 0,  
"_primary_term": 1,  
"found": true,  
      "source": {  
 "name": "Alen Paul",  
 "gender": "Male",  
 "phone": "9876543210",   
 "street": "Malen Park",  
 "city": "California",  
 "country": "United State",  
 "zip": "94025",  
                  }  
}  
"index": "student",  
"type": "_doc",  
"id": "04",  
"version": 1,  
"_seq_no": 3,  
"_primary_term": 1,  
"found": true,  
      "source": {  
 "name": "Stan Lee",  
 "gender": "Male",  
 "phone": "9876543211",   
 "street": "New Rochelle",  
 "city": "New York",  
 "country": "United State",  
 "zip": "10029",  
                  }  
}  
      ]  
}