./bin/elasticsearch -d
查看是否启动成功, 默认监听9200
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下面的参数查看
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 是非常性能监控方面非常重要的一个查询手段,大家有兴趣可以自行研究
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索引信息可以使用
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,会发生数据覆盖更新
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, 实际工作中可以用此来做并发控制 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还支持批量的创建、更新、删除操作
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"} }'