1. 索引

1.1 什么索引

一个索引就是一个拥有几分相似特征的文档的集合。比如说,你可以有一个客户数据的索引,另一个产品目录的索引,还有一个订单数据的索引。一个索引由一个名字来标识(必须全部是小写字母的),并且当我们要对对应于这个索引中的文档进行索引、搜索、更新和删除的时候,都要使用到这个名字;

1.2 命令操作

  1. # 查看所有索引
  2. GET /_cat/indices?v
  3. health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
  4. yellow open twitter c5JJhKzuS_CBv-q4xexdzw 3 2 1 0 4.6kb 4.6kb
  5. yellow open movie SMGLbYkFRPierTNaNsbXng 3 1 3 0 13.1kb 13.1kb
  6. yellow open weibo hqxawYAZTJqkKFkPQxMk4w 1 1 5 0 7.7kb 7.7kb
  7. yellow open idx_locat 1CRshRsUTpKhe7Y4LEUbtg 3 2 2 0 9kb 9kb
  8. green open .kibana_task_manager_1 MEZeVn5IQ7im2X-ljPrWzA 1 0 2 0 21.8kb 21.8kb
  9. yellow open my_index dHN-CBpySAeD6T29Bn-MSw 1 1 0 0 283b 283b
  10. green open .apm-agent-configuration GFZqnLa9TYOXw9MKYYD92Q 1 0 0 0 283b 283b
  11. green open .kibana_1 zDKttXRTQAGpd3jKp3webg 1 0 9 0 32.2kb 32.2kb
  1. # 创建索引
  2. PUT movie
  3. {
  4. "settings": {
  5. "number_of_shards": 3,
  6. "number_of_replicas": 1
  7. },
  8. "mappings": {
  9. "_doc": {
  10. "properties": {
  11. "name": {
  12. "type": "text"
  13. },
  14. "type": {
  15. "type": "keyword"
  16. },
  17. "country": {
  18. "type": "text"
  19. },
  20. "director": {
  21. "type": "text"
  22. },
  23. "date": {
  24. "type": "date"
  25. }
  26. }
  27. }
  28. }
  29. }
  1. # 删除索引
  2. DELETE weibo
  1. #插入数据 (1代表id,不写自动设置为默认值)
  2. POST movie/_doc/1
  3. {
  4. "name":"Titanic",
  5. "type":"romance",
  6. "country":"America",
  7. "director":"James",
  8. "date":"1997-12-19"
  9. }
  1. # 查看ID1的数据
  2. GET movie/_doc/1
  3. # 结果返回
  4. {
  5. "_index" : "movie",
  6. "_type" : "_doc",
  7. "_id" : "1",
  8. "_version" : 3,
  9. "_seq_no" : 3,
  10. "_primary_term" : 1,
  11. "found" : true,
  12. "_source" : {
  13. "id" : "1",
  14. "name" : "Titanic",
  15. "type" : "Titanic",
  16. "country" : "America",
  17. "director" : "James",
  18. "date" : "1997-12-19"
  19. }
  20. }
  1. # 修改数据(修改id1director)
  2. POST movie/_doc/1
  3. {
  4. "name":"Titanic",
  5. "type":"romance",
  6. "country":"America",
  7. "director":"James-Cameron",
  8. "date":"1997-12-19"
  9. }
  1. # 直接修改指定字段
  2. POST movie/_update/1
  3. {
  4. "doc": {
  5. "director":"Luc-Besson"
  6. }
  7. }
  1. # 删除数据(删除id1的数据)
  2. DELETE movie/_doc/1
  1. # 查询所有数据
  2. GET movie/_search