三、ES文档操做命令(一)

创建索引

以student为例子,创建索引请求体

PUT /student
{
  "settings": {
    "number_of_shards": "5", 
    "number_of_replicas": "1"
  },
  "mappings": {
    "properties": {
      "student-name": {
        "type": "keyword"
      },
      "student-provice": {
        "type": "keyword"
      },
      "student-city": {
        "type": "keyword"
      },
      "student-gender": {
        "type": "keyword"
      },
      "student-address": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "student-age": {
        "type": "integer"
      },
      "student-total-score": {
        "type": "double"
      },
      "student-birthday": {
        "type": "keyword"
      },
      "student-hobby": {
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }
}

响应体

{
    "acknowledged": true,//响应结果
    "shards_acknowledged": true,//分片结果
    "index": "student"//索引名称
}

参数说明看官方文档 链接: 相关的属性说明 ,在实际开发中,用到的类型并不多,会用了解原理,用的时候查看文档就行了

文档操作

这里只有DSL语句,后面通过跟着黑马公开的酒店Demo视频更新RestAPI的java代码

新增文档(指定ID)

这里插入六条,为后面的操作,做好数据

PUT /student/_doc/1
{
	"student-provice": "山东",
	"student-city": "菏泽",
	"student-name": "张海剑",
	"student-gender": "男",
	"student-address": "历下区111号",
	"student-age": 11,
	"student-total-score": 540.5,
	"student-birthday": "2003-08-09",
	"student-hobby": "唱歌、游泳、跑步、骑行、爬山、篮球、乒乓球、足球、排球、跳舞、下棋、军事"
}

PUT /student/_doc/2
{
	"student-provice": "山东",
	"student-city": "威海",
	"student-name": "张小明",
	"student-gender": "男",
	"student-address": "历城区10001号",
	"student-age": 12,
	"student-total-score": 583.5,
	"student-birthday": "2003-08-09",
	"student-hobby": "唱歌、游泳、跑步"
}

PUT /student/_doc/3
{
	"student-provice": "山东",
	"student-city": "济宁",
	"student-name": "冯程程",
	"student-gender": "女",
	"student-address": "高新区10689号",
	"student-age": 10,
	"student-total-score": 603.5,
	"student-birthday": "2003-08-09",
	"student-hobby": "唱歌、跳舞、下棋"
}

PUT /student/_doc/4
{
	"student-provice": "山东",
	"student-city": "济南",
	"student-name": "程诚",
	"student-gender": "男",
	"student-address": "历下区2869号",
	"student-age": 11,
	"student-total-score": 559.5,
	"student-birthday": "2004-06-09",
	"student-hobby": "唱歌、游泳、跑步、骑行、爬山、篮球、乒乓球、足球、排球、跳舞、下棋、军事"
}

PUT /student/_doc/5
{
	"student-provice": "山东",
	"student-city": "聊城",
	"student-name": "刘英杰",
	"student-gender": "男",
	"student-address": "平阴县10001号",
	"student-age": 14,
	"student-total-score": 483.5,
	"student-birthday": "2003-02-13",
	"student-hobby": "唱歌、游泳、跑步、爬山、篮球、乒乓球"
}

PUT /student/_doc/6
{
	"student-provice": "山东",
	"student-city": "威海",
	"student-name": "宋丽君",
	"student-gender": "女",
	"student-address": "槐荫区10989号",
	"student-age": 12,
	"student-total-score": 596.5,
	"student-birthday": "2003-11-22",
	"student-hobby": "唱歌、跳舞、下棋"
}

新增文档(随机ID)

POST /student/_doc
{
	"student-provice": "山东",
	"student-city": "威海11111",
	"student-name": "hahahah",
	"student-gender": "女",
	"student-address": "槐荫区10989号",
	"student-age": 12,
	"student-total-score": 596.5,
	"student-birthday": "2003-11-22",
	"student-hobby": "唱歌、跳舞、下棋"
}

如下响应的结果

{
  "_index": "student",
  "_id": "3q1TpI8Bwik6Dln3Ik39",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

根据id查询文档

GET /student/_doc/1

返回结果如下

{
  "_index": "student",
  "_id": "1",
  "_version": 1,
  "_seq_no": 0,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "student-provice": "山东",
    "student-city": "菏泽",
    "student-name": "张海剑",
    "student-gender": "男",
    "student-address": "历下区111号",
    "student-age": 11,
    "student-total-score": 540.5,
    "student-birthday": "2003-08-09",
    "student-hobby": "唱歌、游泳、跑步、骑行、爬山、篮球、乒乓球、足球、排球、跳舞、下棋、军事"
  }
}

根据id删除文档

DELETE /student/_doc/3q1TpI8Bwik6Dln3Ik39

返回结果如下

{
  "_index": "student",
  "_id": "3q1TpI8Bwik6Dln3Ik39",
  "_version": 2,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 1,
  "_primary_term": 1
}

修改文档(有ID无数据新增,有id有数据修改)

POST /student/_doc/1
{
	"student-provice": "山东",
	"student-city": "菏泽",
	"student-name": "张海剑",
	"student-gender": "男",
	"student-address": "历下区111号",
	"student-age": 11,
	"student-total-score": 540.5,
	"student-birthday": "2003-08-12",
	"student-hobby": "唱歌、游泳、跑步、骑行、爬山、篮球、乒乓球、足球、排球、跳舞、下棋、军事"
}

返回结果如下,版本号变成了2

{
  "_index": "student",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 1,
  "_primary_term": 1
}

修改文档(局部更新)

POST /student/_update/1
{
  "doc": {
    "student-birthday": "2003-08-25"
  }
}

返回结果如下,版本号变成了3

{
  "_index": "student",
  "_id": "1",
  "_version": 3,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 2,
  "_primary_term": 1
}

查询所有文档(无参数)

GET /student/_search

查询所有文档(有参数)match_all

GET /student/_search

URL带参数查询

GET /student/_search?q=student-age:11
  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Grain322

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值