在 Elasticsearch 中插入数据时如何提供我自己的 _id?

How to give my own _id while inserting data in Elasticsearch?

我有一个示例数据库如下:

SNO Name Address
99123 Mike Texas
88124 Tom California

我想将我的 SNO 保留在 elastic search _id 中,以便更容易根据我的 SNO 更新文档。

Python 创建索引的代码:

    abc = {
    "settings": {
        "number_of_shards": 2,
        "number_of_replicas": 2
    }
}

es.indices.create(index='test',body = abc)

I fetched data from postman as below:

{
"_index": "test",
"_id": "13",
"_data": {
    "FirstName": "Sample4",
    "LastName": "ABCDEFG",
    "Designation": "ABCDEF",
    "Salary": "99",
    "DateOfJoining": "2020-05-05",
    "Address": "ABCDE",
    "Gender": "ABCDE",
    "Age": "21",
    "MaritalStatus": "ABCDE",
    "Interests": "ABCDEF",
    "timestamp": "2020-05-05T14:42:46.394115",
    "country": "Nepal"
}

}

And Insert code in python is below:

req_JSON = request.json 
input_index = req_JSON['_index']
input_id = req_JSON['_id']
input_data = req_JSON['_data']    
doc = input_data 
res = es.index(index=input_index, body=doc) 

I thought _id will remain the same as what I had given but it generated the auto _id.

你可以简单地做到这一点like this:

res = es.index(index=input_index, body=doc, id=input_id) 
                                                ^
                                                |
                                             add this