1,基礎概念
1)由Java開發,基于Restful Web接口,分布式的多用戶,可擴展,高可用的全文搜索和分析引擎。
store, search, and analyze big volumes of data
eg:收集日志,分析日志;收集、分析交易數據;存儲數據庫大表到es,用于檢索和聚合。
2)基本概念。
---Near Realtime NRT近實時,從索引文檔到該文檔可被查找,通常在1s內。
---Cluster 集群,共享同一個cluster name的node(server)集合,集群保存了所有的數據,并在各節點之上提供索引和搜索功能。一個節點只能屬于一個集群
查看集群的健康狀態curl 172.17.6.11:9200/_cluster/health?pretty
查看具體索引的健康狀態curl 172.17.6.11:9200/_cluster/health/dby_course_info?pretty
---Node 節點,一個啟動時分配uuid的server。存儲數據,參與集群的索引和查找。每個節點都能使用http和客戶端通信,使用tcp和內部節點通信
---Index 索引,有相同特點的documents的集合。名稱必須小寫
,通過name標識,來操作(索引,查找,更新,刪除
)index內的document。(相當于db)
---Type 類型,一個Index可以擁有多個type,可以定義為擁有共同字段(field)的文檔集合。(相當于table)
---Document 文檔,使用JSON描述的,可以被索引的基本信息單元。(相當于記錄)
---Field域,
3)Shard分片和Replica副本。
---目的:水平拆分和擴展容量;支持在多個分片之間并發和并行操作,提高吞吐量。
創建索引時可以指定number_of_shards
主分片的數量,默認為5個(一個分片的容量可達到20億條文檔
)。
---Replica 副本,是一個index所有shards的拷貝,創建索引時可以指定number_of_replicas
副本的數量,默認為1。(5個primary shard, 5個replica shard)。
---replicas機制是索引級別的,可以動態修改replicas副本的數量,但是不能修改分片的個數。
---目的:解決node/shard宕掉之后的高可用;提高查詢的并發,可以并行在所有副本上執行查找。
2,Mapping映射(相當于表結構)
1)一個Index可以有多個Mapping type,每個document都屬于一個Type類型,每個Type都有自己的Mapping映射,存儲field type和field信息。
image.png
dynamic mapping 規則,可以不定義直接使用mapping。image.png
2)查看type對應的mapping映射curl 172.17.6.11:9200/dby_course_info/_mapping/course_info?pretty
。查看index中type對應的mapping。
3)mapping還可以設置關聯到type類型上的元數據,Meta-fields元數據字段,以下劃線開頭,eg: _index, _type, _id, and _source fields.image.png
properties屬性(fields)。
{
"properties": {
"roomId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
4)創建mapping。image.png
3,template模板
1)查看模板。
curl 172.17.6.11:9200/_template/dby_template?pretty
"template" : "dby_*" : 聲明模板對那些index有效。
2)模板包括setting和mapping。對于所有te開頭的index都生效。
當創建一個新的索引,以te開頭時,匹配成功后會按照settings和mappings創建一個新的索引。
{
"template": "te*",
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"type1": {
"_source": {
"enabled": false
}
}
}
}