Elasticsearch實戰系列(八)--ES常見操作

  1. 查詢ES下所有的索引
    1. curl '192.168.160.128:9200/_cat/indices'
  2. 刪除指定的索引
    1. curl -XDELETE '192.168.160.128:9200/index1'
  3. 刪除指定的多個索引
    1. curl -XDELETE '192.168.160.128:9200/index1,index2'
  4. 刪除所有索引
    1. curl -XDELETE '192.168.160.128:9200/_all'
  5. 創建索引
    1. curl '192.168.160.128:9200/_cat/indices'
  6. 創建索引並指定分片和副本數量
    1. curl -H "Content-Type:application/json" -XPUT '192.168.160.128:9200/index2' -d '{"settings":{"number_of_shards":12,"number_of_replicas":1}}'
  7. 創建索引並指定配置以及映射
    1. curl -H "Content-Type:application/json" -XPUT '192.168.160.128:9200/index3?pretty' -d '{"settings":{"number_of_shards":12,"number_of_replicas":1},"mappings":{"type1":{"properties":{"name":{"type":"keyword"}}}}}'
  8. 創建索引並指定配置、映射以及別名
    1. curl -H "Content-Type:application/json" -XPUT '192.168.160.128:9200/index4?pretty' -d '{"settings":{"number_of_shards":12,"number_of_replicas":1},"mappings":{"type1":{"properties":{"name":{"type":"keyword"}}}},"aliases":{"index4_v1":{},"index4_v2":{}}}'
  9. 使用_aliases爲索引添加別名
    1. curl -XPOST '192.168.160.128:9200/_aliases' -H "Content-Type:application/json" -d '{"actions":[{"add":{"index":"index1","alias":"index1_v1"}}]}'
  10. 使用_alias爲索引添加別名
    1. curl '192.168.16-XPUT '192.168.160.128:9200/index1/_alias/index1_v3'
  11. 查詢索引的所有別名
    1. curl '192.168.160.128:9200/index1/_alias/*'
  12. 查詢索引的映射
    1. curl '192.168.160.128:9200/index1/_mapping?pretty'
  13. 爲索引創建映射
    1. curl -H "Content-Type:application/json" '192.168.160.128:9200/index1/_mapping/type1' -d '{"type1":{"properties":{"name":{"type":"keyword"}}}}'
  14. 向索引中插入數據
    1. curl -H "Content-Type:application/json" -XPOST '192.168.160.128:9200/index1/type1/1?pretty' -d '{"name":"zhangsan","age":"22"}'
  15. 使用_bulk批量插入數據
      1. curl -H "Content-Type:application/json" -XPOST '192.168.160.128:9200/index1/type1/_bulk?pretty' --data-binary @test.json
    1. "index":{"_id":"3"}}

      {"name":"wangwu","age":"33"}

      {"index":{"_id":"4"}}

      {"name":"zhangliu","age":"35"}

      //最後一行應該空出

      1. curl -H "Content-Type:application/json" -XPOST '192.168.160.128:9200/index1/type1/_bulk?pretty' -d '{"index":{"_id":"3"}}{"name":"wangwu","age":"33"}{"index":{"_id":"4"}}{"name":"zhangliu","age":"35"}'
        1. 報錯,The bulk request must be terminated by a newline [\n]。-d中每行都需要換行,類似於json文檔
  16. 查詢指定id的數據
      1. curl '192.168.160.128:9200/index1/type1/1?pretty'
  17. 查詢指定索引指定類型下所有數據
    1. curl '192.168.160.128:9200/index1/type1/_search?pretty'
  18. 修改指定id的文檔
    1. curl -H "Content-Type:application/json" -XPOST '192.168.160.128:9200/index1/type1/4/_update?pretty' -d '{"doc":{"tags":["2","3"]}}'
  19. 查詢時在URL中指定搜索請求基本模塊from、size、sort等
    1. curl '192.168.160.128:9200/index1/type1/_search?pretty&from=0&size=2&sort=age:asc&_source=age&q=zhangsan'
  20. 查詢時在JSON請求體中指定搜索請求基本模塊from、size、sort等
    1. curl -H "Content-Type:application/json" '192.168.160.128:9200/index1/type1/_search?pretty' -d '{"query":{"match_all":{}},"from":0,"size":1,"_source":"name"}'
  21. 使用過濾器查詢
    1. curl -H "Content-Type:application/json" '192.168.160.128:9200/index1/type1/_search?pretty' -d '{"query":{"bool":{"filter":{"term":{"age":"33"}}}}}'
  •  
  •  
  •  
  •  
  •  
  •  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章