1. 新增文档

格式:
PUT /index/type/1
在es6.x版本后,就不推荐使用type了。可以使用_doc替代type。

  1. PUT /person/_doc/1
  2. {
  3. "name":"john",
  4. "age":10,
  5. "born":"2010-12-01",
  6. "tags":["boy","sunny"]
  7. }

es会自动建立index和type,而且es还会为每个字段创建倒排索引,可以让其被搜索到。

2. 查询文档

格式:
GET /index/_search 或者 GET /index/_dodc/id
查询结果:

  1. {
  2. "_index" : "person",
  3. "_type" : "_doc",
  4. "_id" : "1",
  5. "_version" : 1,
  6. "_seq_no" : 0,
  7. "_primary_term" : 3,
  8. "found" : true,
  9. "_source" : {
  10. "name" : "john",
  11. "age" : 10,
  12. "born" : "2010-12-01",
  13. "tags" : [
  14. "boy",
  15. "sunny"
  16. ]
  17. }
  18. }

match查询

  1. GET /sit_midea_auth_role/_search
  2. {
  3. "query": {
  4. "match": {
  5. "roleCode": "RL201912040006"
  6. }
  7. }
  8. }

bool查询
·

查询结果字段说明

字段 含义
_index 文档索引
_type 文档类型
_id 文档id
_version 文档版本,更新时自动加1,针对指定文档
_seq_no

版本控制使用,针对于当前index
_primary_term
found 是否查询到,true or false
_source 查询结果

3. 更新文档

3.1 覆盖更新

  1. PUT /person/_doc/1
  2. {
  3. "name":"lisi"
  4. }

更新后查询结果如下

  1. {
  2. "_index" : "person",
  3. "_type" : "_doc",
  4. "_id" : "1",
  5. "_version" : 2,
  6. "_seq_no" : 1,
  7. "_primary_term" : 3,
  8. "found" : true,
  9. "_source" : {
  10. "name" : "lisi"
  11. }
  12. }

3.2 更新指定字段

  1. POST /person/_update/1
  2. {
  3. "doc": {
  4. "name":"lier"
  5. }
  6. }

3.3 更新指定查询文档

  1. POST /index/_update_by_query
  2. {
  3. "script":{
  4. "source":"ctx._source.deleteFlag = 1",
  5. "lang":"painless"
  6. },
  7. "query":{
  8. "bool": {
  9. "must": [
  10. {
  11. "term": {
  12. "roleCode.keyword": {
  13. "value": "RL201905080024"
  14. }
  15. }
  16. }
  17. ]
  18. }
  19. }
  20. }

script:修改文档操作
query:查出符合条件的文档

4. 删除文档

格式: DELETE /index/_doc/id

  1. DELETE /person/_doc/1

根据条件删除:_delete_by_query

  1. POST /index/_delete_by_query
  2. {
  3. "query": {"match_all": {}}
  4. }
  5. #不带条件则删除所有