elasticsearch version: 6.7.
kibana version: 6.7.
注意:es版本和kibana的版本要对应,否则可能会 出现各种意外情况
基本概念
Elasticsearch的文件存储是面向文档型数据库(有点类似 mongodb)
一条数据在这里就是一个文档,用JSON作为文档序列化的格式,比如下面这条用户数据:
{"id" : "1001","name" : "tom","age" : "20"}
和关系型数据库(mysql)术语做对比
| mysql | elasticsearch |
|---|---|
| 数据库(database) | 索引(index) |
| 数据表(table) | 类型(type) |
| 一行数据(row) | 一个文档(doument) |
| 表结构字段(field) | 字段(field) |
常用API及kibana DevTool 使用
如果使用curl命令虽然也是可以操作es的,但是curl命令实在是不方便,所以要借助kibana这个工具,就像一个是你在命令行敲sql语句,另外一个是在phpmyadmin中图形化操作…
查看所有index信息
GET _cat/indices?v
查看所有type信息
GET _mapping?pretty=true
根据ID获取文档
GET /<index>/<type>/<id>#在 7.0 以及之后的版本中 Type 被废弃了。一个 index 中只有一个默认的 type,即 _docGET /<index>/_doc/<id>
索引操作(类似 mysql 数据库操作)
查看索引健康状态
GET _cluster/health?level=indices
创建索引(类似mysql的create database)
PUT users
创建索引并直接设置索引的设置信息
PUT books{"settings": {"number_of_shards": 10,"number_of_replicas": 6}}
删除索引(类似mysql的 drop database)
DELETE users
查看索引设置(类似mysql的 show create)
GET users/_settings
设置索引信息
PUT users/_settings{ "number_of_replicas": 1 }
将备份分片设置为只有一个(分片可以简单理解为分支,类似git)
检查一个索引是否存在(类似mysql: if table exists)
HEAD users
关闭一个索引(关闭索引后不能在读写 索引中的文档)
POST users/_close
开启一个索引(将一个关闭的索引从新开启)
POST users/_open
文档操作 (类似mysql: 数据操作curd)
创建一个文档(类似mysql: insert)
PUT users/user/1{"name":"zhangShang","age" : 20,"sex" : 1}
注意:在 es 6.* 以上版本 一个 index 中只能有一个 type,如果执行上面这条命令之后,在执行下面这个命令就会报错
POST users/user{"name" : "hello","age" : 10,"sex" : 0}
来看看响应的结果:
{"error": {"root_cause": [{"type": "illegal_argument_exception","reason": "Rejecting mapping update to [users] as the final mapping would have more than 1 type: [user, userinfo]"}],"type": "illegal_argument_exception","reason": "Rejecting mapping update to [users] as the final mapping would have more than 1 type: [user, userinfo]"},"status": 400}
创建多个文档(类似mysql: insert 多个 values)
POST books/name/_bulk{"index": {"_id": 1}}{"name": "《PHP之道》"}{"index": {"_id": 2}}{"name": "《代码简洁之道》"}
查看一个索引的所有文档 (类似mysql: select)
GET books/_search
查看指定id的文档
GET books/book/2
修改文档(类似mysql: update)
post 方式
POST books/name/_bulk{"index": {"_id": "3"}}{"update": "hello"}
put 方式
PUT books/name/3{"name": "java"}
删除文档
# 删除 _id 为3的这个文档DELETE books/name/3
批量导入测试数据
测试数据
https://raw.githubusercontent.com/elastic/elasticsearch/master/docs/src/test/resources/accounts.json
http://download.elastic.co/demos/kibana/gettingstarted/shakespeare_6.0.json
使用 curl命令 导入数据
curl -H 'Content-Type:application/json' -XPOST '192.168.10.10:9200/shakespeare/novels/_bulk' --data-binary @acc.json
单条件搜索
GET bank/account/_search?q=firstname:Virginia
搜索bank这个索引中,所有firstname字段为Virginia的所有文档
GET bank/account/_search?q=firstname:Virginia,Alex
搜索bank这个索引中,所有firstname字段为Virginia 或者 Alex的所有文档
模糊搜索(不指定字段,搜索出所有字段中包涵指定字符串)
#1GET bank/account/_search?q=jones# 2GET bank/account/_search?q=jones pace# 3GET bank/account/_search?q="jones pace"
1:搜索bank这个索引中,所有字段包涵jones这个字符的文档
2:搜索bank这个索引中,所有字段包涵jones 或者 pace字符的文档
2:搜索bank这个索引中,所有字段包涵jones pace这个字符的文档
多条件搜索
GET bank/account/_search?q=(firstname:Virginia AND lastname:Ayala)
搜索bank这个索引中,所有firstname字段为Virginia 并且 lastname字段为Ayala的所有文档
以上这种形式的搜索相当于在地址栏中输入参数,非常局限,了解即可
多条件搜索(query)
# 1GET bank/account/_search{"query": {"match": {# 普通搜索"firstname": {"query": "virginia jones",# 搜索的关键词"operator": "or"# 操作: or and}}}}# 2GET bank/account/_search{"query": {"match_phrase": {# 词组搜索"address": "Baycliff Terrace"}}}# 3GET bank/account/_search{"query": {"multi_match": {# 多字段词组搜索"query": "Baycliff Terrace","fields": ["address", "firstname", "lastname"]}}}
1:搜索bank这个索引中,所有firstname字段为Virginia或者Ayala的所有文档
2:搜索bank这个索引中,所有address字段包涵 Virginia Ayala的所有文档
3: 搜索bank这个索引中,字段address,firstanme,lastname中含有 Baycliff 或者 Terrace 的所有文档
关键字搜索
如果使用上面的方式搜索,就会搜索出,所有包涵指定字符串的文档, 如果想要某个字段自能是某个字符串的时候,就需要使用使用 term的方式搜索并且使用 .keyword 修饰修饰字段, 此时如果使用 query 的方式搜索是搜索不到的
GET bank/account/_search{"query": {"term": {"address.keyword": {"value": "171 Putnam Avenue"}}}}
字段数据类型
string 字符串
text (会被拆分然后分析)
keyword (不会被拆分)
number 数值
long
integer
short
byte
double
float
half_float
scaled_float
date 日期
range 范围值
integer_range
float_range
long_range
double_range
date_range
array 数组
复杂数据类型
object 对象
nested json 对象数组
geo 地理位置
IP 地址
