./bin/elasticsearch -d
複製代碼
查看是否啓動成功, 默認監聽9200node
curl http://127.0.0.1:9200
output:
{
"name" : "Christopher Summers",
"cluster_name" : "elasticsearch",
"version" : {
"number" : "2.3.3",
"build_hash" : "218bdf10790eef486ff2c41a3df5cfa32dadcfde",
"build_timestamp" : "2016-05-17T15:40:04Z",
"build_snapshot" : false,
"lucene_version" : "5.5.0"
},
"tagline" : "You Know, for Search"
}
複製代碼
成功返回信息,證實咱們的es服務啓動成功數據庫
咱們可使用_cat下面的參數查看json
curl http://127.0.0.1:9200/_cat/indices?v
output:
health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open test 5 1 0 0 800b 800b
yellow open synctest 5 1 4 0 16.2kb 16.2kb
複製代碼
_cat 是很是性能監控方面很是重要的一個查詢手段,你們有興趣能夠自行研究api
curl http://127.0.0.1:9200/_cat/
output:
=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
複製代碼
咱們還可使用 _all 獲取全部index和type具體mapping信息安全
curl http://127.0.0.1:9200/_all
複製代碼
若是須要查看具體的index索引信息可使用bash
curl http://127.0.0.1:9200/test/_mapping
output:
{
"synctest":{
"mappings":{
"logs":{
"properties":{
"@timestamp":{
"type":"date",
"format":"strict_date_optional_time||epoch_millis"
},
"@version":{
"type":"string"
},
"host":{
"type":"string"
},
"message":{
"type":"string"
}
}
},
"article":{
"properties":{
"@timestamp":{
"type":"date",
"format":"strict_date_optional_time||epoch_millis"
},
"@version":{
"type":"string"
},
"id":{
"type":"long"
},
"is_deleted":{
"type":"long"
},
"name":{
"type":"string"
},
"type":{
"type":"string"
},
"update_time":{
"type":"date",
"format":"strict_date_optional_time||epoch_millis"
},
"user_name":{
"type":"string"
}
}
}
}
}
}
複製代碼
若是查看再具體的tpye _mapping, 可使用網絡
curl http://127.0.0.1:9200/synctest/article/_mapping
複製代碼
咱們在url中指定插入數據 _id=4,而後新增數據併發
curl -X PUT 127.0.0.1:9200/synctest/article/4 -d '{"id":4,"name":"Tom cat"}'
output:
{
"_index":"synctest",
"_type":"article",
"_id":"4",
"_version":1,
"_shards":{
"total":2,
"successful":1,
"failed":0
},
"created":true
}
複製代碼
這裏必定要注意, 若是系統中已經存在 _id=4,會發生數據覆蓋更新app
curl -X PUT http://127.0.0.1:9200/synctest/article/4?pretty -d '{"id":4,"cc":1}'
output:
{
"_index":"synctest",
"_type":"article",
"_id":"4",
"_version":2,
"_shards":{
"total":2,
"successful":1,
"failed":0
},
"created":false
}
複製代碼
注意到裏面有個 _version 字段, 故名思意是版本號的意思, 每更新一次版本號會加1, 實際工做中能夠用此來作併發控制curl
url中後面增長 pretty 意思是返回漂亮的json格式
注意咱們返回的 created 返回值,若是是更新 created 將返回false
咱們經過上面的 PUT 方式是能夠建立數據的, 可是它可能還會有反作用去更新數據, 在實際工做環境中多是不須要額外覆蓋以前數據去更新的。
那咱們經過一個 api 能夠只建立麼,若是存在就再也不建立了 ?
答案固然是有的啦!
咱們能夠在url後面加上 _create 指定建立
curl -X PUT http://127.0.0.1:9200/synctest/article/4/_create -d
'{"id":4,"name":"heihei"}'
output:
{
"error" : {
"root_cause" : [ {
"type" : "document_already_exists_exception",
"reason" : "[article][4]: document already exists",
"shard" : "2",
"index" : "synctest"
} ],
"type" : "document_already_exists_exception",
"reason" : "[article][4]: document already exists",
"shard" : "2",
"index" : "synctest"
},
"status" : 409
}
複製代碼
curl -X PUT http://127.0.0.1:9200/synctest/article/5/_create?pretty -d '{"id":5,"name":"heihei"}'
output:
{
"_index" : "synctest",
"_type" : "article",
"_id" : "5",
"_version" : 1,
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true
}
複製代碼
數據庫事物是咱們常常使用的操縱,那咱們怎麼實現es的事務呢.
還記得上面咱們提到的版本號嘛?
curl -X PUT http://127.0.0.1:9200/synctest/article/5?version=1 -d '{"id":5,"name":"heihei"}'
output:
{
"error":{
"root_cause":[
{
"type":"version_conflict_engine_exception",
"reason":"[article][5]: version conflict, current [2], provided [1]",
"shard":"1",
"index":"synctest"
}
],
"type":"version_conflict_engine_exception",
"reason":"[article][5]: version conflict, current [2], provided [1]",
"shard":"1",
"index":"synctest"
},
"status":409
}
複製代碼
上例指定版本號必須爲 version=1 才能更新成功,不然將會更新失敗
curl -X POST http://127.0.0.1:9200/synctest/article/4/_update
-d {"doc":{"views":1}}
output:
{
"_index":"synctest",
"_type":"article",
"_id":"4",
"_version":7,
"_shards":{
"total":2,
"successful":1,
"failed":0
}
}
修改後:
{
"_index":"synctest",
"_type":"article",
"_id":"4",
"_version":7,
"found":true,
"_source":{
"id":4,
"cc":1,
"views":1
}
}
複製代碼
看到咱們新增了一個字段views,表示爲瀏覽量,若是須要增長1的話,應該用一個api實現呢,咱們可使用腳本(默認groovy腳本)
首先咱們須要在elasticsearch.yml開啓腳本支持,並進行從新加載配置
script.inline: on
script.indexed: on
複製代碼
curl -X POST http://127.0.0.1:9200/synctest/article/4/_update -d
'{"script":"ctx._source.views+=1"}'
output:
{
"_index":"synctest",
"_type":"article",
"_id":"4",
"_version":12,
"_shards":{
"total":2,
"successful":1,
"failed":0
}
}
複製代碼
由於views在 _id=4 中是存在的,可是若是我想更新其餘fields不存在views字段,就會報錯
curl -X POST http://127.0.0.1:9200/synctest/article/2/_update
-d '{"script":"ctx._source.views+=1"}'
output:
{
"error":{
"root_cause":[
{
"type":"remote_transport_exception",
"reason":"[Ranger][192.168.2.108:9300][indices:data/write/update[s]]"
}
],
"type":"illegal_argument_exception",
"reason":"failed to execute script",
"caused_by":{
"type":"script_exception",
"reason":"failed to run inline script [ctx._source.views+=1] using lang [groovy]",
"caused_by":{
"type":"null_pointer_exception",
"reason":"Cannot execute null+1"
}
}
},
"status":400
}
複製代碼
那這種狀況如何解決呢?
{
"script":"ctx._source.views+=1",
"upsert":{
"views":1 #初始化值爲1
}
}
複製代碼
在併發網絡請求環境中,可能會出現各類問題, 你能夠了解下還有 retry_on_conflict 這個參數, 表示失敗重試的次數, 默認爲0, 我並無使用過此參數.
curl -X POST http://127.0.0.1:9200/synctest/article/4/_update?retry_on_conflict=5
-d '{"upsert":{"views":1},"script":"ctx._source.views+=1"}'
複製代碼
咱們還可使用腳本作更多的事情。
根據條件判斷是否應該刪除此條文檔(高本班 >6.0)
curl -X POST http://127.0.0.1:9200/synctest/article/4/_update
-d '{"script":"ctx.op = ctx._source.views>3 ? 'delete' : 'none' "}'
複製代碼
或者使用傳參形式
{
"script" : "ctx.op = ctx._source.views>count ? 'delete' : 'none'",
"params" : {
"count": 3 #參數
}
}
複製代碼
es還支持批量的建立、更新、刪除操做(es 6.6)
curl -X POST http://127.0.0.1:9200/_bulk
-d '{"delete": { "_index": "synctest", "_type": "article", "_id": "4" } {"update": { "_index": "synctest", "_type": "article", "_id": "3" } { "doc" : {"title" : "bluk update"} }'
複製代碼
接下來更多精彩內容,親關注: