以前已經說過最近正在作數據建設,爬取數據以後通過處理,最終導入到ElasticSearch中,並編寫公共接口以提供給後臺進行檢索操做;原本想等把ElasticSearch官方API都看過一遍,造成思惟導圖以後再整理出來,由於熟悉一個工具,它能作到的,比你知道它能作到的要全面也重要的多,可是整理了兩章以後發現,內容真的太多了,這還僅僅只2類。。因此想仍是先把基礎用法記錄下來,先一步步來了。html
Elasticsearch 是一個分佈式可擴展的近實時搜索和分析引擎,一個創建在全文搜索引擎 Apache Lucene(TM) 基礎上的搜索引擎.固然 Elasticsearch 並不只僅是 Lucene 那麼簡單,它不只包括了全文搜索功能,還能夠進行如下工做:node
- 分佈式實時文件存儲,並將每個字段都編入索引,使其能夠被搜索。
- 實時分析的分佈式搜索引擎。
- 能夠擴展到上百臺服務器,處理PB級別的結構化或非結構化數據。
安裝比較簡單,建議練手階段安裝Kibana,安裝步驟參見以前寫的博客:ElasticSearch安裝數據庫
關係數據庫 ⇒ 數據庫 ⇒ 表 ⇒ 行 ⇒ 列(Columns) Elasticsearch ⇒ 索引(Index) ⇒ 文檔(Docments) ⇒ 字段(Fields)服務器
Elasticsearch ⇒ 索引(Index) ⇒ 文檔(Docments) ⇒ 字段(Fields)app
既然刪除了type,感受將Index理解爲table是否是更加合理些。。elasticsearch
建立Index語法以下:分佈式
PUT /userinfo?pretty { "mappings": { "_doc": { "properties": { "name": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "fields": {"raw":{"type":"keyword"}}}, "content": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "org_type": { "type": "keyword"}, "create_time":{"type":"date", "format": "epoch_second"} } } } }
若是理解了上面的基本概念的話,這命令看起來應該不難理解,有幾點須要提一下:ide
PUT /userinfo/_doc/1? { "name":"lctest", "age":29 }經過GET /userinfo/_mapping命令查看userinfo最新的字段,能夠看到ES默認新增了一個類型爲long的age字 段。可是通常建議關鍵字段在建立Index的時候進行指定字段;
{ "userinfo": { "mappings": { "_doc": { "properties": { "age": { "type": "long" }, "content": { "type": "text", "analyzer": "ik_max_word" }, "create_time": { "type": "date", "format": "epoch_second" }, "name": { "type": "text", "fields": { "raw": { "type": "keyword" } }, "analyzer": "ik_max_word" }, "org_type": { "type": "keyword" } } } } } }
PUT /userinfo/_mapping/_doc { "properties": { "params": { "type": "nested", "properties": { "update_time":{"type":"date", "format": "epoch_second"} } } } }
//刪除指定索引 DELETE /userinfo //刪除多個索引 DELETE /index1,index2 或者 DELETE /index* //刪除全部索引 DELETE /_all 或者 DELETE /*
最基本的索引操做就到此結束,基本上能知足簡單的基本需求,下面有一些擴展知識點,能夠選擇性的使用工具
索引別名就像一個快捷方式或軟鏈接,或者是一個指向,都是最終指的同一個東西,別名 帶給咱們極大的靈活性,容許咱們作下面這些:ui
POST /_aliases { "actions" : [ { "add" : { "index" : "test1", "alias" : "alias1" } } ] }
POST /_aliases { "actions" : [ { "remove" : { "index" : "test1", "alias" : "alias1" } } ] }
POST /_aliases { "actions" : [ { "remove" : { "index" : "test1", "alias" : "alias1" } }, { "add" : { "index" : "test2", "alias" : "alias1" } } ] }
PUT /{index}/_alias/{name} PUT /logs_201305/_alias/2013
以上即是Index別名的基本經常使用語法,完整API詳見Aliases API
查看全部索引信息 GET /_cat/indices?v&pretty
查看某個索引信息 GET /{index}
刪除索引單個索引 DELETE /{index}
刪除全部索引 DELETE /_all 或者 DELETE /*
刪除多個索引: DELETE /index1,index2 或者 DELETE /index*
查看索引的映射 GET /{index}/_mapping
查看某個索引的某個類型的映射 GET /{index}/_mapping/{type}
映射添加新字段 PUT /{index}/_mapping/{type}
爲了防止誤操做 ,形成刪庫跑路的狀況,建議在elasticsearch.yml 作以下配置:action.destructive_requires_name: true 這個設置使刪除只限於特定名稱指向的數據, 而不容許經過指定 _all 或通配符來刪除指定索引庫。你一樣能夠經過 Cluster State API 動態的更新這個設置。
新手推薦使用Kibana工具,帶命令提示,很適合不熟悉命令的初學者,我也一直在用,只是博客的話,命令的表現形式感受更好一些