一、檢測集羣是否健康。javascript
curl -XGET 'localhost:9200/_cat/health?v'
#後面加一個v表示讓輸出內容表格顯示錶頭
綠色表示一切正常,黃色表示全部的數據可用可是部分副本尚未分配,紅色表示部分數據由於某些緣由不可用。html
二、查看集羣的的節點列表。java
curl -XGET 'localhost:9200/_cat/nodes?v'
三、建立索引(方法一)node
#建立一個名爲demo_v1的索引
> curl -XPUT 'localhost:9200/demo_v1' {"acknowledged":true,"shards_acknowledged":true}%
默認的number_of_shards=5, number_of_replicas=1,app
說明:number_of_shards表示設置一個索引的碎片數量,number_of_replicas表示設置一個索引可被複制的數量。這兩個屬性的設置直接影響集羣中索引和搜索操做的執行。curl
假設你有足夠的機器來持有碎片和複製品,那麼能夠按以下規則設置這兩個值: ide
1) 擁有更多的碎片能夠提高索引執行能力,並容許經過機器分發一個大型的索引; 學習
2) 擁有更多的複製器可以提高搜索執行能力以及集羣能力。 ui
對於一個索引來講,number_of_shards只能設置一次,而number_of_replicas可使用索引更新設置API在任什麼時候候被增長或者減小。 搜索引擎
四、建立索引(方法二)
#建立一個名爲demo_v2的索引,並設置number_of_shards=3,number_of_replicas=2
curl -XPUT 'http://localhost:9200/demo_v2/' -d'{ "settings":{ "index":{ "number_of_shards":3, "number_of_replicas":2 } } }'
五、查看索引
#查看全部索引列表
>curl -XGET 'localhost:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open test_v3 MBBLajV1RXynlf9AsRk-nQ 5 1 0 0 260b 260b
yellow open test_v1 nD02rWTqQO-vUaiM8fvolg 5 1 0 0 650b 650b
yellow open demo_v1 TItuViE3Qyu8qqqMMiLkAQ 5 1 0 0 795b 795b
yellow open english Y6Zwxj2cQPijI6NBOmGy9g 5 1 10010130 0 29gb 29gb
yellow open demo_v2 RmYvA8NdSOupXCS5VlXraA 3 2 0 0 390b 390b
yellow open test_v2 HBlEt6zCRomBnQYQrfcpuA 5 1 0 0 260b 260b
#查看索引demo_v1列表
curl -XGET 'localhost:9200/_cat/indices/demo_v1?v'
#查看索引demo_v1,demo_v2列表
curl -XGET 'localhost:9200/_cat/indices/demo_v1,demo_v2?v'
6、給索引設置別名
#給索引demo_v1設置別名demo curl -XPUT 'localhost:9200/demo_v1/_alias/demo' #給索引demo_v1設置別名demo curl -XPOST 'localhost:9200/_aliases?pretty' -d'{ "actions" : [ { "add" : { "index" : "demo_v1", "alias" : "demo"} } ]}' #移除索引demo_v1的別名demo,並給索引demo_v2設置別名demo curl -XPOST 'localhost:9200/_aliases?pretty' -d'{ "actions" : [ { "remove" : { "index" : "demo_v1", "alias" : "demo" } }, { "add" : { "index" : "demo_v2", "alias" : "demo" } } ]}'
命令後面添加pretty表示漂亮的輸出,也就是格式化輸出
七、查看別名
#查看全部的別名 curl -XGET 'localhost:9200/_alias/*' {"demo_v1":{"aliases":{"demo":{}}}}% #查看索引demo_v1的全部別名 curl -XGET 'localhost:9200/demo_v1/_alias/*' {"demo_v1":{"aliases":{"demo":{}}}}%
八、刪除索引
#刪除索引test_v1
curl -XDELETE 'localhost:9200/test_v1' #刪除索引test_v2,test_v3 curl -XDELETE 'localhost:9200/test_v2,test_v3'
九、查看索引的設置信息
#查看索引demo_v1的全部設置信息 > curl -XGET 'localhost:9200/demo_v1?pretty' { "demo_v1" : { "aliases" : { "demo" : { } }, "mappings" : { }, "settings" : { "index" : { "creation_date" : "1497955726944", "number_of_shards" : "5", "number_of_replicas" : "1", "uuid" : "TItuViE3Qyu8qqqMMiLkAQ", "version" : { "created" : "5030099" }, "provided_name" : "demo_v1" } } } } #查看索引demo_v1設置的基礎信息 curl -XGET 'localhost:9200/demo_v1/_settings?pretty' { "demo_v1" : { "settings" : { "index" : { "creation_date" : "1497955726944", "number_of_shards" : "5", "number_of_replicas" : "1", "uuid" : "TItuViE3Qyu8qqqMMiLkAQ", "version" : { "created" : "5030099" }, "provided_name" : "demo_v1" } } } } #查看索引demo_v1設置的mapping信息 curl -XGET 'localhost:9200/demo_v1/_mapping?pretty' { "demo_v1" : { "mappings" : { } } }
九、CRUD數據
#向索引demo_v1中添加幾條數據(type=fruit添加3條數據,type=book添加兩條數據) curl -XPOST 'localhost:9200/_bulk?pretty' -d' { "index" : { "_index" : "demo_v1", "_type" : "fruit", "_id" : "1" }} { "name" : "蘋果"} { "create" : { "_index" : "demo_v1", "_type" : "fruit", "_id" : "2" }} { "name" : "香蕉"} { "index" : { "_index" : "demo_v1", "_type" : "fruit", "_id" : "3" }} { "name" : "西瓜"} { "index" : { "_index" : "demo_v1", "_type" : "book", "_id" : "1" }} { "name" : "深刻淺出node.js" } { "create" : { "_index" : "demo_v1", "_type" : "book", "_id" : "2" }} { "name" : "你不知道的javascript" } ' #查看索引demo_v1下的數據 curl -XGET 'localhost:9200/demo_v1/_search?pretty' #查看索引demo_v1類型fruit下的數據 curl -XGET 'localhost:9200/demo_v1/fruit/_search?pretty' #查看索引demo_v1類型fruit,類型book下的數據 curl -XGET 'localhost:9200/demo_v1/fruit,book/_search?pretty'
#用別名查看demo_v1下的數據
curl -XGET 'localhost:9200/demo/_search?pretty' #刪除索引=demo_v1類型=fruit下面id=2的數據 curl -XPOST 'localhost:9200/_bulk?pretty' -d' { "delete" : { "_index" : "demo_v1", "_type" : "fruit", "_id" : "2" }}'
或者操做
curl -XDELETE 'localhost:9200/demo_v1/fruit/2' #修改索引demo_v1類型fruit下面id=3的數據name字段 curl -XPOST 'localhost:9200/_bulk?pretty' -d' { "update" : {"_id" : "3", "_type":"fruit", "_index": "demo_v1"}} { "doc" : {"name":"草莓"}} '
十、自定義分詞器
#若是索引demo_v6沒有建立,能夠直接建立並設置逗號分詞器 curl -XPUT 'localhost:9200/demo_v6' -d'{ "settings": { "index": { "analysis": { "analyzer": { "douhao_analyzer": { "pattern": ",", "type": "pattern" } } }, "number_of_shards": "5", "number_of_replicas": "1" } } }' #若是索引demo_v1已經存在,先關索引,再設置逗號分詞器,而後打開索引 curl -XPOST 'localhost:9200/omo_v1/_close' curl -XPUT 'localhost:9200/demo_v1/_settings' -d'{"analysis": { "analyzer": { "douhao_analyzer": { "type":"pattern", "pattern":"," } } }}' curl -XPOST 'localhost:9200/omo_v1/_open'
十一、給索引設置mapping
#查看索引demo_v1的mapping設置( 沒有手動設置的話採用系統默認設置,分詞器默認standard分詞(標準分詞器)) curl -XGET 'localhost:9200/demo_v1/_mapping?pretty' { "demo_v1" : { "mappings" : { "book" : { "properties" : { "name" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } }, "fruit" : { "properties" : { "name" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } } } } } #給索引demo_v1的mapping設置中fruit新增一個字段tag curl -XPOST 'localhost:9200/demo_v1/fruit/_mapping?pretty' -d '{ "fruit": { "properties": { "tag":{ "type":"text" } } } }'
注意:已經設置好的mapping能夠新增字段,可是對已經設置的字段是不能修改的。由於Elasticsearch底層使用的是lucene庫,修改之後索引和搜索要涉及分詞方式等操做,因此不容許修改。
若是一個mapping設置過了,想要修改type或analyzer,一般的作法是新建一個索引,從新設置mapping,再把數據同步過來。
具體作法參見下一篇文章 《學習用Node.js和Elasticsearch構建搜索引擎(7):零停機時間更新索引配置或遷移索引》