RestFUL 风格

总览:

method URL格式 描述
PUT localhost:9200/索引名/文档类型/文档ID 创建文档(指定 ID)
POST localhost:9200/索引名/文档类型 创建文档(随机 ID)
POST localhost:9200/索引名/文档类型/文档ID/_update 修改文档(部分修改)
DELETE localhost:9200/索引名/文档类型/文档ID 删除指定的文档
DELETE localhost:9200/索引名 删除指定的索引
GET localhost:9200/索引名/文档类型/文档ID 通过文档 ID 查询文档

关于索引的基本操作

添加索引

以添加文档的形式创建索引:
03-RestFul 风格 - 图1
image.png

注:指定文档类型的用法已经过时,现在使用 _doc 代替文档类型。

此时查看创建的索引,发现文档的各属性都有了类型。自动类型判断?

03-RestFul 风格 - 图3image.png

创建索引,同时指定文档中属性的类型:

映射类型文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html
03-RestFul 风格 - 图5
image.png

创建索引时如果添加文档类型则 出现错误,歪?

  1. PUT /idx2/_doc
  2. {
  3. "mappings": {
  4. "properties": {
  5. "name": {
  6. "type": "text"
  7. },
  8. "age": {
  9. "type": "long"
  10. },
  11. "birth": {
  12. "type": "date"
  13. }
  14. }
  15. }
  16. }
  17. {
  18. "error" : "Incorrect HTTP method for uri [/idx2/_doc?pretty=true] and method [PUT], allowed: [POST]",
  19. "status" : 405
  20. }

推测, 大概是因为指定的类型是文档的, 而不是索引的.

查看索引

  1. GET /索引名 # 查看索引信息

GET /myidx

  1. {
  2. "myidx" : {
  3. "aliases" : { },
  4. "mappings" : {
  5. "properties" : { ########## 文档属性类型
  6. "age" : {
  7. "type" : "long"
  8. },
  9. "birth" : {
  10. "type" : "date"
  11. },
  12. "name" : {
  13. "type" : "text",
  14. "fields" : {
  15. "keyword" : {
  16. "type" : "keyword",
  17. "ignore_above" : 256
  18. }
  19. }
  20. }
  21. }
  22. },
  23. "settings" : {
  24. "index" : {
  25. "routing" : {
  26. "allocation" : {
  27. "include" : {
  28. "_tier_preference" : "data_content"
  29. }
  30. }
  31. },
  32. "number_of_shards" : "1",
  33. "provided_name" : "myidx",
  34. "creation_date" : "1638236601941",
  35. "number_of_replicas" : "1",
  36. "uuid" : "tpkXV0QKTXC2w8wUIQnRrg",
  37. "version" : {
  38. "created" : "7150299"
  39. }
  40. }
  41. }
  42. }
  43. }

修改文档

[ PUT /索引名/类型名/文档ID ] 方式下的修改,会删除原文档,创建新文档

  1. PUT /myidx/mytype/1
  2. {
  3. "name": "engureeeeeeee",
  4. "age": 18
  5. }

原文档:

  1. {
  2. "name": "engure",
  3. "age": 18,
  4. "birth": "1999-01-02"
  5. }

修改后:
03-RestFul 风格 - 图7image.png

[ POST /索引名/类型名/文档ID/_update ] 方式下的修改,是部分修改,推荐使用!

  1. POST /myidx/mytype/1/_update
  2. {
  3. "doc": {
  4. "name": "engurggggggggg",
  5. "age": 188
  6. }
  7. }
  1. {
  2. "_index" : "myidx",
  3. "_type" : "mytype",
  4. "_id" : "1",
  5. "_version" : 3,
  6. "result" : "updated",
  7. "_shards" : {
  8. "total" : 2,
  9. "successful" : 1,
  10. "failed" : 0
  11. },
  12. "_seq_no" : 2,
  13. "_primary_term" : 1
  14. }

删除索引

  1. DELETE /索引名
  2. 比如, DELETE /myidx

关于文档的基本操作

复习上一小结内容,同时补充一些内容

添加文档

  1. PUT users/_doc/1
  2. {
  3. "name": "engure",
  4. "age": 18,
  5. "birth": "1998-01-01",
  6. "desc": "00后技术小萌新~",
  7. "tags": ["技术宅","金属乐","数码迷","直男"]
  8. }

查询文档

  1. GET /users/_doc/3

更新文档(2)

1、原 PUT 文档 写法。会删除原文档。不推荐!

  1. PUT users/_doc/1
  2. {
  3. "name": "engure",
  4. "age": 18,
  5. "birth": "1998-01-01",
  6. "desc": "00后技术小萌新~",
  7. "tags": ["技术宅","金属乐","数码迷","直男"]
  8. }

2、POST 修改。不会删除原文档,而是在原来的基础上改动。推荐使用!

  1. POST /users/_doc/3/_update
  2. {
  3. "doc": {
  4. "name": "James",
  5. "desc": "pets lover."
  6. }
  7. }

简单搜索

  1. GET 索引名/类型/文档ID

简单的条件查询:

  1. # 搜索所有文档
  2. GET user/_doc/_search
  3. # 按条件搜索
  4. GET 索引名/类型/_search?q=属性名1:属性值1&q=属性2:属性值2
  5. 比如, GET user/_doc/_search?q=name:engure&q=age:18

复杂搜索

排序、分页、高亮、模糊查询、精准查询、、、

其他

查看 ES 状态

  1. GET _cat/health
  2. GET _cat/indices?v