...

Search Engine - Elastic Search

Back to Course

Lesson Description


Lession - #132 ES Document APIs


Elasticsearch Document APIs

As we previously examined in our initial instructional exercise that Elasticsearch is a record situated data set, which stores all information in archive structure. Programming interface utilizes JSON report organization to store information. Record APIs are those APIs that play out the activity at the archive level. Thus, to play out some activity at archive level, you need to utilize the report show up. With the assistance of archive API, you can do that well overall. In Elasticsearch ,Document API is grouped into two classes that are single report API what's more, multi-record API . We will examine every API exhaustively with models -

1. Single Document API

These APIs are helpful if you have any desire to perform procedure on a solitary record rather than a gathering of reports. In this way, you can utilize these single archive API when you need to perform procedure on a solitary report. Single Document APIs is additionally arranged into four APIs, which are recorded beneath: Index API Get API Update API Delete API Right off the bat, make a file to utilize this large number of API. Utilize the accompanying order -

PUT student  




Index API

This API assists with adding a report in a predefined record when a solicitation is made. It likewise permits us to refresh the record assuming that the archive as of now exists. Record API is utilized for the full substitution of a current archive. File API permits us to add and refresh the record. We need to utilize the POST technique to add the information where we want to indicate the report id. Model Utilize the accompanying code to add information in the file - Copy Code

POST student/_doc/02/  
{  
"name ": "Stan Lee",  
"gender": "Male",  
"phone": "9876543211",  
"street": " New Rochelle",   
"city": "New York",   
"country": "United States",   
"zip": "10029"  
}  

Here POST is demand strategy and _doc is utilized for archive type to add information in the list.

Response

The information gave in demand body is effectively added in the event that you get the accompanying result accordingly body.

{   
"index": "student",  
"type": "_doc",  
"id": "02",  
"version": 1,  
"result": "created",  
"_shards": {  
"total":2,  
"successful":1,  
"failed": 0  
}  
"_seq_no": 0,  
"_primary_term": 1  
}  




Get API

Get API assists with recovering or read the information put away in a list. This API is answerable for recovering the predefined JSON report from a list. Model In this model, we will utilize GET API to peruse a record. Make another solicitation and determine the list name and report id. It will bring and show the outcome accordingly body. List name = understudy Record id = 02


GET student/_doc/02/  
Select GET method and append the remaining code in the request string.

Response


{   
"index": "student",  
"type": "_doc",  
"id": "02",  
"version": 1,  
"_seq_no": 0,  
"_primary_term": 1,  
"found": true,  
"_source": {  
"name ": "Stan Lee",  
"gender": "Male",  
"phone": "9876543211",  
"street": "New Rochelle",   
"city": "New York",   
"country": "United States",   
"zip": "10029"  
}  
}  




Update API

The Update API assists with refreshing a current report, which utilizations script and forming. It permits passing a halfway archive that converges to the current record. The _update is utilized while making a solicitation. Script watchword is utilized to make inquiry demand for playing out this activity. It can change, update, or erase the report. You can likewise involve doc instead of content watchword to refresh the report. Forming is utilized to guarantee that no update has happened during the get and reindex. Recall that it would refresh the single report just, not all. It acknowledges to two boundaries record name and archive id, i.e., and . Model Elasticsearch gives _update API and POST demand technique to refresh the records. In this model, we will supplant the hurdle from 10029 to 94003. Technique: POST Programming interface: _update Record Name: understudy Type: _doc Information for update: postal division Execute the accompanying question and update the archive.

POST student/_doc/02/  
_update  
{  
"doc": {  
"zip": "94003"  
}  
}   


Response

If you get the same output, the document is updated successfully.

{   
"index": "student",  
"type": "_doc",  
"id": "02",  
"version": 2,  
"result": "updated",  
"shards": {  
"total": 2,  
"successful": 1,  
"failed": 0  
}  
"_seq_no": 1,  
"_primary_term": 1  
}  




Delete API

You can utilize this API to erase a specific list, archive, or planning. Send a HTTP DELETE solicitation to Elasticsearch for executing erase activity. It doesn't need a huge assertion to make an erase inquiry. Utilize the _delete API and determine the record name to erase. Model We will utilize DELETE API to erase an archive put away in a list. Along these lines, just set the DELETE the solicitation technique and give file name and archive id, which you need to erase. Copy Code

DELETE student/_doc/02/  


Response

If you get the below output, document 02 is deleted successfully.

{   
"index": "student",  
"type": "_doc",  
"id": "02",  
"version": 3,  
"result": "deleted",  
"shards": {  
"total": 2,  
"successful": 1,  
"failed": 0  
}  
"_seq_no": 2,  
"_primary_term": 1  
}  
Look at the below screenshot to see the result on browser -




2. Multi-Document API

