Basic operations of ES documents

1 Introduction

Mainly introduce the basic API operation of index request, use postman to request, the prefix address of the interface request is unified as elasticsearch deployment IP address + port number (for example, http://192.168.51.4:9200. The following provides the interface address JSON export for postman test file:

Postman interface collection download address https://download.csdn.net/download/qq_15769939/15492332

2 Basic document operations

First, create an index (doc_api_demo) to test the document API.

2.1 Create a document

Request method interface address Remarks
POST /doc_api_demo/_doc/1 doc_api_demo: Index name
1: ID of the index in ES

传递的JSON参数

{
    
    
    "id": 1001,
    "name": "auskat-1",
    "desc": "tic is a good man 1",
    "create_date": "2019-12-24"
}
{
    
    
    "id": 1002,
    "name": "auskat-2",
    "desc": "tic 是个好人",
    "create_date": "2019-12-25"
}
{
    
    
    "id": 1003,
    "name": "auskat-3",
    "desc": "tic 要成为一个好人",
    "create_date": "2020-12-26"
}
{
    
    
    "id": 1004,
    "name": "auskat-5",
    "desc": "tic 真是一个好人",
    "create_date": "2020-12-27"
}

请求结果

{
    
    
    "_index": "doc_api_demo",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
    
    
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

This ID attribute is not the ID of this record, but the ID of this index record. If not set, a unique string ID will be generated by default. In actual use, it is generally consistent with the unique ID of the data.

  • If the index does not create mappings manually, when inserting document data, the attribute type will be automatically set according to the document type. This is the dynamic mapping of es, which helps us to establish the relevant configuration information of the data structure in the index library.
  • "Fileds":{"type":"keyword"} Set multiple index modes for a field, use text type for full-text search, and keyword type for aggregation and sorting.
  • "Ignore_above": 256 sets the maximum length of the field index and storage, if it exceeds, it will be ignored.

2.2 Document modification

2.2.1 Partial modification

Request method interface address Remarks
POST /doc_api_demo/_doc/1004/_update doc_api_demo: Index name
1004: ID of the index

传递JSON数据

{
    
    
    "doc": {
    
    
        "name": "我是tic 我要单独改名为 ttd"
    }
}

请求结果

{
    
    
    "_index": "doc_api_demo",
    "_type": "_doc",
    "_id": "1004",
    "_version": 3,
    "result": "updated",
    "_shards": {
    
    
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 6,
    "_primary_term": 1
}

2.2.2 Full replacement

Request method interface address Remarks
PUT /doc_api_demo/_doc/1004 doc_api_demo: Index name
1004: ID of the index

传递JSON数据

{
    
    
    "id": 1004,
    "name": "tic tic tic",
    "desc": "tic 真的要变成一个好人了",
    "create_date": "2020-12-28"
}

请求结果

{
    
    
    "_index": "doc_api_demo",
    "_type": "_doc",
    "_id": "1004",
    "_version": 4,
    "result": "updated",
    "_shards": {
    
    
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 7,
    "_primary_term": 1
}

_version represents the version number of the document, and cumulative changes will occur if it is not modified once.

2.3 Document query

2.3.1 Single document query

Request method interface address Remarks
GET /doc_api_demo/_doc/1004 doc_api_demo: Index name
1004: ID of the index

请求结果

{
    
    
    "_index": "doc_api_demo",
    "_type": "_doc",
    "_id": "1004",
    "_version": 4,
    "_seq_no": 7,
    "_primary_term": 1,
    "found": true,
    "_source": {
    
    
        "id": 1004,
        "name": "tic tic tic",
        "desc": "tic 真的要变成一个好人了",
        "create_date": "2020-12-28"
    }
}

2.3.2 Query full document data

Request method interface address Remarks
GET /doc_api_demo/_doc/_search doc_api_demo: Index name
1004: ID of the index

请求结果

{
    
    
    "took": 7,
    "timed_out": false,
    "_shards": {
    
    
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
    
    
        "total": {
    
    
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
    
    
                "_index": "my_doc",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
    
    
                    "doc": {
    
    
                        "name": "我是阿苏卡DDD"
                    },
                    "name": "我是阿苏卡CC"
                }
            },
            {
    
    
                "_index": "my_doc",
                "_type": "_doc",
                "_id": "okcQzncBZX7VGcys4RIc",
                "_score": 1.0,
                "_source": {
    
    
                    "id": 1009,
                    "name": "auskat-1",
                    "desc": "tic is a good man 1",
                    "create_date": "2019-12-24"
                }
            },
            {
    
    
                "_index": "my_doc",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
    
    
                    "id": 1003,
                    "name": "auskat-3",
                    "desc": "tic is a good man 3",
                    "create_date": "2019-12-24"
                }
            },
            {
    
    
                "_index": "my_doc",
                "_type": "_doc",
                "_id": "6",
                "_score": 1.0,
                "_source": {
    
    
                    "name": "auskat-8",
                    "desc": "tic is a good man 8",
                    "create_date": "2019-12-24"
                }
            }
        ]
    }
}

2.3.3 Specify the field to be queried

Request method interface address Remarks
GET /doc_api_demo/_doc/1004?_source=id,name doc_api_demo: Index name
1004: ID of the index
GET /doc_api_demo/_doc/_search?_source=id,name doc_api_demo: index name

请求结果

{
    
    
    "took": 7,
    "timed_out": false,
    "_shards": {
    
    
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
    
    
        "total": {
    
    
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
    
    
                "_index": "my_doc",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
    
    
                    "name": "我是阿苏卡CC"
                }
            },
            {
    
    
                "_index": "my_doc",
                "_type": "_doc",
                "_id": "okcQzncBZX7VGcys4RIc",
                "_score": 1.0,
                "_source": {
    
    
                    "name": "auskat-1",
                    "id": 1009
                }
            },
            {
    
    
                "_index": "my_doc",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
    
    
                    "name": "auskat-3",
                    "id": 1003
                }
            },
            {
    
    
                "_index": "my_doc",
                "_type": "_doc",
                "_id": "6",
                "_score": 1.0,
                "_source": {
    
    
                    "name": "auskat-8"
                }
            }
        ]
    }
}
  • _index: The index to which the document data belongs can be understood as a table in the database.
  • _type: Which type of document data belongs to, and the new version uses __doc
  • _id: The unique identifier of the document data, similar to the primary key of the database. Can be automatically generated or manually specified
  • _score:Query relevance, whether it fits the user’s match, the higher the score, the higher the user’s search experience
  • _version:version number
  • _source: Document data, JSON format

2.3.4 Determine whether the document exists

Request method interface address Remarks
HEAD /doc_api_demo/_doc/1004 doc_api_demo: Index name
1004: ID of the index

请求结果

{
    
    
    "_index": "doc_api_demo",
    "_type": "_doc",
    "_id": "1004",
    "_version": 4,
    "_seq_no": 7,
    "_primary_term": 1,
    "found": true,
    "_source": {
    
    
        "id": 1004,
        "name": "tic tic tic",
        "desc": "tic 真的要变成一个好人了",
        "create_date": "2020-12-28"
    }
}

2.4 Document deletion

Request method interface address Remarks
DELETE /index_api_demo/1004 doc_api_demo: Index name
1004: ID of the index

返回结果

{
    
    
    "_index": "doc_api_demo",
    "_type": "_doc",
    "_id": "4",
    "_version": 2,
    "result": "deleted",
    "_shards": {
    
    
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}

Document deletion is not deleted immediately. Documents are still stored on the disk. The index grows more and more, and only those that have been marked for deletion will be cleaned up and removed from the disk.

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/114382136