Elasticsearch的檔案、索引和rest api
阿新 • • 發佈:2019-12-31
基本概念
檔案(document)
-
elasticsearch 是面向檔案的,檔案是所有可搜尋資料的最小單位
例如:
- 日誌檔案中的日誌項
- 一部電影中的具體資訊
- 一首歌/一個PDF檔案的具體內容
-
檔案會被序列化為JSON格式,儲存在Elasticsearch中
- JSON物件由欄位組成
- 每個欄位都有相應的欄位型別(字串/數值/布林/日期/二機制/範圍型別)
-
每個檔案都有一個Unique ID
- 可以自己指定ID
- 或者通過Elasticsearch自動生成
JSON檔案
- 一篇檔案包含了一系列的欄位,類似於資料庫中的一條記錄
- JSON 檔案,格式靈活,不需要預先定義格式
- 欄位的型別可以指定或者通過Elasticsearch自動推算
- 支援資料/支援巢狀
movieId,title,genres
1,Toy Story(1995),AdvenTure|Animation|Children|Comedy|Fantasy
複製程式碼
- csv file通過es轉為json
{
"year" : 1995,"@version" : 1,"genres" : [
"AdvenTure","Animation","Children","Comedy","Fantasy"
],"id" : "1","title" : "Tony Story"
}
複製程式碼
檔案的元資料
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "1",
"_score" : "14.626",
"_source" : {
"year" : 1995,"genres" : [
"AdvenTure","Fantasy"
],"title" : "Tony Story"
}
}
複製程式碼
- 元資料,用於標註檔案的相關資訊
- _index : 檔案所屬的索引名
- _type : 檔案所屬的型別名
- _id : 檔案唯一ID
- _source : 檔案的原始Json資料
- _all : 整合所有欄位到該欄位(已被廢除)
- _version : 檔案的版本資訊
- _score : 相關性打分
索引
{
"movies" : {
"settings" : {
"index" : {
"create_date" : "15526261177","number_of_shards" : "2","number_of_replicas" : "0","uuid" : "","verison" : {
"created" : "302302"
},"provided_name" : "movies"
}
}
}
}
複製程式碼
- Index-索引是檔案的容器,是一類檔案的結合
- Index 體現了邏輯空間的概念:每個索引都有自己的Mapping定義,用於定義包含的檔案的欄位名和欄位型別
- Shard提現了物理空間的概念:索引中的資料分散在shard上
- 索引的mapping 和Settings
- Mapping 定義檔案欄位的型別
- Setting 定義不同的資料分佈
索引的不同語意
- 一個Elasticsearch 叢集中,可以建立很多個不同的索引
- 儲存一個檔案到Elasticsearch的過程也叫索引(indexing)
- es中,建立一個倒排索引的過程
- 一個B樹索引,一個倒排索引
課程Demo
- 需要通過Kibana匯入Sample Data的電商資料。具體參考 Kibana的安裝與介面快速瀏覽
Index 相關 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
複製程式碼
Type
- 7.0之前,一個index可以設定多個types
- 7.0之後,一個索引只能建立一個Type-"_doc"
抽象和類比
RDBMS | Elasticsearch |
---|---|
Table | Index(Type) |
Row | Document |
Column | Field |
Schema | Mapping |
SQL | DSL |
傳統關係型資料庫和Elasticsearch的區別
- Elasticsearch - Schemaless /相關性/高效能全文檢索
- RDMS - 事務性/Join
Rest API-被各種語言呼叫
一些基本的API
- Indices
- 建立Index
- PUT movies
- 檢視所有Index
- _cat/indices
- 建立Index
部分操作
在kibana的 開發工具
中執行
// 檢視索引相關資訊
GET kibana_sample_data_ecommerce
// 檢視索引的檔案總數
GET kibana_sample_data_ecommerce/_count
// 檢視前10條檔案,瞭解檔案格式
POST kibana_sample_data_ecommerce/_search
{
}
//_cat indeices 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&h=health,mt
// 每個索引所佔的記憶體空間
GET /_cat/indices?v&h=i,tm&s=tm:desc
複製程式碼
相關閱讀
- 為什麼不再支援單個Index下,多個Tyeps www.elastic.co/cn/blog/mov…
- CAT Index API www.elastic.co/guide/en/el…