Not at all like a solitary report API, you can utilize the multi-archive APIs for questioning across different records. For instance - erase or refresh information in mass, and reindex, and so on. Multi-report APIs is additionally arranged into five APIs, which are recorded beneath: Multi Get API Mass API Erase By Query API Update By Query API Reindex API Multi Get API It recovers various reports utilizing _docs exhibit of file. It offers a similar usefulness, like the Single Get API. The main distinction is that this API returns more than one report. To remove reports, we utilize the doc exhibit for determining the file, type, and id of that multitude of archives. We really want to utilize POST API to recover various archives from a record. It isn't sufficient; we likewise need to add _mget in demand string and characterize report id inquiry space to get numerous records. Recall that more than three or four records should be included a list (data set>
to play out this activity. See the model beneath -

OST student/_mget/  
{  
  "docs": [  
   {    
       "_index": "student", "_type": "_doc", "_id": "01"  
   },  
   {    
       "_index": "student", "_type": "_doc", "_id": "03"  
   }  
  ]  
}  


Response


"docs": [  
{   
"index": "student",  
"type": "_doc",  
"id": "01",  
"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": "03",  
"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",  
                  }  
}  
      ]  
}  

Look at the below screenshot to see the result on browser -



BulkAPI

Mass API assists with transferring or erase the JSON object in mass. To do this, make different file/erase tasks in a solitary solicitation. Mass API is like the Get API and gives any remaining usefulness same as it. Add the "_bulk" catchphrase to call this API. It is utilized to perform erase/update/record tasks in mass. The following are a few instances of mass orders:

{ "delete": { "_index" : "student", "_type": "_docs", "_id": "02"} }  
{ "update": { "_index" : "student", "_type": "_docs", "_id": "04"} } {"doc": {"field3": "value3"} } 


Delete By Query API

The _delete_by_query demand is utilized to erase every single matching report. Whenever this solicitation processes, Elasticsearch (ES>
play out different inquiries to track down all the report to erase. Model Execute the accompanying question and update the file. This will erase that large number of archives that have city = New York.

Copy code

POST student/_doc/  
_delete_by_query  
{  
  "query": {  
    "match": {  
        "city": "New York"  
    }  
  }  


Response

If you get the same output, the document is deleted successfully.

{   
"took": 9376,  
"timed_out": false,  
"total": 3,  
"deleted": 3,  
"batches": 1,  
"version_conflicts": 0,  
"noops": 0,  
"retries": {  
"bulk": 0,  
"search": 0  
},  
"throttled_millis": 0,  
"request_per_second": -1,  
"throttled_until_millis": 0,  
"faliures": [ ]  
}  




On the off chance that assuming we use "script": {} instead of field_name: esteem (i.e., "city": "New York">
, it will erase all the archive present in understudy list. The solicitation question will be like -

{  
  "query": {  
    "match": {  
        "match_all": {}  
    }  
  }  
}  


Update By Query API

Update By Query API (_update_by_query>
plays out a report on each archive present in the record without changing the source. The content watchword is utilized to make an inquiry demand for playing out this activity. Method: POST API: _update_by_query Index Name: student Type: _doc Data for update: zip code Execute the following query and update the documents.{ "took": 346, "timed_out": false, "total": 3, "updated": 3, "deleted": 0, "batches": 1, "version_conflicts": 0, "noops": 0, "retries": { "bulk": 0, "search": 0 }, "throttled_millis": 0, "request_per_second": -1, "throttled_until_millis": 0, "faliures": [ ] } { "took": 346, "timed_out": false, "total": 3, "updated": 3, "deleted": 0, "batches": 1, "version_conflicts": 0, "noops": 0, "retries": { "bulk": 0, "search": 0 }, "throttled_millis": 0, "request_per_second": -1, "throttled_until_millis": 0, "faliures": [ ] }

Reindex API

This API is fundamentally involved when we want a record in another list. We duplicate the record information starting with one file then onto the next utilizing _reindex API. With the assistance of this API, we can without much of a stretch to duplicate an archive from a list and make a copy duplicate of it to another file. The following are a few significant focuses about reindex API: Reindex API separates the report from the source list and records that archive to the objective file. _source should be empowered for all reports present in the source file. Prior to calling _reindex, you should set up the objective file first. Model Execute the accompanying question to reindex the report starting with one list then onto the next. Technique: POST Programming interface: _reindex File Name: understudy New file: new_student


Copy Code

POST   
_reindex  
{  
"source": {  
    "index": "student"  
  },  
  "dest": {  
        "index": "new_student"  
  }  
}  


Response

If you get the same output, the document is copied successfully.

{   
"took": 1748,  
"timed_out": false,  
"total": 2,  
"updated": 0,  
"created": 2,  
"deleted": 0,  
"batches": 1,  
"version_conflicts": 0,  
"noops": 0,  
"retries": {  
"bulk": 0,  
"search": 0  
},  
"throttled_millis": 0,  
"request_per_second": -1,  
"throttled_until_millis": 0,  
"faliures": [ ]  
}