一 序
本文爲極客時間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查詢索引數據