截止2019-06-28,ES最新版是 7.2.0 。
可以进入 https://www.elastic.co/cn/downloads/past-releases 如果要下载指定版本的安装包。
安装
前置条件是安装 Java。
然后去 ES 官网下载 ES。下载地址。ES 的最新版本是 7.2.0。
下载后,解压。
启动
$ cd elasticsearch-7.2.0
$ ./bin/elasticsearch
关于 Index、Type、Document
- Index 对应 MySQL 中的 Database;
Type 对应 MySQL 中的 Table;- Document 对应 MySQL 中表的记录。
在 7.0 以及之后的版本中 Type 被废弃了。(其实还可以用,但是已经不推荐了)
一个MySQL实例中可以创建多个 Database,一个Database中可以创建多个Table。
ES 的Type 被废弃后:
- ES 实例:对应 MySQL 实例中的一个 Database。
- Index 对应 MySQL 中的 Table 。
- Document 对应 MySQL 中表的记录。
Index 的复数是 Indices。
使用 curl 命令操作与 ES 交互
查看信息:
$ curl http://localhost:9200
{
"name" : "Myhost",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "abtEL4GKRfulSwTfJ0wX5Q",
"version" : {
"number" : "7.2.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "508c38a",
"build_date" : "2019-06-20T15:54:18.811730Z",
"build_snapshot" : false,
"lucene_version" : "8.0.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
查看所有的 Index
$ curl 'localhost:9200/_mapping?pretty=true'
{ }
创建 Index
使用 HTTP PUT 请求创建 Index:
$ curl -X PUT 'localhost:9200/school?pretty=true'
{
"acknowledged":true,
"shards_acknowledged":true,
"index":"school"
}
再次查询所有 Index:
▶ curl 'localhost:9200/_mapping?pretty=true'
{
"school" : {
"mappings" : { }
}
}
删除 Index
使用 HTTP DELETE 请求删除 Index:
$ curl -X DELETE 'localhost:9200/school?pretty=true'
{
"acknowledged" : true
}
新增记录
虽然 ES 7 中废弃了 Type 的概念,但是新增数据时仍然可以指定 Type 。这只是为了过渡期的兼容,因为大部分接口已经不支持了。
$ curl -X POST -H "Content-Type: application/json" 'localhost:9200/school/student/1?pretty=true' -d '
{
"name": "张三"
}'
{
"_index" : "school",
"_type" : "student",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
$ curl -X POST -H "Content-Type: application/json" 'localhost:9200/school/student/?pretty=true' -d '
{
"name": "李四"
}'
{
"_index" : "school",
"_type" : "student",
"_id" : "Qeg8omsBnXt8WY5cFy3z",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
再次查看 mapping:
▶ curl 'localhost:9200/_mapping?pretty=true'
{
"school" : {
"mappings" : {
"properties" : {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
注意,查询结果中并没有 student
这个type。
更合理的做法应该是:
curl -X POST -H "Content-Type: application/json" 'localhost:9200/school/_doc/1?pretty=true' -d '
{
"name": "王五"
}'
查询记录
▶ curl 'localhost:9200/school/student/1?pretty=true'
{
"_index" : "school",
"_type" : "student",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "张三"
}
}
▶ curl 'localhost:9200/school/student/Qeg8omsBnXt8WY5cFy3z?pretty=true'
{
"_index" : "school",
"_type" : "student",
"_id" : "Qeg8omsBnXt8WY5cFy3z",
"_version" : 1,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "李四"
}
}
查找不存在的记录:
▶ curl 'localhost:9200/school/student/123?pretty=true'
{
"_index" : "school",
"_type" : "student",
"_id" : "123",
"found" : false
}
查看所有记录:
$ curl -X GET -H "Content-Type: application/json" 'localhost:9200/school/student/_search?size=50&pretty=true' -d '
{
"query" : {
"match_all" : {}
}
}'
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "school",
"_type" : "student",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "张三"
}
},
{
"_index" : "school",
"_type" : "student",
"_id" : "Qeg8omsBnXt8WY5cFy3z",
"_score" : 1.0,
"_source" : {
"name" : "李四"
}
}
]
}
}
更新记录
curl -X POST -H "Content-Type: application/json" 'localhost:9200/school/student/1?pretty=true' -d '
{
"name": "张三换名字了"
}'
{
"_index" : "school",
"_type" : "student",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
▶ curl 'localhost:9200/school/student/1?pretty=true'
{
"_index" : "school",
"_type" : "student",
"_id" : "1",
"_version" : 2,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "张三换名字了"
}
}
删除记录
先创建一条记录:
curl -X POST -H "Content-Type: application/json" 'localhost:9200/school/student/?pretty=true' -d '
{
"name": "测试用户"
}'
{
"_index" : "school",
"_type" : "student",
"_id" : "QuhJomsBnXt8WY5cGi2q",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
查询:
curl 'localhost:9200/school/student/QuhJomsBnXt8WY5cGi2q?pretty=true'
{
"_index" : "school",
"_type" : "student",
"_id" : "QuhJomsBnXt8WY5cGi2q",
"_version" : 1,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "测试用户"
}
}
删除:
curl -X DELETE 'localhost:9200/school/student/QuhJomsBnXt8WY5cGi2q?pretty=true'
{
"_index" : "school",
"_type" : "student",
"_id" : "QuhJomsBnXt8WY5cGi2q",
"_version" : 2,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 1
}
再次查询:
▶ curl 'localhost:9200/school/student/QuhJomsBnXt8WY5cGi2q?pretty=true'
{
"_index" : "school",
"_type" : "student",
"_id" : "QuhJomsBnXt8WY5cGi2q",
"found" : false
}
kibana 简单入门
安装
到官网下载 kibana,解压。
启动
▶ cd kibana-7.2.0
▶ ./bin/kibana
使用 Dev Tools 查询
进入 http://localhost:5601 ,选择 Dev tools。或者直接进入 http://localhost:5601/app/kibana#/dev_tools 。
运行:
GET /school/_mapping
输出:
{
"school" : {
"mappings" : {
"properties" : {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
运行:
GET /school/student/_search
{
"query": {
"match_all": {}
}
}
输出:
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "school",
"_type" : "student",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "张三换名字了"
}
},
{
"_index" : "school",
"_type" : "student",
"_id" : "Qeg8omsBnXt8WY5cFy3z",
"_score" : 1.0,
"_source" : {
"name" : "李四"
}
}
]
}
}
运行:
GET /school/student/1
输出:
#! Deprecation: [types removal] Specifying types in document get requests is deprecated, use the /{index}/_doc/{id} endpoint instead.
{
"_index" : "school",
"_type" : "student",
"_id" : "1",
"_version" : 2,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "张三换名字了"
}
}
GET /school/student/1
这种方式要被废弃了,可以换成 GET /school/_doc/1
。
查询 name 中含有李
的记录:
GET /school/student/_search
{
"query": {
"match": {"name": "李"}
}
}
使用 Dev tools 添加记录
运行:
POST /school/student
{
"name": "王五"
}
输出:
#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{
"_index" : "school",
"_type" : "student",
"_id" : "PKoco2sBUDoa-284Kvwn",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 5,
"_primary_term" : 2
}
可以看到POST /school/student
已经被废弃了(虽然现在还是支持的)。
我们用新的方式创建。
运行:
POST /school/_doc/
{
"name": "李白"
}
输出:
{
"_index" : "school",
"_type" : "_doc",
"_id" : "Paofo2sBUDoa-284nvzx",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 6,
"_primary_term" : 2
}
其实就是 type 是 _doc
,这是 ES 团队选择的一种废弃Type的方案。上文中我们用 curl 命令操作文档,type 的值换成 _doc
即可。