Elasticsearch篇之入门
术语介绍
文档Document(类似于数据库中的一条记录)
用户存储在es中的数据文档
索引Index(类似于数据库中的一个表)
由具有相同字段的文档列表组成
type在6.0之后不允许在Index下面建立多个type,在未来会去除type
节点Node
一个Elasticsearch的运行实例,是集群额构成单元
集群Cluster
由一个或多个节点组成, 对外提供服务
Document介绍
Json Object,由字段(Field)组成,常见数据类型如下:
- 字符串:text,keyword
- 数值型:long,integer,short,byte,double,float,half_float,scaled_float
- 布尔:boolean
- 日期:date
- 二进制:binary
- 范围类型:integer_range,float_range,long_range,double_range,date_range
每一个文档有唯一的id标识
- 自行指定
- es自动生成
Document MetaData
元数据,用于标注文档的相关信息
- _index:文档所在的索引名
- _type:文档所在的类型名
- _id:文档唯一id
- _uid:组合id,由_type和_id组成(6.x _type不再起作用),同_id一样
- source:文档的原始json数据,可以从这里面获取每个字段的内容
- all:整合所有字段内容到该字段,默认禁用
Index介绍
索引中存储具有相同结构的文档(Document)
- 每个索引都有自己的mapping定义,用于定义字段名和类型
一个集群可以有多个索引,比如: - nginx日志存储的时候可以按照日期每天生成一个索引来存储
- nginx-log-2020-01-01
- nginx-log-2020-01-02
- nginx-log-2020-01-03
restapi介绍
Elasticsearch集群对外提供RESTful API
- URI指定资源,如index、document等
- http method指明资源操作类型,如get、post、put、delete等
使用Kibana的DevTools插件
index_api介绍
es有专门的Index API,用于创建、更新、删除索引配置等
test_index索引名
- 创建索引
PUT /test_index
- 查看索引
GET _cat/indices
- 删除索引
DELETE /test_index
document_api介绍
es有专门的Document API
- 创建文档
# 指定文档id插入
PUT books/doc/1
{
"word_count": 1000,
"author": "王帆",
"title": "ElasticSearch",
"publish_date": "2020-01-06"
}
# 自动产生文档id插入
POST books/doc
{
"word_count": 2000,
"author": "小白王帆",
"title": "ElasticSearch小白",
"publish_date": "2020-01-01"
}
- 查询文档
_source存储了文档的完整原始数据
found标识是否找到,如果不存在返回false
took查询耗时,单位ms
total符合条件的总文档数
# 查找所有
GET books/doc/_search
{
"query": {
"match_all": {}
}
}
GET books/doc/_search
{
"query": {
"term": {
"_id": {
"value": "1"
}
}
}
}
# 指定要查询的文档id
GET books/doc/1
id为2的数据不存在,返回如下
GET books/doc/2
- 批量创建文档
es允许一次创建多个文档,从而减少网络传输开销,提升写入速率
endpoint为_bulk,如下:
action_type:index,update,create,delete
如果已存在用create会报错,用index不会(相当于更新)
POST _bulk
{"index":{"_index":"books","_type":"doc","_id":"3"}}
{"word_count":3000,"author":"小白王3","title":"ElasticSearch小3","publish_date":"2020-01-03"}
{"delete":{"_index":"books","_type":"doc","_id":1}}
{"update":{"_index":"books","_type":"doc","_id":2}}
{"doc":{"author":"tom"}}
返回结果items,每个bulk操作的返回结果
errors为true表示有错误,因为id为2的文档不存在
- 批量查询文档
es允许一次查询多个文档
endpoint为_mget,如下:
GET /_mget
{
"docs": [
{
"_index": "books",
"_type": "doc",
"_id": "1"
},
{
"_index": "books",
"_type": "doc",
"_id": "3"
}
]
}
文档不存在时候found为false
所有代码
# 查找所有
GET books/doc/_search
{
"query": {
"match_all": {}
}
}
GET books/doc/_search
{
"query": {
"term": {
"_id": {
"value": "1"
}
}
}
}
# 创建索引
PUT test_index
# 查看现有索引
GET _cat/indices
# 删除索引
DELETE /test_index
# 指定文档id插入
PUT books/doc/1
{
"word_count": 1000,
"author": "王帆",
"title": "ElasticSearch",
"publish_date": "2020-01-06"
}
# 自动产生文档id插入
POST books/doc
{
"word_count": 2000,
"author": "小白王帆",
"title": "ElasticSearch小白",
"publish_date": "2020-01-01"
}
# 指定要查询的文档id
GET books/doc/1
GET books/doc/2
POST _bulk
{"index":{"_index":"books","_type":"doc","_id":"3"}}
{"word_count":3000,"author":"小白王3","title":"ElasticSearch小3","publish_date":"2020-01-03"}
{"delete":{"_index":"books","_type":"doc","_id":1}}
{"update":{"_index":"books","_type":"doc","_id":2}}
{"doc":{"author":"tom"}}
GET /_mget
{
"docs": [
{
"_index": "books",
"_type": "doc",
"_id": "1"
},
{
"_index": "books",
"_type": "doc",
"_id": "3"
}
]
}