一 序
本文为极客时间Elasticsearch核心技术与实战学习笔记系列。
从这一节课开始,老师开始讲述Elasticsearch基本概念。
二 文档
2.1 文档
Elasticsearch 是面向文档,文档是所有搜索数据的最小单元。
文档类似数据库里面的一条长长的存储记录。文档(Document)是索引信息的基本单位。
每个文档都有一个uniqueID,其值不会被索引
2.2 文档元数据
元数据是用于标注文档的相关信息,那么索引文档的元数据如下:
- _index 文档所属索引名称
- _type 文档所属类型名
- _id 文档唯一 ID
- _score 文档相关性打分
- _source 文档 JSON 数据
- _version 文档版本信息
其中 _type 文档所属类型名,需要关注版本不同之间区别:
- 7.0 之前,一个索引可以设置多个 types
- 7.0 开始,被 Deprecated 了。一个索引只能创建一个 type,值为 _doc
三 索引
作为名词,索引代表是在 Elasticsearch 集群中,可以创建很多不同索引。
作为动词,索引代表保存一个文档到 Elasticsearch。就是在 Elasticsearch 创建一个倒排索引的意思
索引,就是相似类型文档的集合。
ES 索引就是文档的容器,是一类文档的集合。
在devtools 执行:GET movies
{
"movies" : {
"aliases" : { },
"mappings" : {
"properties" : {
"@version" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"genre" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"title" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"year" : {
"type" : "long"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1589097356250",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "S0Ic8iLJTjKfL4wd-wGckA",
"version" : {
"created" : "7020099"
},
"provided_name" : "movies"
}
}
}
}
根据返回结果,我们知道:
- mappings:定义文档字段的类型
- settings:定义不同数据分布
- aliases:定义索引的别名,可以通过别名访问该索引
索引,是逻辑空间概念,每个索引有对那个的 Mapping 定义,对应的就是文档的字段名和字段类型。相比后面会讲到分片,是物理空间概念,索引中存储数据会分散到分片上。
跟 MySQL 类比
四 REST API 方便 ES 被各种语言调用
如图,Elasticsearch 提供了 REST API,方便,相关索引 API 如下:
#查看索引相关信息 GET kibana_sample_data_ecommerce #查看索引的文档总数 GET kibana_sample_data_ecommerce/_count #查看前10条文档,了解文档格式 POST kibana_sample_data_ecommerce/_search { } #_cat indices API #查看indices GET /_cat/indices/kibana*?v&s=index #查看状态为绿的索引 GET /_cat/indices?v&health=green #按照文档个数排序 GET /_cat/indices?v&s=docs.count:desc #查看具体的字段 GET /_cat/indices/kibana*?pri&v&h=health,index,pri,rep,docs.count,mt #How much memory is used per index? GET /_cat/indices?v&h=i,tm&s=tm:desc
使用呢dev tools查询索引数据