针对elasticsearch已经提供了一套rest接口让我们进行索引创建,更新,删除,添加等很多的操作。在命令行下格式为:
curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>
1.当我们需要提供一个搜索功能时,比如有一个数据库music,那么我们会使用elasticsearch创建一个数据库索引
curl -XPUT 'localhost:9200/music?pretty'
这个其实也可以用表单提交,只是method为put,这个命令创建了索引music。加上pretty表示格式化输出结果,创建完后,我们再使用命令看看创建的索引,可以发现
curl -XGET localhost:9200/_cat/indices?v
health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open music 5 1 0 0 575b 575b
yellow open bank 5 1 1000 0 418.4kb 418.4kb
music的index已经创建好了,并且有5个分片,还有一个rep分片,因为只有一个服务因此,rep分片还没启用,所以health为yellow,同时占用的存储空间为575b。目前还没有一条索引数据。
2.向music数据库索引的表song中添加一条id为1的数据{ name:“huage”,age:25 }
2.1创建方式XPUT
curl -XPUT 'localhost:9200/music/song/1?pretty' -d '{ name:"huage",age:25 }'
{
"_index" : "music",
"_type" : "song",
"_id" : "1",
"_version" : 1,
"created" : true
}
可以看到创建了id为1,在索引music数据库上,song其实对应我们所说的表,刚开始创建version为1,如果有更新,version会加1,同时created为false。
比如我再次执行上述命令后,结果为
{
"_index" : "music",
"_type" : "song",
"_id" : "1",
"_version" : 2,
"created" : false
}
这是一种更新方式。
因此可以概况为如果id不存在那么进行创建,如果存在那么进行更新。
2.2使用XPOST创建
curl -XPOST localhost:9200/music/song?pretty -d '{name:"tonva",age:25}'
{
"_index" : "music",
"_type" : "song",
"_id" : "AUqAcG3AEK4pL-NYj_98",
"_version" : 1,
"created" : true
}
可以发现创建后的id是一个随机生成的id。
3.查询music中song的id为1的索引数据
curl -XGET localhost:9200/music/song/1?pretty
{
"_index" : "music",
"_type" : "song",
"_id" : "1",
"_version" : 2,
"found" : true,
"_source":{ name:"huage",age:25 }
}
可以看到found为true,也就是找到结果了,结果为_source:{ name:"huage",age:25 }
4.删除索引库中的表song
curl -XDELETE localhost:9200/music/song?pretty
{
"acknowledged" : true
}
curl -XGET localhost:9200/music/_search?pretty
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
删除之后,可以看到hits里面没有任何信息,表示type为song的表记录索引被删除了。
5.删除索引库music
curl -XDELETE localhost:9200/music?pretty
{
"acknowledged" : true
}
curl localhost:9200/_cat/indices?v
health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open bank 5 1 1000 0 418.4kb 418.4kb
可以看到不再存在music这个index了。
6.更新索引id为1的song数据
我们先恢复下之前的数据,可以看到存在一条数据
curl localhost:9200/music/song/_search?pretty
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [ {
"_index" : "music",
"_type" : "song",
"_id" : "1",
"_score" : 1.0,
"_source":{name:"tonva",age:25}
} ]
}
}
我们对他进行更新
curl -XPOST localhost:9200/music/song/1/_update?pretty -d '{"doc":{name:"huage",sex:"man"}}'
{
"_index" : "music",
"_type" : "song",
"_id" : "1",
"_version" : 2
}
curl localhost:9200/music/song/1?pretty
{
"_index" : "music",
"_type" : "song",
"_id" : "1",
"_version" : 2,
"found" : true,
"_source":{"name":"huage","age":25,"sex":"man"}
}
更新之后,我们发现数据source的name改为huage了,还有增加了sex字段为man。
如果我们需要对age进行加5,那么如下
curl -XPOST 'localhost:9200/music/song/1/_update?pretty' -d '
{
"script" : "ctx._source.age += 5"
}'
可以看到age变成30了。
要注意的是使用这个命令只是一次操作单条记录】。因此效率不是很高
7.删除songs中id为1的数据
curl -XDELETE 'localhost:9200/music/song/1?pretty'
我们还可以指定条件删除
curl -XDELETE 'localhost:9200/msuci/song/_query?pretty' -d '
{
"query": { "match": { "name": "huage" } }
}'
{
"_indices" : {
"music" : {
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
}
}
}
}
8.批量数据操作,添加两条数据到songs中
curl -XPOST 'localhost:9200/music/song/_bulk?pretty' -d '
{"index":{"_id":"1"}}
{"name": "tonva" }
{"index":{"_id":"2"}}
{"name": "huage" }
'
{
"took" : 5,
"errors" : false,
"items" : [ {
"index" : {
"_index" : "music",
"_type" : "song",
"_id" : "1",
"_version" : 1,
"status" : 201
}
}, {
"index" : {
"_index" : "music",
"_type" : "song",
"_id" : "2",
"_version" : 1,
"status" : 201
}
} ]
}
可以看到插入了两条数据
curl -XPOST 'localhost:9200/music/song/_bulk?pretty' -d '
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'
{
"took" : 5,
"errors" : false,
"items" : [ {
"update" : {
"_index" : "music",
"_type" : "song",
"_id" : "1",
"_version" : 2,
"status" : 200
}
}, {
"delete" : {
"_index" : "music",
"_type" : "song",
"_id" : "2",
"_version" : 4,
"status" : 200,
"found" : true
}
} ]
}
可以看到update了id为1的数据,delete了id为2的数据
9.导入json文件数据
curl -XPOST localhost:9200/bank/account/_bulk?pretty --data-binary @accounts.json
accounts.json是一个json文件,可以看到结果
curl localhost:9200/_cat/indices?v
health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open music 5 1 3 0 7.7kb 7.7kb
yellow open bank 5 1 1000 0 418.2kb 418.2kb