1.document的id
1)根据应用情况来说,是否满足是否指定document id的手动指定,一般情况,是从某些其他的系统中,导入一些数据到es时,会采用这种方式,就是使用系统中已有数据的唯一标识,作为es中document的id,举个例子,比如说我们现在在开发一个电商网站,做搜索功能,或者oa系统的员工查询,这个时候,数据首先会在网站的系统中的数据库中,会先有一份,此时就肯定会有一个数据库的primary key(自增长,uuid,业务id),此时就比较适合采用数据在数据库中已有的primary key
语法:
PUT /index/type/id
{
"xxx":"xxx"
}
例子:
PUT /ecommerce/product/4
{
"name":"yayale yagao",
"desc":"gaoxiao meibai",
"price":100,
"producer":"yayale producer",
"tags":[
"fangzhu","meibai","qingxin"
]
}
执行结果:
{
"_index": "ecommerce",
"_type": "product",
"_id": "4",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 3
}
2)如果数据主要存储到es中,那么就可以使用document中的自动生成的id
语法:不写id
POST /index/type
{
"xxx":"xxx"
}
例子:
POST /test_index/salesOrder/
{
"OrderProductIds":"5"
}
执行结果:生成的自动id是20的guid
{
"_index": "test_index",
"_type": "salesOrder",
"_id": "1l3zmmABEP2XuYqp4PmX",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
例子2:
POST /ecommerce/product
{
"name":"heiren yagao",
"desc":"gaoxiao meibai",
"price":100,
"producer":"heiren producer",
"tags":[
"fangzhu","meibai","qingxin"
]
}
执行结果:
{
"_index": "ecommerce",
"_type": "product",
"_id": "1131mmABEP2XuYqpoflk",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 3
}
如下链接只能在低版本使用
elasticsearch指定其他字段为主键_id字段
elasticsearch 文档唯一性由多个字段共同确定(类似联合主键),并发下如何确保唯一