自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ElasticSearch最全詳細(xì)使用教程

系統(tǒng) Linux
本文介紹了ElasticSearch的必備知識(shí):從入門、索引管理到映射詳解。

 [[340958]]

本文介紹了ElasticSearch的必備知識(shí):從入門、索引管理到映射詳解。

一、快速入門

1. 查看集群的健康狀況

http://localhost:9200/_cat

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

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

狀態(tài)值說明

Green - everything is good (cluster is fully functional),即最佳狀態(tài)

Yellow - all data is available but some replicas are not yet allocated (cluster is fully functional),即數(shù)據(jù)和集群可用,但是集群的備份有的是壞的

Red - some data is not available for whatever reason (cluster is partially functional),即數(shù)據(jù)和集群都不可用

查看集群的節(jié)點(diǎn)

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

2. 查看所有索引

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

3. 創(chuàng)建一個(gè)索引

創(chuàng)建一個(gè)名為 customer 的索引。pretty要求返回一個(gè)漂亮的json 結(jié)果

PUT /customer?pretty

再查看一下所有索引

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

GET /_cat/indices?v 

4. 索引一個(gè)文檔到customer索引中 

  1. curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'  
  2.  
  3.   "name": "John Doe"  
  4.  

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

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

6. 查詢所有文檔 

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

 JSON格式方式 

  1. GET /customer/_search  
  2.  
  3.   "query": { "match_all": {} },  
  4.   "sort": [  
  5.     {"name": "asc" }  
  6.   ]  

二、索引管理

  

 

 1. 創(chuàng)建索引

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

  1. PUT twitter  
  2.  
  3.     "settings" : {  
  4.         "index" : {  
  5.             "number_of_shards" : 3,  
  6.             "number_of_replicas" : 2  
  7.         }  
  8.     }  

說明:

默認(rèn)的分片數(shù)是5到1024

默認(rèn)的備份數(shù)是1

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

創(chuàng)建結(jié)果:

 創(chuàng)建的命令還可以簡寫為 

  1. PUT twitter  
  2.  
  3.     "settings" : {  
  4.         "number_of_shards" : 3,  
  5.         "number_of_replicas" : 2  
  6.     }  

 2. 創(chuàng)建mapping映射

注意:在ES中創(chuàng)建一個(gè)mapping映射類似于在數(shù)據(jù)庫中定義表結(jié)構(gòu),即表里面有哪些字段、字段是什么類型、字段的默認(rèn)值等;也類似于solr里面的模式schema的定義 

  1. PUT twitter  
  2.  
  3.     "settings" : {  
  4.         "index" : {  
  5.             "number_of_shards" : 3,  
  6.             "number_of_replicas" : 2  
  7.         }  
  8.     },  
  9.    "mappings" : {  
  10.         "type1" : {  
  11.             "properties" : {  
  12.                 "field1" : { "type" : "text" }  
  13.             }  
  14.         }  
  15.     }  

 3. 創(chuàng)建索引時(shí)加入別名定義 

  1. PUT twitter  
  2.  
  3.     "aliases" : {  
  4.         "alias_1" : {},  
  5.         "alias_2" : { 
  6.              "filter" : {  
  7.                 "term" : {"user" : "kimchy" }  
  8.             },  
  9.             "routing" : "kimchy"  
  10.         }  
  11.     }  

4. 創(chuàng)建索引時(shí)返回的結(jié)果說明

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

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

 

GET /twitter/_settings

GET /twitter/_mapping

 

 6. 刪除索引

DELETE /twitter

說明:

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

7. 判斷索引是否存在

HEAD twitter

 HTTP status code 表示結(jié)果 404 不存在 , 200 存在

8. 修改索引的settings信息

索引的設(shè)置信息分為靜態(tài)信息和動(dòng)態(tài)信息兩部分。靜態(tài)信息不可更改,如索引的分片數(shù)。動(dòng)態(tài)信息可以修改。

REST 訪問端點(diǎn):

/_settings 更新所有索引的。

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

詳細(xì)的設(shè)置項(xiàng)請(qǐng)參考:https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#index-modules-settings

