久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

ElasticSearch使用過程是怎樣的

164次閱讀
沒有評論

共計 11209 個字符,預計需要花費 29 分鐘才能閱讀完成。

ElasticSearch 使用過程是怎樣的,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面丸趣 TV 小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

這里介紹 ElasticSearch 的必備知識:從入門、索引管理到映射詳解。

一、快速入門

1. 查看集群的健康狀況

http://localhost:9200/_cat

http://localhost:9200/_cat/health?v

說明:v 是用來要求在結果中返回表頭

狀態值說明

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/?v

2. 查看所有索引

3. 創建一個索引

創建一個名為 customer 的索引。pretty 要求返回一個漂亮的 json 結果

PUT /customer?pretty

再查看一下所有索引

GET /_cat/indices?v

4. 索引一個文檔到 customer 索引中

curl -X PUT  localhost:9200/customer/_doc/1?pretty  -H  Content-Type: application/json  -d  {  name :  John Doe  } 

5. 從 customer 索引中獲取指定 id 的文檔

curl -X GET  localhost:9200/customer/_doc/1?pretty

6. 查詢所有文檔

GET /customer/_search?q=* sort=name:asc pretty

JSON 格式方式

GET /customer/_search {  query : {  match_all : {} },  sort : [ { name :  asc  } ] }

二、索引管理

 1. 創建索引

創建一個名為 twitter 的索引,設置索引的分片數為 3,備份數為 2。注意:在 ES 中創建一個索引類似于在數據庫中建立一個數據庫 (ES6.0 之后類似于創建一個表)

PUT twitter {  settings  : {  index  : {  number_of_shards  : 3,  number_of_replicas  : 2 } } }

說明:

默認的分片數是 5 到 1024

默認的備份數是 1

索引的名稱必須是小寫的,不可重名

創建結果:

創建的命令還可以簡寫為

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  } } } } }

 3. 創建索引時加入別名定義

PUT twitter {  aliases  : {  alias_1  : {},  alias_2  : {  filter  : {  term  : { user  :  kimchy  } },  routing  :  kimchy  } } }

4. 創建索引時返回的結果說明

5. Get Index 查看索引的定義信息

GET /twitter,可以一次獲取多個索引(以逗號間隔)獲取所有索引 _all 或 用通配符 *

GET /twitter/_settings

GET /twitter/_mapping

6. 刪除索引

DELETE /twitter

說明:

可以一次刪除多個索引(以逗號間隔)刪除所有索引 _all 或 通配符 *

7. 判斷索引是否存在

HEAD twitter

HTTP status code 表示結果 404 不存在,200 存在

8. 修改索引的 settings 信息

索引的設置信息分為靜態信息和動態信息兩部分。靜態信息不可更改,如索引的分片數。動態信息可以修改。

REST 訪問端點:

/_settings 更新所有索引的。

{index}/_settings 更新一個或多個索引的 settings。

詳細的設置項請參考:https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#index-modules-settings

9. 修改備份數

PUT /twitter/_settings {  index  : {  number_of_replicas  : 2 } }

10. 設置回默認值,用 null

PUT /twitter/_settings {  index  : {  refresh_interval  : null } }

11. 設置索引的讀寫

index.blocks.read_only:設為 true, 則索引以及索引的元數據只可讀  index.blocks.read_only_allow_delete:設為 true,只讀時允許刪除。 index.blocks.read:設為 true,則不可讀。 index.blocks.write:設為 true,則不可寫。 index.blocks.metadata:設為 true,則索引元數據不可讀寫。

12. 索引模板

在創建索引時,為每個索引寫定義信息可能是一件繁瑣的事情,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

13. Open/Close  Index    打開 / 關閉索引

POST /my_index/_close POST /my_index/_open

說明:

關閉的索引不能進行讀寫操作,幾乎不占集群開銷。

關閉的索引可以打開,打開走的是正常的恢復流程。

14. Shrink Index 收縮索引

索引的分片數是不可更改的,如要減少分片數可以通過收縮方式收縮為一個新的索引。新索引的分片數必須是原分片數的因子值,如原分片數是 8,則新索引的分片數可以為 4、2、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

15. Split Index 拆分索引

當索引的分片容量過大時,可以通過拆分操作將索引拆分為一個倍數分片數的新索引。能拆分為幾倍由創建索引時指定的 index.number_of_routing_shards 路由分片數決定。這個路由分片數決定了根據一致性 hash 路由文檔到分片的散列空間。

如 index.number_of_routing_shards = 30,指定的分片數是 5,則可按如下倍數方式進行拆分:

