本文介紹了ElasticSearch的必備知識:從入門、索引管理到映射詳解。html
http://localhost:9200/_catnode
http://localhost:9200/_cat/he...數據庫
說明:v是用來要求在結果中返回表頭json
#狀態值說明 Green - everything is good (cluster is fully functional),即最佳狀態 Yellow - all data is available but some replicas are not yet allocated (cluster is fully functional),即數據和集羣可用,可是集羣的備份有的是壞的 Red - some data is not available for whatever reason (cluster is partially functional),即數據和集羣都不可用
http://localhost:9200/_cat/?vapi
http://localhost:9200/_cat/in...數組
建立一個名爲 customer 的索引。pretty要求返回一個漂亮的json 結果緩存
PUT /customer?prettyapp
再查看一下全部索引curl
http://localhost:9200/_cat/in...elasticsearch
GET /_cat/indices?v
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d' { "name": "John Doe" } '
curl -X GET "localhost:9200/customer/_doc/1?pretty"
GET /customer/_search?q=*&sort=name:asc&pretty
JSON格式方式
GET /customer/_search { "query": { "match_all": {} }, "sort": [ {"name": "asc" } ] }
建立一個名爲twitter的索引,設置索引的分片數爲3,備份數爲2。注意:在ES中建立一個索引相似於在數據庫中創建一個數據庫(ES6.0以後相似於建立一個表)
PUT twitter { "settings" : { "index" : { "number_of_shards" : 3, "number_of_replicas" : 2 } } }
說明:
建立結果:
建立的命令還能夠簡寫爲
PUT twitter { "settings" : { "number_of_shards" : 3, "number_of_replicas" : 2 } }
##### 2. 建立mapping映射
注意:在ES中建立一個mapping映射相似於在數據庫中定義表結構,即表裏面有哪些字段、字段是什麼類型、字段的默認值等;也相似於solr裏面的模式schema的定義
PUT twitter { "settings" : { "index" : { "number_of_shards" : 3, "number_of_replicas" : 2 } }, "mappings" : { "type1" : { "properties" : { "field1" : { "type" : "text" } } } } }
PUT twitter { "aliases" : { "alias_1" : {}, "alias_2" : { "filter" : { "term" : {"user" : "kimchy" } }, "routing" : "kimchy" } } }
GET /twitter,能夠一次獲取多個索引(以逗號間隔)獲取全部索引_all或用通配符*
GET /twitter/_settings
GET /twitter/_mapping
DELETE /twitter
說明:能夠一次刪除多個索引(以逗號間隔) 刪除全部索引 _all 或 通配符 *
HEAD twitter
HTTP status code 表示結果 404 不存在 , 200 存在
索引的設置信息分爲靜態信息和動態信息兩部分。靜態信息不可更改,如索引的分片數。動態信息能夠修改。
REST 訪問端點: /_settings 更新全部索引的。 {index}/_settings 更新一個或多個索引的settings。
詳細的設置項請參考:https://www.elastic.co/guide/...
PUT /twitter/_settings { "index" : { "number_of_replicas" : 2 } }
PUT /twitter/_settings { "index" : { "refresh_interval" : null } }
index.blocks.read_only:設爲true,則索引以及索引的元數據只可讀 index.blocks.read_only_allow_delete:設爲true,只讀時容許刪除。 index.blocks.read:設爲true,則不可讀。 index.blocks.write:設爲true,則不可寫。 index.blocks.metadata:設爲true,則索引元數據不可讀寫。
在建立索引時,爲每一個索引寫定義信息多是一件繁瑣的事情,ES提供了索引模板功能,讓你能夠定義一個索引模板,模板中定義好settings、mapping、以及一個模式定義來匹配建立的索引。
注意:模板只在索引建立時被參考,修改模板不會影響已建立的索引
12.1 新增/修更名爲tempae_1的模板,匹配名稱爲te 或 bar的索引建立:
PUT _template/template_1 { "index_patterns": ["te*", "bar*"], "settings": { "number_of_shards": 1 }, "mappings": { "type1": { "_source": { "enabled": false }, "properties": { "host_name": { "type": "keyword" }, "created_at": { "type": "date", "format": "EEE MMM dd HH:mm:ss Z YYYY" } } } } }
12.2 查看索引模板
GET /_template/template_1 GET /_template/temp* GET /_template/template_1,template_2 GET /_template
12.3 刪除模板
DELETE /_template/template_1
POST /my_index/_close POST /my_index/_open
說明:
索引的分片數是不可更改的,如要減小分片數能夠經過收縮方式收縮爲一個新的索引。新索引的分片數必須是原分片數的因子值,如原分片數是8,則新索引的分片數能夠爲四、二、1 。
何時須要收縮索引呢? 最初建立索引的時候分片數設置得太大,後面發現用不了那麼多分片,這個時候就須要收縮了
收縮的流程:
收縮前的準備工做:
將原索引各分片的一個副本重分配到同一個節點上,而且要是健康綠色狀態。
PUT /my_source_index/_settings { "settings": { <!-- 指定進行收縮的節點的名稱 --> "index.routing.allocation.require._name": "shrink_node_name", <!-- 阻止寫,只讀 --> "index.blocks.write": true } }
進行收縮:
POST my_source_index/_shrink/my_target_index { "settings": { "index.number_of_replicas": 1, "index.number_of_shards": 1, "index.codec": "best_compression" }}
監控收縮過程:
GET _cat/recovery?v GET _cluster/health
當索引的分片容量過大時,能夠經過拆分操做將索引拆分爲一個倍數分片數的新索引。能拆分爲幾倍由建立索引時指定的index.number_of_routing_shards 路由分片數決定。這個路由分片數決定了根據一致性hash路由文檔到分片的散列空間。
如index.number_of_routing_shards = 30 ,指定的分片數是5,則可按以下倍數方式進行拆分:
5 → 10 → 30 (split by 2, then by 3) 5 → 15 → 30 (split by 3, then by 2) 5 → 30 (split by 6)
爲何須要拆分索引?
當最初設置的索引的分片數不夠用時就須要拆分索引了,和壓縮索引相反
注意:只有在建立時指定了index.number_of_routing_shards 的索引才能夠進行拆分,ES7開始將再也不有這個限制。
和solr的區別是,solr是對一個分片進行拆分,es中是整個索引進行拆分。
準備一個索引來作拆分:
PUT my_source_index { "settings": { "index.number_of_shards" : 1, <!-- 建立時須要指定路由分片數 --> "index.number_of_routing_shards" : 2 } }
先設置索引只讀:
PUT /my_source_index/_settings { "settings": { "index.blocks.write": true } }
作拆分:
POST my_source_index/_split/my_target_index { "settings": { <!--新索引的分片數需符合拆分規則--> "index.number_of_shards": 2 } }
監控拆分過程:
GET _cat/recovery?v GET _cluster/health
對於有時效性的索引數據,如日誌,過必定時間後,老的索引數據就沒有用了。咱們能夠像數據庫中根據時間建立表來存放不一樣時段的數據同樣,在ES中也可用建多個索引的方式來分開存放不一樣時段的數據。比數據庫中更方便的是ES中能夠經過別名滾動指向最新的索引的方式,讓你經過別名來操做時老是操做的最新的索引。
ES的rollover index API 讓咱們能夠根據知足指定的條件(時間、文檔數量、索引大小)建立新的索引,並把別名滾動指向新的索引。
注意:這時的別名只能是一個索引的別名。
Rollover Index 示例:建立一個名字爲logs-0000001 、別名爲logs_write 的索引:
PUT /logs-000001 { "aliases": { "logs_write": {} } }
添加1000個文檔到索引logs-000001,而後設置別名滾動的條件
POST /logs_write/_rollover { "conditions": { "max_age": "7d", "max_docs": 1000, "max_size": "5gb" } }
說明:若是別名logs_write指向的索引是7天前(含)建立的或索引的文檔數>=1000或索引的大小>= 5gb,則會建立一個新索引 logs-000002,並把別名logs_writer指向新建立的logs-000002索引
Rollover Index 新建索引的命名規則:
若是索引的名稱不是-數值結尾,則在請求rollover api時需指定新索引的名稱
POST /my_alias/_rollover/my_new_index_name { "conditions": { "max_age": "7d", "max_docs": 1000, "max_size": "5gb" } }
在名稱中使用Date math(時間表達式)
若是你但願生成的索引名稱中帶有日期,如logstash-2016.02.03-1 ,則能夠在建立索引時採用時間表達式來命名:
# PUT /<logs-{now/d}-1> with URI encoding: PUT /%3Clogs-%7Bnow%2Fd%7D-1%3E { "aliases": { "logs_write": {} } } PUT logs_write/_doc/1 { "message": "a dummy log" } POST logs_write/_refresh # Wait for a day to pass POST /logs_write/_rollover { "conditions": { "max_docs": "1" } }
Rollover時可對新的索引做定義:
PUT /logs-000001 { "aliases": { "logs_write": {} } } POST /logs_write/_rollover { "conditions" : { "max_age": "7d", "max_docs": 1000, "max_size": "5gb" }, "settings": { "index.number_of_shards": 2 } }
Dry run 實際操做前先測試是否達到條件:
POST /logs_write/_rollover?dry_run { "conditions" : { "max_age": "7d", "max_docs": 1000, "max_size": "5gb" } }
說明:測試不會建立索引,只是檢測條件是否知足
注意:rollover是你請求它纔會進行操做,並非自動在後臺進行的。你能夠週期性地去請求它。
官網連接:https://www.elastic.co/guide/...
查看全部的索引狀態:
GET /_stats
查看指定索引的狀態信息:
GET /index1,index2/_stats
官網連接:https://www.elastic.co/guide/...
GET /test/_segments GET /index1,index2/_segments GET /_segments
官網連接:https://www.elastic.co/guide/...
GET index1,index2/_recovery?human GET /_recovery?human
官網連接:https://www.elastic.co/guide/...
# return information of only index test GET /test/_shard_stores # return information of only test1 and test2 indices GET /test1,test2/_shard_stores # return information of all indices GET /_shard_stores GET /_shard_stores?status=green
18.1 Clear Cache 清理緩存
POST /twitter/_cache/clear
默認會清理全部緩存,可指定清理query, fielddata or request 緩存
POST /kimchy,elasticsearch/_cache/clear POST /_cache/clear
18.2 Refresh,從新打開讀取索引
POST /kimchy,elasticsearch/_refresh POST /_refresh
18.3 Flush,將緩存在內存中的索引數據刷新到持久存儲中
POST twitter/_flush
18.4 Force merge 強制段合併
POST /kimchy/_forcemerge?only_expunge_deletes=false&max_num_segments=100&flush=true
可選參數說明:
POST /kimchy,elasticsearch/_forcemerge POST /_forcemerge
映射定義索引中有什麼字段、字段的類型等結構信息。至關於數據庫中表結構定義,或 solr中的schema。由於lucene索引文檔時須要知道該如何來索引存儲文檔的字段。
ES中支持手動定義映射,動態映射兩種方式。
1.1. 爲索引建立mapping
PUT test { <!--映射定義 --> "mappings" : { <!--名爲type1的映射類別 mapping type--> "type1" : { <!-- 字段定義 --> "properties" : { <!-- 名爲field1的字段,它的field datatype 爲 text --> "field1" : { "type" : "text" } } } } }
說明:映射定義後續能夠修改
ES最早的設計是用索引類比關係型數據庫的數據庫,用mapping type 來類比表,一個索引中能夠包含多個映射類別。這個類比存在一個嚴重的問題,就是當多個mapping type中存在同名字段時(特別是同名字段仍是不一樣類型的),在一個索引中很差處理,由於搜索引擎中只有 索引-文檔的結構,不一樣映射類別的數據都是一個一個的文檔(只是包含的字段不同而已)
從6.0.0開始限定僅包含一個映射類別定義( "index.mapping.single_type": true ),兼容5.x中的多映射類別。從7.0開始將移除映射類別。
爲了與將來的規劃匹配,請如今將這個惟一的映射類別名定義爲「_doc」,由於索引的請求地址將規範爲:
PUT {index}/_doc/{id} and POST {index}/_doc
Mapping 映射示例:
PUT twitter { "mappings": { "_doc": { "properties": { "type": { "type": "keyword" }, "name": { "type": "text" }, "user_name": { "type": "keyword" }, "email": { "type": "keyword" }, "content": { "type": "text" }, "tweeted_at": { "type": "date" } } } } }
多映射類別數據轉儲到獨立的索引中:
ES 提供了reindex API 來作這個事
字段類型定義了該如何索引存儲字段值。ES中提供了豐富的字段類型定義,請查看官網連接詳細瞭解每種類型的特色:
https://www.elastic.co/guide/...
3.1 Core Datatypes 核心類型
string text and keyword Numeric datatypes long, integer, short, byte, double, float, half_float, scaled_float Date datatype date Boolean datatype boolean Binary datatype binary Range datatypes 範圍 integer_range, float_range, long_range, double_range, date_range
3.2 Complex datatypes 複合類型
Array datatype 數組就是多值,不須要專門的類型 Object datatype object :表示值爲一個JSON 對象 Nested datatype nested:for arrays of JSON objects(表示值爲JSON對象數組 )
3.3 Geo datatypes 地理數據類型
Geo-point datatype geo_point:for lat/lon points (經緯座標點) Geo-Shape datatype geo_shape:for complex shapes like polygons (形狀表示)
3.4 Specialised datatypes 特別的類型
IP datatype ip:for IPv4 and IPv6 addresses Completion datatype completion:to provide auto-complete suggestions Token count datatype token_count:to count the number of tokens in a string mapper-murmur3 murmur3:to compute hashes of values at index-time and store them in the index Percolator type Accepts queries from the query-dsl join datatype Defines parent/child relation for documents within the same index
字段的type (Datatype)定義瞭如何索引存儲字段值,還有一些屬性可讓咱們根據須要來覆蓋默認的值或進行特別定義。請參考官網介紹詳細瞭解:https://www.elastic.co/guide/...
analyzer 指定分詞器 normalizer 指定標準化器 boost 指定權重值 coerce 強制類型轉換 copy_to 值複製給另外一字段 doc_values 是否存儲docValues dynamic enabled 字段是否可用 fielddata eager_global_ordinals format 指定時間值的格式 ignore_above ignore_malformed index_options index fields norms null_value position_increment_gap properties search_analyzer similarity store term_vector
字段定義屬性—示例
PUT my_index { "mappings": { "_doc": { "properties": { "date": { "type": "date", <!--格式化日期 --> "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" } } } } }
當咱們須要對一個字段進行多種不一樣方式的索引時,可使用fields多重字段定義。如一個字符串字段即須要進行text分詞索引,也須要進行keyword 關鍵字索引來支持排序、聚合;或須要用不一樣的分詞器進行分詞索引。
示例:定義多重字段:
說明:raw是一個多重版本名(自定義)
PUT my_index { "mappings": { "_doc": { "properties": { "city": { "type": "text", "fields": { "raw": { "type": "keyword" } } } } } } }
往多重字段裏面添加文檔
PUT my_index/_doc/1 { "city": "New York" } PUT my_index/_doc/2 { "city": "York" }
獲取多重字段的值:
GET my_index/_search { "query": { "match": { "city": "york" } }, "sort": { "city.raw": "asc" }, "aggs": { "Cities": { "terms": { "field": "city.raw" } } } }
官網連接:https://www.elastic.co/guide/...
元字段是ES中定義的文檔字段,有如下幾類:
動態映射:ES中提供的重要特性,讓咱們能夠快速使用ES,而不須要先建立索引、定義映射。如咱們直接向ES提交文檔進行索引:
PUT data/_doc/1 { "count": 5 }
ES將自動爲咱們建立data索引、_doc 映射、類型爲 long 的字段 count
索引文檔時,當有新字段時, ES將根據咱們字段的json的數據類型爲咱們自動加人字段定義到mapping中。
所謂時間偵測是指咱們往ES裏面插入數據的時候會去自動檢測咱們的數據是否是日期格式的,是的話就會給咱們自動轉爲設置的格式
date_detection 默認是開啓的,默認的格式dynamic_date_formats爲:
[ "strict_date_optional_time","yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"] PUT my_index/_doc/1 { "create_date": "2015/09/02" } GET my_index/_mapping
自定義時間格式:
PUT my_index { "mappings": { "_doc": { "dynamic_date_formats": ["MM/dd/yyyy"] } } }
禁用時間偵測:
PUT my_index { "mappings": { "_doc": { "date_detection": false } } }
開啓數值偵測(默認是禁用的)
PUT my_index { "mappings": { "_doc": { "numeric_detection": true } } } PUT my_index/_doc/1 { "my_float": "1.0", "my_integer": "1" }
PUT /logs_20162801 { "mappings" : { "type" : { "properties" : { "year" : {"type" : "integer"} } } }, <!-- 定義了兩個別名 --> "aliases" : { "current_day" : {}, "2016" : { "filter" : { "term" : {"year" : 2016 } } } } }
爲索引test1建立別名alias1
POST /_aliases { "actions" : [ { "add" : { "index" : "test1", "alias" : "alias1" } } ] }
POST /_aliases { "actions" : [ { "remove" : { "index" : "test1", "alias" : "alias1" } } ] }
還能夠這樣寫
DELETE /{index}/_alias/{name}
刪除索引test1的別名alias1,同時爲索引test2添加別名alias1
POST /_aliases { "actions" : [ { "remove" : { "index" : "test1", "alias" : "alias1" } }, { "add" : { "index" : "test2", "alias" : "alias1" } } ] }
方式1:
POST /_aliases { "actions" : [ { "add" : { "index" : "test1", "alias" : "alias1" } }, { "add" : { "index" : "test2", "alias" : "alias1" } } ] }
方式2:
POST /_aliases { "actions" : [ { "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } } ] }
注意:只可經過多索引別名進行搜索,不可進行文檔索引和根據id獲取文檔。
方式3:經過統配符*模式來指定要別名的索引
POST /_aliases { "actions" : [ { "add" : { "index" : "test*", "alias" : "all_test_indices" } } ] }
注意:在這種狀況下,別名是一個點時間別名,它將對全部匹配的當前索引進行別名,當添加/刪除與此模式匹配的新索引時,它不會自動更新。
索引中須要有字段
PUT /test1 { "mappings": { "type1": { "properties": { "user" : { "type": "keyword" } } } } }
過濾器經過Query DSL來定義,將做用於經過該別名來進行的全部Search, Count, Delete By Query and More Like This 操做。
POST /_aliases { "actions" : [ { "add" : { "index" : "test1", "alias" : "alias2", "filter" : { "term" : { "user" : "kimchy" } } } } ] }
可在別名定義中指定路由值,可和filter一塊兒使用,用來限定操做的分片,避免不須要的其餘分片操做。
POST /_aliases { "actions" : [ { "add" : { "index" : "test", "alias" : "alias1", "routing" : "1" } } ] }
爲搜索、索引指定不一樣的路由值
POST /_aliases { "actions" : [ { "add" : { "index" : "test", "alias" : "alias2", "search_routing" : "1,2", "index_routing" : "2" } } ] }
PUT /{index}/_alias/{name} PUT /logs_201305/_alias/2013
帶filter 和 routing
PUT /users { "mappings" : { "user" : { "properties" : { "user_id" : {"type" : "integer"} } } } }
PUT /users/_alias/user_12 { "routing" : "12", "filter" : { "term" : { "user_id" : 12 } } }
GET /{index}/_alias/{alias} GET /logs_20162801/_alias/* GET /_alias/2016 GET /_alias/20*
原文:https://blog.csdn.net/ZYC8888...