9. 修改備份數(shù) 

  1. PUT /twitter/_settings  
  2.  
  3.     "index" : {  
  4.         "number_of_replicas" : 2  
  5.     }  

10. 設(shè)置回默認(rèn)值,用null 

  1. PUT /twitter/_settings  
  2.  
  3.     "index" : {  
  4.         "refresh_interval" : null  
  5.     }  

11. 設(shè)置索引的讀寫 

  1. index.blocks.read_only:設(shè)為true,則索引以及索引的元數(shù)據(jù)只可讀  
  2. index.blocks.read_only_allow_delete:設(shè)為true,只讀時(shí)允許刪除。  
  3. index.blocks.read:設(shè)為true,則不可讀。 
  4. index.blocks.write:設(shè)為true,則不可寫。  
  5. index.blocks.metadata:設(shè)為true,則索引元數(shù)據(jù)不可讀寫。 

12. 索引模板

在創(chuàng)建索引時(shí),為每個(gè)索引寫定義信息可能是一件繁瑣的事情,ES提供了索引模板功能,讓你可以定義一個(gè)索引模板,模板中定義好settings、mapping、以及一個(gè)模式定義來匹配創(chuàng)建的索引。

注意:模板只在索引創(chuàng)建時(shí)被參考,修改模板不會(huì)影響已創(chuàng)建的索引

12.1 新增/修改名為tempae_1的模板,匹配名稱為te* 或 bar*的索引創(chuàng)建: 

  1. PUT _template/template_1  
  2.  
  3.   "index_patterns": ["te*", "bar*"],  
  4.   "settings": { 
  5.      "number_of_shards": 1  
  6.   },  
  7.   "mappings": {  
  8.     "type1": {  
  9.       "_source": {  
  10.         "enabled": false  
  11.       },  
  12.       "properties": {  
  13.         "host_name": {  
  14.           "type": "keyword"  
  15.         },  
  16.         "created_at": {  
  17.           "type": "date",  
  18.           "format": "EEE MMM dd HH:mm:ss Z YYYY"  
  19.         }  
  20.       }  
  21.     }  
  22.   }  

12.2 查看索引模板 

  1. GET /_template/template_1  
  2. GET /_template/temp*   
  3. GET /_template/template_1,template_2  
  4. GET /_template 

12.3 刪除模板 

  1. DELETE /_template/template_1 

13. Open/Close  Index   打開/關(guān)閉索引 

  1. POST /my_index/_close  
  2. POST /my_index/_open 

說明:

關(guān)閉的索引不能進(jìn)行讀寫操作,幾乎不占集群開銷。

關(guān)閉的索引可以打開,打開走的是正常的恢復(fù)流程。

14. Shrink Index 收縮索引

索引的分片數(shù)是不可更改的,如要減少分片數(shù)可以通過收縮方式收縮為一個(gè)新的索引。新索引的分片數(shù)必須是原分片數(shù)的因子值,如原分片數(shù)是8,則新索引的分片數(shù)可以為4、2、1 。

什么時(shí)候需要收縮索引呢?

最初創(chuàng)建索引的時(shí)候分片數(shù)設(shè)置得太大,后面發(fā)現(xiàn)用不了那么多分片,這個(gè)時(shí)候就需要收縮了

收縮的流程:

先把所有主分片都轉(zhuǎn)移到一臺(tái)主機(jī)上;

在這臺(tái)主機(jī)上創(chuàng)建一個(gè)新索引,分片數(shù)較小,其他設(shè)置和原索引一致;

把原索引的所有分片,復(fù)制(或硬鏈接)到新索引的目錄下;

對(duì)新索引進(jìn)行打開操作恢復(fù)分片數(shù)據(jù);

(可選)重新把新索引的分片均衡到其他節(jié)點(diǎn)上。

收縮前的準(zhǔn)備工作:

將原索引設(shè)置為只讀;