5  rarr; 10  rarr; 30 (split by 2, then by 3) 5  rarr; 15  rarr; 30 (split by 3, then by 2) 5  rarr; 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

16. Rollover Index 別名滾動指向新創建的索引

對于有時效性的索引數據,如日志,過一定時間后,老的索引數據就沒有用了。我們可以像數據庫中根據時間創建表來存放不同時段的數據一樣,在 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 新建索引的命名規則:

如果索引的名稱是 - 數字結尾,如 logs-000001,則新建索引的名稱也會是這個模式,數值增 1。

如果索引的名稱不是 - 數值結尾,則在請求 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 是你請求它才會進行操作,并不是自動在后臺進行的。你可以周期性地去請求它。

17. 索引監控

17.1 查看索引狀態信息

查看所有的索引狀態:

GET /_stats

查看指定索引的狀態信息:

GET /index1,index2/_stats

17.2 查看索引段信息

GET /test/_segments GET /index1,index2/_segments GET /_segments

17.3 查看索引恢復信息

GET index1,index2/_recovery?human

GET /_recovery?human

17.4 查看索引分片的存儲信息

# 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. 索引狀態管理

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

可選參數說明:

max_num_segments 合并為幾個段,默認 1

only_expunge_deletes 是否只合并含有刪除文檔的段,默認 false

flush 合并后是否刷新,默認 true

POST /kimchy,elasticsearch/_forcemerge POST /_forcemerge

三、映射詳解

1. Mapping 映射是什么

映射定義索引中有什么字段、字段的類型等結構信息。相當于數據庫中表結構定義,或 solr 中的 schema。因為 lucene 索引文檔時需要知道該如何來索引存儲文檔的字段。

ES 中支持手動定義映射,動態映射兩種方式。

1.1. 為索引創建 mapping

 PUT test {  !-- 映射定義  --   mappings  : {  !-- 名為 type1 的映射類別  mapping type--   type1  : {  !--  字段定義  --   properties  : {  !--  名為 field1 的字段,它的 field datatype  為  text --   field1  : {  type  :  text  } } } } }

說明:映射定義后續可以修改

2. 映射類別 Mapping type 廢除說明

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 來做這個事

3. 字段類型 datatypes

字段類型定義了該如何索引存儲字段值。ES 中提供了豐富的字段類型定義,請查看官網鏈接詳細了解每種類型的特點:

 https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html

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

 4. 字段定義屬性介紹

字段的 type (Datatype) 定義了如何索引存儲字段值,還有一些屬性可以讓我們根據需要來覆蓋默認的值或進行特別定義。

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

字段定義屬性 mdash; 示例

PUT my_index {  mappings : {  _doc : {  properties : {  date : {  type :  date ,  !-- 格式化日期  --   format :  yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis  } } } } }

5. Multi Field 多重字段

當我們需要對一個字段進行多種不同方式的索引時,可以使用 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  } } } }

6. 元字段

官網鏈接:

元字段是 ES 中定義的文檔字段,有以下幾類:

7. 動態映射

動態映射:ES 中提供的重要特性,讓我們可以快速使用 ES,而不需要先創建索引、定義映射。如我們直接向 ES 提交文檔進行索引:

PUT data/_doc/1 {  count : 5 }

ES 將自動為我們創建 data 索引、_doc 映射、類型為 long 的字段 count

索引文檔時,當有新字段時,ES 將根據我們字段的 json 的數據類型為我們自動加人字段定義到 mapping 中。

7.1 字段動態映射規則

7.2 Date detection 時間偵測

所謂時間偵測是指我們往 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 } } }

7.3 Numeric detection  數值偵測

開啟數值偵測(默認是禁用的)

PUT my_index {  mappings : {  _doc : {  numeric_detection : true } } } PUT my_index/_doc/1 {  my_float :  1.0 ,  my_integer :  1  }

看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注丸趣 TV 行業資訊頻道,感謝您對丸趣 TV 的支持。

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-08-25發表,共計11209字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 页游| 桓仁| 竹山县| 新郑市| 盐津县| 宜宾市| 墨竹工卡县| 顺平县| 平和县| 上虞市| 加查县| 北碚区| 洪江市| 肥西县| 长阳| 淮阳县| 松江区| 哈巴河县| 洛宁县| 鸡西市| 灵台县| 旌德县| 曲靖市| 大庆市| 齐齐哈尔市| 江永县| 习水县| 讷河市| 南康市| 萨迦县| 息烽县| 大邑县| 永登县| 东城区| 奇台县| 萨迦县| 定陶县| 伊川县| 塘沽区| 裕民县| 万年县|