elasticseach简单的增、删、查、改操作

我使用的是kibana内置的开发者工具(Dev Tools)

一、添加数据

输入如下语句,点击运行

PUT person/doc/grade1
{
  "name":"code",
  "grade":90
}

可以看到输出

{
  "_index" : "person",
  "_type" : "doc",
  "_id" : "grade1",
  "_version" : 5,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 9,
  "_primary_term" : 1
}

二、查询数据

为了方便进行查询演示,我们再添加2条数据

PUT person/doc/grade2
{
  "name":"teste",
  "grade":80
}
PUT person/doc/grade3
{
  "name":"product",
  "grade":30
}

1. 根据索引(index)、类型(type)、id直接进行查看

GET person/doc/grade1

可以看到查询结果为:

{
  "_index" : "person",
  "_type" : "doc",
  "_id" : "grade1",
  "_version" : 5,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 9,
  "_primary_term" : 1
}

2. 根据指定字段的值查询,有两种方式

我们以查询分数为80的记录为例进行查询

(1)直接拼接参数

GET person/doc/_search?q=grade:80

我们可以看到查询结果为:

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "person",
        "_type" : "doc",
        "_id" : "grade2",
        "_score" : 1.0,
        "_source" : {
          "name" : "teste",
          "grade" : 80
        }
      }
    ]
  }
}

(2)结构化查询

GET person/doc/_search
{
  "query":{
    "match": {
      "grade": 80
    }
  }
}

查询结果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "person",
        "_type" : "doc",
        "_id" : "grade2",
        "_score" : 1.0,
        "_source" : {
          "name" : "teste",
          "grade" : 80
        }
      }
    ]
  }
}

三、更新数据

修改第一条数据的grade字段的值为100

POST person/doc/grade1/_update
{
  "doc":{
    "grade":100
  }
}

结果显示成功:

{
  "_index" : "person",
  "_type" : "doc",
  "_id" : "grade1",
  "_version" : 6,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 10,
  "_primary_term" : 1
}

四、删除数据

DELETE person/doc/grade1

结果显示成功:

{
  "_index" : "person",
  "_type" : "doc",
  "_id" : "grade1",
  "_version" : 7,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 11,
  "_primary_term" : 1
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值