前幾天寫過一篇《Elasticsearch 7.x 最詳細安裝及配置》,今天繼續最新版基礎入門內容。這一篇簡單總結了 Elasticsearch 7.x 之文檔、索引和 REST API。html
從使用案例出發,Elasticsearch 是面向文檔,文檔是全部搜索數據的最小單元。數據庫
等等案例不少,那麼文檔就是相似數據庫裏面的一條長長的存儲記錄。文檔(Document)是索引信息的基本單位。api
文檔被序列化成爲 JSON 格式,物理保存在一個索引中。JSON 是一種常見的互聯網數據交換格式:app
每一個文檔都會有一個 Unique ID,其字段名稱爲 _id
:less
PUT my_index/_doc/1
{
"text": "Document with ID 1"
}
PUT my_index/_doc/2&refresh=true
{
"text": "Document with ID 2"
}
GET my_index/_search
{
"query": {
"terms": {
"_id": [ "1", "2" ]
}
}
}複製代碼
元數據是用於標註文檔的相關信息,那麼索引文檔的元數據以下:socket
其中 _type 文檔所屬類型名,須要關注版本不一樣之間區別:elasticsearch
做爲名詞,索引表明是在 Elasticsearch 集羣中,能夠建立不少不一樣索引。也是本小節要總結的內容。ide
做爲動詞,索引表明保存一個文檔到 Elasticsearch。就是在 Elasticsearch 建立一個倒排索引的意思工具
索引,就是類似類型文檔的集合。相似 Spring Bean 容器裝載着不少 Bean ,ES 索引就是文檔的容器,是一類文檔的集合。網站
之前導入了 kibanasampledata_flights 索引,經過 GET 下面這個 URL ,就能獲得索引一些信息:
GET http://localhost:9200/kibana_sample_data_flights複製代碼
結果以下:
{
"kibana_sample_data_flights": {
"aliases": {},
"mappings": {
"properties": {
"AvgTicketPrice": {
"type": "float"
},
"Cancelled": {
"type": "boolean"
},
"Carrier": {
"type": "keyword"
},
"DestLocation": {
"type": "geo_point"
},
"FlightDelay": {
"type": "boolean"
},
"FlightDelayMin": {
"type": "integer"
},
"timestamp": {
"type": "date"
}
}
},
"settings": {
"index": {
"number_of_shards": "1",
"auto_expand_replicas": "0-1",
"blocks": {
"read_only_allow_delete": "true"
},
"provided_name": "kibana_sample_data_flights",
"creation_date": "1566271868125",
"number_of_replicas": "0",
"uuid": "SfR20UNiSLKJWIpR1bcrzQ",
"version": {
"created": "7020199"
}
}
}
}
}複製代碼
根據返回結果,咱們知道:
索引,是邏輯空間概念,每一個索引有對那個的 Mapping 定義,對應的就是文檔的字段名和字段類型。相比後面會講到分片,是物理空間概念,索引中存儲數據會分散到分片上。
實戰經驗總結:aliases 別名大有做爲,好比 myindex 遷移到 myindex_new , 數據遷移後,只須要保持一致的別名配置。那麼經過別名訪問索引的業務方都不須要修改,直接遷移便可。
基本理解了 Elasticsearch 重要的兩個概念,能夠將 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複製代碼
具體 API 能夠經過 POSTMan 等工具操做,或者安裝 kibana ,對應的 Dev Tools工具進行訪問。
(完),更多能夠看 ES 7.x 系列教程 bysocket.com
資料:
本文由博客一文多發平臺 OpenWrite 發佈!