Description
ES安装好之后(参考 #5 ),可以使用以下命令进行简单的操作。
创建索引
ES的索引,在6.x之后,可以初略的理解为相当于关系型数据库概念里面的表。与关系型数据库不一样的地方在于ES的索引并不强制要求结构化。所以最简单的一个存储题目的索引(question_index)就可以创建如下:
shellbye@localhost:~$ curl -X PUT "localhost:9200/question_index?pretty"
以下为输出
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "question_index"
}
创建完成之后可以通过以下命令查看索引列表(相当于MySQL里面的show tables
):
shellbye@localhost:~$ curl "localhost:9200/_cat/indices?v"
以下为输出
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open question_index sunRjkjmRYGuwg5-CLJokA 5 1 0 0 1.1kb 1.1kb
有了索引之后,写下来就可以向索引里面写入数据(增)了。
写入数据(增)
shellbye@localhost:~$ curl -X PUT "localhost:9200/question_index/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"id": "10001",
"subject": "math",
"question": "calculate 1+1"
}
'
以下为输出
{
"_index" : "question_index",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
通过返回数据里面的"result" : "created"
我们可以看到这个新的条目创建成功了,这里需要注意以下请求url里面的1
、我输入的数据里面的{"id": "1001"}
,和返回的"_id" : "1"
。其中输入的url里面的1表示我指定了这个新增的题目在ES内部的id,我输入的数据里面的{"id": "1001"}
则是数据本身的一个标志id,对于ES来说,它和别的字段都是一视同仁的,那么返回的"_id" : "1"
,即对应请求url里面的1
。
做个实验会更加明白:
shellbye@localhost:~$ curl -X PUT "localhost:9200/question_index/_doc/this_id_can_be_any_thing?pretty" -H 'Content-Type: application/json' -d'
{
"id": "10002",
"subject": "math",
"question": "calculate 1+2"
}
'
以下为输出
{
"_index" : "question_index",
"_type" : "_doc",
"_id" : "this_id_can_be_any_thing",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
好了,添加数据之后,我们就可以进行接下来的修改数据(改)了。
修改数据(改)
通过修改数据,也可以更加清晰的理解前面的url里面的和返回的_id
了.
shellbye@localhost:~$ curl -X PUT "localhost:9200/question_index/_doc/this_id_can_be_any_thing?pretty" -H 'Content-Type: application/json' -d'
{
"id": "10002",
"subject": "math",
"question": "calculate 1+2222"
}
'
以下为输出
{
"_index" : "question_index",
"_type" : "_doc",
"_id" : "this_id_can_be_any_thing",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
如上所示,只要提供的id是已经有的,那么就是相当于是一次更新操作("result" : "updated",
)。当然也可以显示的在请求的url后面加上_update
,但是这个时候要注意和不加_update
的操作的http方法不一样,而且参数外面也多了一层doc
,如下所示:
shellbye@localhost:~$ curl -X POST "localhost:9200/question_index/_doc/this_id_can_be_any_thing/_update?pretty" -H 'Content-Type: application/json' -d'
{
"doc": {
"id": "10002",
"subject": "math",
"question": "calculate 1+3"
}
}
'
以下为输出
{
"_index" : "question_index",
"_type" : "_doc",
"_id" : "this_id_can_be_any_thing",
"_version" : 5,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 1
}
查找数据(查)
ES里的“查找”,有两层含义
- 如同传统关系型数据库那样的根据主键进行的查找
shellbye@localhost:~$ curl -X GET "localhost:9200/question_index/_doc/1?pretty"
以下为输出
{
"_index" : "question_index",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"id" : "10001",
"subject" : "math",
"question" : "calculate 1+1"
}
}
shellbye@localhost:~$ curl -X GET "localhost:9200/question_index/_doc/this_id_can_be_any_thing?pretty"
以下为输出
{
"_index" : "question_index",
"_type" : "_doc",
"_id" : "this_id_can_be_any_thing",
"_version" : 5,
"found" : true,
"_source" : {
"id" : "10002",
"subject" : "math",
"question" : "calculate 1+3"
}
}
- 另一种就是ES作为搜索引擎提供的搜索
shellbye@localhost:~$ curl -X GET 'localhost:9200/question_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
"size":1,
"query" : {
"match" : {
"question" : "calculate"
}
}
}'
以下为输出
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "question_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"id" : "10001",
"subject" : "math",
"question" : "calculate 1+1"
}
}
]
}
}
如上所示的搜索,输入的关键字是calculate
,正确的返回了包含该关键字的数据。
删除数据(删)
最后是删除数据,之所以最后写删除数据,是因为一旦删除了之后,还得重新添加,比较麻烦,所以就把删除数据的demo写到最后了。
shellbye@localhost:~$ curl -X DELETE "localhost:9200/question_index/_doc/1?pretty"
以下为输出
{
"_index" : "question_index",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
删除数据之后可以通过查看总数来确认删除:
shellbye@localhost:~$ curl "localhost:9200/_cat/indices?v"
以下为输出
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open question_index sunRjkjmRYGuwg5-CLJokA 5 1 1 0 10kb 10kb
可以看到我们创建并添加了两条数据、删除了一条数据的索引目前只有一条数据了。
删除索引
删除索引和删除数据的接口基本一致,就是末尾少了几个参数
shellbye@localhost:~$ curl -X DELETE "localhost:9200/question_index/?pretty"
以下为输出
{
"acknowledged" : true
}
shellbye@localhost:~$ curl "localhost:9200/_cat/indices?v"
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
可以看到此时已经没有任何索引了。🙂