將原索引各分片的一個(gè)副本重分配到同一個(gè)節(jié)點(diǎn)上,并且要是健康綠色狀態(tài)。 

  1. PUT /my_source_index/_settings  
  2.  
  3.   "settings": {  
  4.     <!-- 指定進(jìn)行收縮的節(jié)點(diǎn)的名稱 -->  
  5.     "index.routing.allocation.require._name": "shrink_node_name",  
  6.     <!-- 阻止寫,只讀 -->  
  7.      "index.blocks.write": true  
  8.   }  

進(jìn)行收縮: 

  1. POST my_source_index/_shrink/my_target_index  
  2.  
  3.   "settings": {  
  4.     "index.number_of_replicas": 1,  
  5.     "index.number_of_shards": 1,  
  6.     "index.codec": "best_compression"  
  7.   }} 

監(jiān)控收縮過程: 

  1. GET _cat/recovery?v  
  2. GET _cluster/health 

15. Split Index 拆分索引

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

如index.number_of_routing_shards = 30 ,指定的分片數(shù)是5,則可按如下倍數(shù)方式進(jìn)行拆分: 

  1. 5 → 10 → 30 (split by 2, then by 3)  
  2. 5 → 15 → 30 (split by 3, then by 2)  
  3. 5 → 30 (split by 6) 

為什么需要拆分索引?

當(dāng)最初設(shè)置的索引的分片數(shù)不夠用時(shí)就需要拆分索引了,和壓縮索引相反

注意:只有在創(chuàng)建時(shí)指定了index.number_of_routing_shards 的索引才可以進(jìn)行拆分,ES7開始將不再有這個(gè)限制。

和solr的區(qū)別是,solr是對(duì)一個(gè)分片進(jìn)行拆分,es中是整個(gè)索引進(jìn)行拆分。

拆分步驟:

準(zhǔn)備一個(gè)索引來做拆分: 

  1. PUT my_source_index  
  2.  
  3.     "settings": {  
  4.         "index.number_of_shards" : 1,  
  5.         <!-- 創(chuàng)建時(shí)需要指定路由分片數(shù) -->  
  6.         "index.number_of_routing_shards" : 2  
  7.     }  

先設(shè)置索引只讀: 

  1. PUT /my_source_index/_settings  
  2.  
  3.   "settings": {  
  4.     "index.blocks.write": true  
  5.   }  

做拆分: 

  1. POST my_source_index/_split/my_target_index  
  2.  
  3.   "settings": {  
  4.     <!--新索引的分片數(shù)需符合拆分規(guī)則-->  
  5.     "index.number_of_shards": 2  
  6.   }  

監(jiān)控拆分過程: 

  1. GET _cat/recovery?v  
  2. GET _cluster/health 

16. Rollover Index 別名滾動(dòng)指向新創(chuàng)建的索引

對(duì)于有時(shí)效性的索引數(shù)據(jù),如日志,過一定時(shí)間后,老的索引數(shù)據(jù)就沒有用了。我們可以像數(shù)據(jù)庫中根據(jù)時(shí)間創(chuàng)建表來存放不同時(shí)段的數(shù)據(jù)一樣,在ES中也可用建多個(gè)索引的方式來分開存放不同時(shí)段的數(shù)據(jù)。比數(shù)據(jù)庫中更方便的是ES中可以通過別名滾動(dòng)指向最新的索引的方式,讓你通過別名來操作時(shí)總是操作的最新的索引。

ES的rollover index API 讓我們可以根據(jù)滿足指定的條件(時(shí)間、文檔數(shù)量、索引大小)創(chuàng)建新的索引,并把別名滾動(dòng)指向新的索引。

注意:這時(shí)的別名只能是一個(gè)索引的別名。

Rollover Index 示例:

創(chuàng)建一個(gè)名字為logs-0000001 、別名為logs_write 的索引: 

  1. PUT /logs-000001  
  2.  
  3.   "aliases": {  
  4.     "logs_write": {}  
  5.   }  

添加1000個(gè)文檔到索引logs-000001,然后設(shè)置別名滾動(dòng)的條件 

  1. POST /logs_write/_rollover  
  2.  
  3.   "conditions": {  
  4.     "max_age":   "7d",  
  5.     "max_docs":  1000,  
  6.     "max_size":  "5gb"  
  7.   }  

說明:

如果別名logs_write指向的索引是7天前(含)創(chuàng)建的或索引的文檔數(shù)>=1000或索引的大小>= 5gb,則會(huì)創(chuàng)建一個(gè)新索引 logs-000002,并把別名logs_writer指向新創(chuàng)建的logs-000002索引

Rollover Index 新建索引的命名規(guī)則:

如果索引的名稱是-數(shù)字結(jié)尾,如logs-000001,則新建索引的名稱也會(huì)是這個(gè)模式,數(shù)值增1。

如果索引的名稱不是-數(shù)值結(jié)尾,則在請(qǐng)求rollover api時(shí)需指定新索引的名稱 

  1. POST /my_alias/_rollover/my_new_index_name  
  2.  
  3.   "conditions": {  
  4.     "max_age":   "7d",  
  5.     "max_docs":  1000,  
  6.     "max_size": "5gb"  
  7.   }  

在名稱中使用Date math(時(shí)間表達(dá)式)

如果你希望生成的索引名稱中帶有日期,如logstash-2016.02.03-1 ,則可以在創(chuàng)建索引時(shí)采用時(shí)間表達(dá)式來命名: 

  1. # PUT /<logs-{now/d}-1> with URI encoding:  
  2. PUT /%3Clogs-%7Bnow%2Fd%7D-1%3E  
  3.  
  4.   "aliases": { 
  5.      "logs_write": {}  
  6.   }  
  7.  
  8. PUT logs_write/_doc/1  
  9.  
  10.   "message": "a dummy log"  
  11. }   
  12. POST logs_write/_refresh  
  13. # Wait for a day to pass  
  14. POST /logs_write/_rollover  
  15.  
  16.   "conditions": { 
  17.      "max_docs":   "1"  
  18.   }  

Rollover時(shí)可對(duì)新的索引作定義: 

  1. PUT /logs-000001  
  2.  
  3.   "aliases": {  
  4.     "logs_write": {} 
  5.    }  
  6.  
  7. POST /logs_write/_rollover  
  8.  
  9.   "conditions" : {  
  10.     "max_age": "7d", 
  11.      "max_docs": 1000,  
  12.     "max_size": "5gb"  
  13.   },  
  14.   "settings": {  
  15.     "index.number_of_shards": 2  
  16.   }  

Dry run  實(shí)際操作前先測試是否達(dá)到條件: 

  1. POST /logs_write/_rollover?dry_run  
  2.  
  3.   "conditions" : {  
  4.     "max_age": "7d",  
  5.     "max_docs": 1000,  
  6.     "max_size": "5gb"  
  7.   }  

說明:

測試不會(huì)創(chuàng)建索引,只是檢測條件是否滿足

注意:rollover是你請(qǐng)求它才會(huì)進(jìn)行操作,并不是自動(dòng)在后臺(tái)進(jìn)行的。你可以周期性地去請(qǐng)求它。

17. 索引監(jiān)控

17.1 查看索引狀態(tài)信息

官網(wǎng)鏈接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-stats.html

查看所有的索引狀態(tài):

GET /_stats

查看指定索引的狀態(tài)信息:

GET /index1,index2/_stats

17.2 查看索引段信息

官網(wǎng)鏈接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-segments.html 

  1. GET /test/_segments   
  2. GET /index1,index2/_segments  
  3. GET /_segments 

17.3 查看索引恢復(fù)信息

官網(wǎng)鏈接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-recovery.html

GET index1,index2/_recovery?human

GET /_recovery?human

17.4 查看索引分片的存儲(chǔ)信息

官網(wǎng)鏈接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-shards-stores.html 

  1. # return information of only index test  
  2. GET /test/_shard_stores  
  3. # return information of only test1 and test2 indices  
  4. GET /test1,test2/_shard_stores  
  5. # return information of all indices  
  6. GET /_shard_stores  
  7.   GET /_shard_stores?status=green 

18. 索引狀態(tài)管理

18.1 Clear Cache 清理緩存

POST /twitter/_cache/clear

默認(rèn)會(huì)清理所有緩存,可指定清理query, fielddata or request 緩存 

  1. POST /kimchy,elasticsearch/_cache/clear  
  2. POST /_cache/clear 

18.2 Refresh,重新打開讀取索引 

  1. POST /kimchy,elasticsearch/_refresh  
  2. POST /_refresh 

18.3 Flush,將緩存在內(nèi)存中的索引數(shù)據(jù)刷新到持久存儲(chǔ)中

  1. POST twitter/_flush 

18.4 Force merge 強(qiáng)制段合并 

  1. POST /kimchy/_forcemerge?only_expunge_deletes=false&max_num_segments=100&flush=true 

可選參數(shù)說明:

max_num_segments 合并為幾個(gè)段,默認(rèn)1

only_expunge_deletes 是否只合并含有刪除文檔的段,默認(rèn)false

flush 合并后是否刷新,默認(rèn)true 

  1. POST /kimchy,elasticsearch/_forcemerge  
  2. POST /_forcemerge 

三、映射詳解

1. Mapping 映射是什么

映射定義索引中有什么字段、字段的類型等結(jié)構(gòu)信息。相當(dāng)于數(shù)據(jù)庫中表結(jié)構(gòu)定義,或 solr中的schema。因?yàn)閘ucene索引文檔時(shí)需要知道該如何來索引存儲(chǔ)文檔的字段。

ES中支持手動(dòng)定義映射,動(dòng)態(tài)映射兩種方式。

 1.1. 為索引創(chuàng)建mapping 

  1.  PUT test  
  2.  
  3. <!--映射定義 -->  
  4. "mappings" : {  
  5. <!--名為type1的映射類別 mapping type-->  
  6.         "type1" : {  
  7.         <!-- 字段定義 -->  
  8.             "properties" : {  
  9.             <!-- 名為field1的字段,它的field datatype 為 text -->  
  10.                 "field1" : { "type" : "text" } 
  11.              }  
  12.         }  
  13.     }  

 說明:映射定義后續(xù)可以修改

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

ES最先的設(shè)計(jì)是用索引類比關(guān)系型數(shù)據(jù)庫的數(shù)據(jù)庫,用mapping type 來類比表,一個(gè)索引中可以包含多個(gè)映射類別。這個(gè)類比存在一個(gè)嚴(yán)重的問題,就是當(dāng)多個(gè)mapping type中存在同名字段時(shí)(特別是同名字段還是不同類型的),在一個(gè)索引中不好處理,因?yàn)樗阉饕嬷兄挥?索引-文檔的結(jié)構(gòu),不同映射類別的數(shù)據(jù)都是一個(gè)一個(gè)的文檔(只是包含的字段不一樣而已)

從6.0.0開始限定僅包含一個(gè)映射類別定義( "index.mapping.single_type": true ),兼容5.x中的多映射類別。從7.0開始將移除映射類別。

為了與未來的規(guī)劃匹配,請(qǐng)現(xiàn)在將這個(gè)唯一的映射類別名定義為“_doc”,因?yàn)樗饕恼?qǐng)求地址將規(guī)范為:PUT {index}/_doc/{id} and POST {index}/_doc

Mapping 映射示例: 

  1. PUT twitter  
  2.  
  3.   "mappings": {  
  4.     "_doc": {  
  5.       "properties": {  
  6.         "type": { "type": "keyword" },  
  7.         "name": { "type": "text" },  
  8.         "user_name": { "type": "keyword" },  
  9.         "email": { "type": "keyword" },  
  10.         "content": { "type": "text" },  
  11.         "tweeted_at": { "type": "date" }  
  12.       }  
  13.     }  
  14.   }  

多映射類別數(shù)據(jù)轉(zhuǎn)儲(chǔ)到獨(dú)立的索引中:

ES 提供了reindex API 來做這個(gè)事

3. 字段類型 datatypes

字段類型定義了該如何索引存儲(chǔ)字段值。ES中提供了豐富的字段類型定義,請(qǐng)查看官網(wǎng)鏈接詳細(xì)了解每種類型的特點(diǎn):

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

 3.1 Core Datatypes     核心類型 

  1. string  
  2.     text and keyword  
  3. Numeric datatypes  
  4.     long, integer, short, byte, double, float, half_float, scaled_float  
  5. Date datatype  
  6.     date  
  7. Boolean datatype  
  8.     boolean  
  9. Binary datatype  
  10.     binary  
  11. Range datatypes     范圍  
  12.     integer_range, float_range, long_range, double_range, date_range 

3.2 Complex datatypes 復(fù)合類型 

  1. Array datatype  
  2.     數(shù)組就是多值,不需要專門的類型  
  3. Object datatype  
  4.     object :表示值為一個(gè)JSON 對(duì)象  
  5. Nested datatype  
  6.     nested:for arrays of JSON objects(表示值為JSON對(duì)象數(shù)組 ) 

3.3 Geo datatypes  地理數(shù)據(jù)類型 

  1. Geo-point datatype  
  2.     geo_point:for lat/lon points  (經(jīng)緯坐標(biāo)點(diǎn))  
  3. Geo-Shape datatype  
  4.     geo_shape:for complex shapes like polygons (形狀表示) 

 3.4 Specialised datatypes 特別的類型 

  1. IP datatype  
  2.     ip:for IPv4 and IPv6 addresses  
  3. Completion datatype  
  4.     completion:to provide auto-complete suggestions  
  5. Token count datatype  
  6.     token_count:to count the number of tokens in a string  
  7. mapper-murmur3 
  8.      murmur3:to compute hashes of values at index-time and store them in the index  
  9. Percolator type  
  10.     Accepts queries from the query-dsl  
  11. join datatype  
  12.     Defines parent/child relation for documents within the same index 

 4. 字段定義屬性介紹

字段的type (Datatype)定義了如何索引存儲(chǔ)字段值,還有一些屬性可以讓我們根據(jù)需要來覆蓋默認(rèn)的值或進(jìn)行特別定義。請(qǐng)參考官網(wǎng)介紹詳細(xì)了解:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html 

  1. analyzer   指定分詞器  
  2.   normalizer   指定標(biāo)準(zhǔn)化器  
  3.   boost        指定權(quán)重值  
  4.   coerce      強(qiáng)制類型轉(zhuǎn)換  
  5.   copy_to    值復(fù)制給另一字段  
  6.   doc_values  是否存儲(chǔ)docValues  
  7.   dynamic  
  8.   enabled    字段是否可用  
  9.   fielddata  
  10.   eager_global_ordinals  
  11.   format    指定時(shí)間值的格式 
  12.    ignore_above  
  13.   ignore_malformed  
  14.   index_options  
  15.   index  
  16.   fields  
  17.   norms  
  18.   null_value  
  19.   position_increment_gap  
  20.   properties  
  21.   search_analyzer  
  22.   similarity  
  23.   store  
  24.   term_vector 

字段定義屬性—示例 

  1. PUT my_index  
  2.  
  3.   "mappings": {  
  4.     "_doc": {  
  5.       "properties": {  
  6.         "date": {  
  7.           "type":   "date",  
  8.            <!--格式化日期 -->  
  9.           "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"  
  10.         }  
  11.       }  
  12.     }  
  13.   } 

5. Multi Field 多重字段

當(dāng)我們需要對(duì)一個(gè)字段進(jìn)行多種不同方式的索引時(shí),可以使用fields多重字段定義。如一個(gè)字符串字段即需要進(jìn)行text分詞索引,也需要進(jìn)行keyword 關(guān)鍵字索引來支持排序、聚合;或需要用不同的分詞器進(jìn)行分詞索引。

示例:

定義多重字段:

說明:raw是一個(gè)多重版本名(自定義) 

  1. PUT my_index  
  2.  
  3.   "mappings": { 
  4.     "_doc": { 
  5.        "properties": {  
  6.         "city": {  
  7.           "type": "text",  
  8.           "fields": {  
  9.             "raw": {  
  10.               "type":  "keyword"  
  11.             }  
  12.           } 
  13.         }  
  14.       }  
  15.     } 
  16.   }  

往多重字段里面添加文檔 

  1. PUT my_index/_doc/1  
  2.  
  3.   "city": "New York"  
  4.  
  5. PUT my_index/_doc/2  
  6.  
  7.   "city": "York"  

