./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"} }'
复制代码
接下来更多精彩内容,亲关注: