ES bulk operation bulk

1 Introduction

Mainly introduce bulk operation, use postman to request, the prefix address of the interface request is unified as the elasticsearch deployment IP address + port number (for example, http://192.168.51.4:9200).

2 Bulk operation bulk

2.1 Basic grammar

The bulk operation is different from the normal request format in the past. Instructions are generally on one line, separated by newlines, and are not in standard JSON format. This requires attention.

{
    
    action: {
    
    metadata}} \n
{
    
    request body} \n
{
    
    action: {
    
    metadata}} \n
{
    
    request body} \n
...
  • {action: {metadata}} Represents the type of batch operation, which can be add, delete and modify
  • \n It is a specification that must be filled in at the end of each line, and each line including the last line needs to be written for the analysis of es
  • {request body} It is the request body, which is required for add and modify operations, but not for delete operations

2.2 Types of batch operations

action Must be one of the following options:

  • create: If the document does not exist, then create it. If it exists, an error will be reported. An exception error will not affect other operations
  • index: Create a new document or replace an existing document
  • update: Partially update a document
  • delete: delete a document

metadataThe need to specify the document to be operated _index, _typeand _id, at the same time _index, _typecan also be specified in the url

2.3 Operation rehearsal

Official address: https://www.elastic.co/guide/cn/elasticsearch/guide/current/bulk.html

  • create new document data, specify index and type in metadata

    POST /_bulk
    
    {"create":{"_index": "test","_type": "_doc","_id": "2001"}}
    {"id": "2001","nickname":"test-2001"}
    {"create":{"_index": "test","_type": "_doc","_id": "2002"}}
    {"id": "2002","nickname":"test-2002"}
    {"create":{"_index": "test","_type": "_doc","_id": "2003"}}
    {"id": "2003","nickname":"test-2003"}
    
  • create Create an existing id document, specify the index and type in the url

    POST /test/_doc/_bulk
    
    {"create":{"_id": "2004"}}
    {"id": "2004","nickname":"test-2004"}
    {"create":{"_id": "2005"}}
    {"id": "2005","nickname":"test-2005"}
    {"create":{"_id": "2006"}}
    {"id": "2006","nickname":"test-2006"}
    
  • index is created, the existing document ID will be overwritten, and the ID will be added if it does not exist

    POST /test/_doc/_bulk
    
    {"index":{"_id": "2005"}}
    {"id": "2005","nickname":"test-index"}
    {"index":{"_id": "2006"}}
    {"id": "2006","nickname":"test-2006"}
    {"index":{"_id": "2007"}}
    {"id": "2007","nickname":"test-2007"}
    
  • update Update some document information

    POST /test/_doc/_bulk
    
    {"update":{"_id": "2004"}}
    {"doc": {"id": "2004"}}
    {"update":{"_id": "2005"}}
    {"doc": {"nickname":"test-update"}}
    
  • delete batch delete

    POST /test/_doc/_bulk
    
    {"delete":{"_id": "2004"}}
    {"delete":{"_id": "2005"}}
    
  • Comprehensive batch operation

    POST /test/_doc/_bulk
    
    {"create":{"_index": "test","_type": "_doc","_id": "2009"}}
    {"id": "2009","nickname":"test-2009"}
    {"update":{"_id": "2005"}}
    {"doc": {"nickname":"test-update"}}
    {"id": "2001","nickname":"test-2001"}
    {"delete":{"_id": "2004"}}
    
    

3 Related information

  • The blog post is not easy, everyone who has worked so hard to pay attention and praise, thank you

Guess you like

Origin blog.csdn.net/qq_15769939/article/details/114818251