獲取多重字段的值: 

  1. GET my_index/_search  
  2.  
  3.   "query": {  
  4.     "match": {  
  5.       "city": "york"  
  6.     }  
  7.   },  
  8.   "sort": {  
  9.     "city.raw": "asc"  
  10.   },  
  11.   "aggs": {  
  12.     "Cities": {  
  13.       "terms": {  
  14.         "field": "city.raw"  
  15.       }  
  16.     }  
  17.   }  

6. 元字段

官網(wǎng)鏈接:

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

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

7. 動(dòng)態(tài)映射

動(dòng)態(tài)映射:ES中提供的重要特性,讓我們可以快速使用ES,而不需要先創(chuàng)建索引、定義映射。如我們直接向ES提交文檔進(jìn)行索引: 

  1. PUT data/_doc/1  
  2. { "count": 5 } 

ES將自動(dòng)為我們創(chuàng)建data索引、_doc 映射、類型為 long 的字段 count

索引文檔時(shí),當(dāng)有新字段時(shí), ES將根據(jù)我們字段的json的數(shù)據(jù)類型為我們自動(dòng)加人字段定義到mapping中。

7.1 字段動(dòng)態(tài)映射規(guī)則

7.2 Date detection 時(shí)間偵測

所謂時(shí)間偵測是指我們往ES里面插入數(shù)據(jù)的時(shí)候會(huì)去自動(dòng)檢測我們的數(shù)據(jù)是不是日期格式的,是的話就會(huì)給我們自動(dòng)轉(zhuǎn)為設(shè)置的格式

 date_detection 默認(rèn)是開啟的,默認(rèn)的格式dynamic_date_formats為: 

  1. [ "strict_date_optional_time","yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"]  
  2. PUT my_index/_doc/1  
  3.  
  4.   "create_date": "2015/09/02"  
  5. GET my_index/_mapping 

 自定義時(shí)間格式: 

  1. PUT my_index  
  2.  
  3.   "mappings": {  
  4.     "_doc": {  
  5.       "dynamic_date_formats": ["MM/dd/yyyy"]  
  6.     }  
  7.   }  

 禁用時(shí)間偵測: 

  1. PUT my_index  
  2.  
  3.   "mappings": {  
  4.     "_doc": {  
  5.       "date_detection": false  
  6.     }  
  7.   }  

 7.3 Numeric detection  數(shù)值偵測

 開啟數(shù)值偵測(默認(rèn)是禁用的) 

  1. PUT my_index  
  2.  
  3.   "mappings": {  
  4.     "_doc": {  
  5.       "numeric_detection": true  
  6.     }  
  7.   }  
  8.  
  9. PUT my_index/_doc/1  
  10.  
  11.   "my_float":   "1.0",  
  12.   "my_integer": "1"  
  13.  

 

責(zé)任編輯:龐桂玉 來源: 馬哥Linux運(yùn)維
相關(guān)推薦

2011-07-06 10:32:07

Xcode

2011-07-06 10:46:33

Xcode

2023-09-14 08:30:46

JsonPathXPath

2018-08-30 09:36:10

編程語言Python機(jī)器學(xué)習(xí)

2023-10-30 07:05:31

2025-02-28 08:24:26

2019-05-21 09:40:47

Elasticsear高性能 API

2023-08-31 08:56:24

2023-08-30 08:51:41

NginxLinux

2021-12-02 08:37:45

Linux MySQL Linux 系統(tǒng)

2021-03-18 15:10:42

ElasticSearBeta日志

2023-02-02 09:47:39

estext類型

2018-09-17 14:50:41

機(jī)器學(xué)習(xí)教程匯總人工智能

2019-11-19 15:43:07

人工智能軟件技術(shù)

2023-02-23 09:36:34

DockerELK堆棧

2012-04-27 10:55:45

JavaExcelAPI

2021-06-09 09:36:18

DjangoElasticSearLinux

2021-05-11 09:02:34

OpenSearch存儲(chǔ)Elastcsearc

2024-08-01 10:10:24

MySQL場景搜索

2024-02-29 08:02:00

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)