elasticsearch会自动建立index和type,不需要提前创建,而且elasticsearch默认会对document的每个filed建立倒排索引,方便搜索;
PUT /index/type/id
{
"filed":"value"
}
#操作例子
PUT /accounting_tools/voucher_detail/111
{
"subjectId" : "123",
"subjectCode" : "1001",
"subjectName" : "库存现金",
"period" : "201901",
"amount" : 100.0,
"tags": ["资产", "负债", "收入", "费用"],
"auxiliary": {
"auxId":"123",
"auxCode":"001",
"auxName":"客户"
}
}
#操作结果
{
"_index" : "accounting_tools",
"_type" : "voucher_detail",
"_id" : "111",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 8,
"_primary_term" : 1
}
total:指的是预计写入两条数据;successful:指的是写入成功的数据条数;为什么明明写入的是两条数据,怎么只成功了一条呢?那是因为每个索引至少要写入一条数据 primary shard和replica shard中,这也就能解释为什么total为2了,至于只写入成功一条数据,那是因为我的elasticsearch是一个节点的单机版,而primary shard 和 replica shard 由于要容错的原因,必须在不同的机器上,所以只往primary shard写入成功了一条数据,写入replica shard失败了。
GET /index/type/id
#操作例子
GET /accounting_tools/voucher_detail/111
#操作结果
{
"_index" : "accounting_tools",
"_type" : "voucher_detail",
"_id" : "111",
"_version" : 5,
"found" : true,
"_source" : {
"subjectId" : "123",
"subjectCode" : "1001",
"subjectName" : "库存现金",
"period" : "201901",
"amount" : 100.0,
"tags" : [
"资产",
"负债",
"收入",
"费用"
],
"auxiliary" : {
"auxId" : "123",
"auxCode" : "001",
"auxName" : "客户"
}
}
}
必须带上所有filed,否者会造成部分数据filed丢失/覆盖
PUT /index/type/id
{
"filed":"value"
}
#操作例子
PUT /accounting_tools/voucher_detail/111
{
"subjectId" : "123",
"subjectCode" : "1001",
"subjectName" : "库存现金",
"period" : "201901",
"amount" : 100.0,
"tags": ["资产", "负债", "收入", "费用"],
"auxiliary": {
"auxId":"123",
"auxCode":"001",
"auxName":"客户"
}
}
#操作结果
{
"_index" : "accounting_tools",
"_type" : "voucher_detail",
"_id" : "111",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
POST /index/type/id/_update
{
"doc": {
"filed":"value"
}
}
#操作例子
POST /accounting_tools/voucher_detail/111/_update
{
"doc": {
"amount":105.00
}
}
#操作结果
{
"_index" : "accounting_tools",
"_type" : "voucher_detail",
"_id" : "111",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2
"_primary_term" : 1
}
DELETE /index/type/id
#操作例子
DELETE /accounting_tools/voucher_detail/101010
#操作结果
{
"_index" : "accounting_tools",
"_type" : "voucher_detail",
"_id" : "111",
"_version" : 2,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 9,
"_primary_term" : 1
}