ElasticSearch的CRUD操作

ElasticSearch的CRUD操作

 

1.CREATE

elasticsearch会自动建立index和type,不需要提前创建,而且elasticsearch默认会对document的每个filed建立倒排索引,方便搜索;

  • 操作命令
PUT /index/type/id
{
  "filed":"value"
}
  • 1
  • 2
  • 3
  • 4
  • 操作示例
#操作例子
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失败了。

2.READ

  • 操作命令
GET /index/type/id
  • 1
  • 操作示例
#操作例子
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" : "客户"
    }
  }
}

3.UPDATE

3.1.替换

必须带上所有filed,否者会造成部分数据filed丢失/覆盖

  • 操作指令
PUT /index/type/id
{
  "filed":"value"
}
  • 1
  • 2
  • 3
  • 4
  • 操作示例
#操作例子
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
}

3.2.更新

  • 操作指令
POST /index/type/id/_update
{
  "doc": {
 	 "filed":"value"
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 操作示例
#操作例子
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
}

4.DELETE

  • 操作示例
DELETE /index/type/id
  • 1
  • 操作示例
#操作例子
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
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值