一、快速入門(mén)
1. 查看集群的健康狀況
http://localhost:9200/_cat
http://localhost:9200/_cat/health?v
說(shuō)明:v是用來(lái)要求在結(jié)果中返回表頭
狀態(tài)值說(shuō)明
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/nodes?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索引中
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. 創(chuàng)建索引
創(chuàng)建一個(gè)名為twitter的索引,設(shè)置索引的分片數(shù)為3,備份數(shù)為2。注意:在ES中創(chuàng)建一個(gè)索引類似于在數(shù)據(jù)庫(kù)中建立一個(gè)數(shù)據(jù)庫(kù)(ES6.0之后類似于創(chuàng)建一個(gè)表)
PUT twitter
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
}
說(shuō)明:
默認(rèn)的分片數(shù)是5到1024
默認(rèn)的備份數(shù)是1
索引的名稱必須是小寫(xiě)的,不可重名
創(chuàng)建結(jié)果:
PUT twitter
{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
2. 創(chuàng)建mapping映射
注意:在ES中創(chuàng)建一個(gè)mapping映射類似于在數(shù)據(jù)庫(kù)中定義表結(jié)構(gòu),即表里面有哪些字段、字段是什么類型、字段的默認(rèn)值等;也類似于solr里面的模式schema的定義
PUT twitter
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
},
"mappings" : {
"type1" : {
"properties" : {
"field1" : { "type" : "text" }
}
}
}
}
3. 創(chuàng)建索引時(shí)加入別名定義
索引別名
一個(gè)索引可以接受多個(gè)別名,而一個(gè)別名也可以映射到多個(gè)索引,當(dāng)指定別名時(shí),別名將自動(dòng)擴(kuò)展到添加的索引。
別名也可以關(guān)聯(lián)到 filter,然后自動(dòng)應(yīng)用到檢索,和 routing value。別名不能與索引同名。
創(chuàng)建一個(gè)帶過(guò)濾器的別名,首先需要確保所有的字段都存在于mapping中。
PUT twitter
{
"aliases" : {
"alias_1" : {},
"alias_2" : {
"filter" : {
"term" : {"user" : "kimchy" }
},
"routing" : "kimchy"
}
}
}
添加別名,移除別名示例:
POST /_aliases
{
"actions" : [
{ "add" : { "index" : "test1", "alias" : "alias1" } },
{ "remove" : { "index" : "test1", "alias" : "alias2" } }
]
}
在同一個(gè) API 接口中可以先移除然后添加操作。該操作是原子操作,無(wú)需擔(dān)心別名不指向任何一個(gè)索引的短暫瞬間:
POST /_aliases
{
"actions" : [
{ "remove" : { "index" : "test1", "alias" : "alias1" } },
{ "add" : { "index" : "test2", "alias" : "alias1" } }
]
}
Routing
可以將路由值與別名相關(guān)聯(lián)。此功能可以與過(guò)濾器別名一起使用,以避免不必要的分片操作。
POST /_aliases
{
"actions" : [
{
"add" : {
"index" : "test",
"alias" : "alias1",
"routing" : "1"
}
}
]
}
創(chuàng)建索引的時(shí)候指定
PUT /logs_test_index
{
"mappings" : {
"_doc" : {
"properties" : {
"year" : {"type" : "integer"}
}
}
},
"aliases" : {
"current_day" : {},
"2016" : {
"filter" : {
"term" : {"year" : 2016 }
}
}
}
}
同時(shí)使用路由和過(guò)濾器
POST /_aliases
{
"actions" : [
{
"add" : {
"index" : "test",
"alias" : "test_alias",
"search_routing" : "2,3",
"index_routing" : "1",
"filter": {"term": {"user": "kitty"}}
}
}
]
}
4. 創(chuàng)建索引時(shí)返回的結(jié)果說(shuō)明
5. Get Index 查看索引的定義信息
GET /twitter,可以一次獲取多個(gè)索引(以逗號(hào)間隔) 獲取所有索引 _all 或 用通配符*
GET /twitter/_settings
GET /twitter/_mapping
6. 刪除索引
DELETE /twitter
說(shuō)明:
可以一次刪除多個(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 訪問(wèn)端點(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ù)
PUT /twitter/_settings
{
"index" : {
"number_of_replicas" : 2
}
}
10. 設(shè)置回默認(rèn)值,用null
PUT /twitter/_settings
{
"index" : {
"refresh_interval" : null
}
}
11. 設(shè)置索引的讀寫(xiě)
index.blocks.read_only:設(shè)為true,則索引以及索引的元數(shù)據(jù)只可讀
index.blocks.read_only_allow_delete:設(shè)為true,只讀時(shí)允許刪除。
index.blocks.read:設(shè)為true,則不可讀。
index.blocks.write:設(shè)為true,則不可寫(xiě)。
index.blocks.metadata:設(shè)為true,則索引元數(shù)據(jù)不可讀寫(xiě)。
12. 索引模板
在創(chuàng)建索引時(shí),為每個(gè)索引寫(xiě)定義信息可能是一件繁瑣的事情,ES提供了索引模板功能,讓你可以定義一個(gè)索引模板,模板中定義好settings、mapping、以及一個(gè)模式定義來(lái)匹配創(chuàng)建的索引。
注意:模板只在索引創(chuàng)建時(shí)被參考,修改模板不會(huì)影響已創(chuàng)建的索引
12.1 新增/修改名為tempae_1的模板,匹配名稱為te* 或 bar*的索引創(chuàng)建:
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 打開(kāi)/關(guān)閉索引
POST /my_index/_close
POST /my_index/_open
說(shuō)明:
關(guān)閉的索引不能進(jìn)行讀寫(xiě)操作,幾乎不占集群開(kāi)銷。
關(guān)閉的索引可以打開(kāi),打開(kāi)走的是正常的恢復(fù)流程。
14. Shrink Index 收縮索引
索引的分片數(shù)是不可更改的,如要減少分片數(shù)可以通過(guò)收縮方式收縮為一個(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)行打開(kāi)操作恢復(fù)分片數(shù)據(jù);
(可選)重新把新索引的分片均衡到其他節(jié)點(diǎn)上。
收縮前的準(zhǔn)備工作:
將原索引設(shè)置為只讀;
將原索引各分片的一個(gè)副本重分配到同一個(gè)節(jié)點(diǎn)上,并且要是健康綠色狀態(tài)。
PUT /my_source_index/_settings
{
"settings": {
<!-- 指定進(jìn)行收縮的節(jié)點(diǎn)的名稱 -->
"index.routing.allocation.require._name": "shrink_node_name",
<!-- 阻止寫(xiě),只讀 -->
"index.blocks.write": true
}
}
進(jìn)行收縮:
POST my_source_index/_shrink/my_target_index
{
"settings": {
"index.number_of_replicas": 1,
"index.number_of_shards": 1,
"index.codec": "best_compression"
}}
監(jiān)控收縮過(guò)程:
GET _cat/recovery?v
GET _cluster/health
15. Split Index 拆分索引
當(dāng)索引的分片容量過(guò)大時(shí),可以通過(guò)拆分操作將索引拆分為一個(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)行拆分:
5 → 10 → 30 (split by 2, then by 3)
5 → 15 → 30 (split by 3, then by 2)
5 → 30 (split by 6)
為什么需要拆分索引?
當(dāng)最初設(shè)置的索引的分片數(shù)不夠用時(shí)就需要拆分索引了,和壓縮索引相反
注意:只有在創(chuàng)建時(shí)指定了index.number_of_routing_shards 的索引才可以進(jìn)行拆分,ES7開(kāi)始將不再有這個(gè)限制。
和solr的區(qū)別是,solr是對(duì)一個(gè)分片進(jìn)行拆分,es中是整個(gè)索引進(jìn)行拆分。
拆分步驟:
準(zhǔn)備一個(gè)索引來(lái)做拆分:
PUT my_source_index
{
"settings": {
"index.number_of_shards" : 1,
<!-- 創(chuàng)建時(shí)需要指定路由分片數(shù) -->
"index.number_of_routing_shards" : 2
}
}
先設(shè)置索引只讀:
PUT /my_source_index/_settings
{
"settings": {
"index.blocks.write": true
}
}
做拆分:
POST my_source_index/_split/my_target_index
{
"settings": {
<!--新索引的分片數(shù)需符合拆分規(guī)則-->
"index.number_of_shards": 2
}
}
監(jiān)控拆分過(guò)程:
GET _cat/recovery?v
GET _cluster/health
16. Rollover Index 別名滾動(dòng)指向新創(chuàng)建的索引
對(duì)于有時(shí)效性的索引數(shù)據(jù),如日志,過(guò)一定時(shí)間后,老的索引數(shù)據(jù)就沒(méi)有用了。我們可以像數(shù)據(jù)庫(kù)中根據(jù)時(shí)間創(chuàng)建表來(lái)存放不同時(shí)段的數(shù)據(jù)一樣,在ES中也可用建多個(gè)索引的方式來(lái)分開(kāi)存放不同時(shí)段的數(shù)據(jù)。比數(shù)據(jù)庫(kù)中更方便的是ES中可以通過(guò)別名滾動(dòng)指向最新的索引的方式,讓你通過(guò)別名來(lái)操作時(shí)總是操作的最新的索引。
ES的rollover index API 讓我們可以根據(jù)滿足指定的條件(時(shí)間、文檔數(shù)量、索引大?。﹦?chuàng)建新的索引,并把別名滾動(dòng)指向新的索引。
注意:這時(shí)的別名只能是一個(gè)索引的別名。
Rollover Index 示例:
創(chuàng)建一個(gè)名字為logs-0000001 、別名為logs_write 的索引:
PUT /logs-000001
{
"aliases": {
"logs_write": {}
}
}
添加1000個(gè)文檔到索引logs-000001,然后設(shè)置別名滾動(dòng)的條件
POST /logs_write/_rollover
{
"conditions": {
"max_age": "7d",
"max_docs": 1000,
"max_size": "5gb"
}
}
說(shuō)明:
如果別名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í)需指定新索引的名稱
POST /my_alias/_rollover/my_new_index_name
{
"conditions": {
"max_age": "7d",
"max_docs": 1000,
"max_size": "5gb"
}
}
在名稱中使用Date math(時(shí)間表達(dá)式)
如果你希望生成的索引名稱中帶有日期,如logstash-2016.02.03-1 ,則可以在創(chuàng)建索引時(shí)采用時(shí)間表達(dá)式來(lái)命名:
# 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時(shí)可對(duì)新的索引作定義:
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 實(shí)際操作前先測(cè)試是否達(dá)到條件:
POST /logs_write/_rollover?dry_run
{
"conditions" : {
"max_age": "7d",
"max_docs": 1000,
"max_size": "5gb"
}
}
說(shuō)明:
測(cè)試不會(huì)創(chuàng)建索引,只是檢測(cè)條件是否滿足
注意: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
GET /test/_segments
GET /index1,index2/_segments
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
# 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. 索引狀態(tài)管理
18.1 Clear Cache 清理緩存
POST /twitter/_cache/clear
默認(rèn)會(huì)清理所有緩存,可指定清理query, fielddata or request 緩存
POST /kimchy,elasticsearch/_cache/clear
POST /_cache/clear
18.2 Refresh,重新打開(kāi)讀取索引
POST /kimchy,elasticsearch/_refresh
POST /_refresh
18.3 Flush,將緩存在內(nèi)存中的索引數(shù)據(jù)刷新到持久存儲(chǔ)中
POST twitter/_flush
18.4 Force merge 強(qiáng)制段合并
POST /kimchy/_forcemerge?only_expunge_deletes=false&max_num_segments=100&flush=true
可選參數(shù)說(shuō)明:
max_num_segments 合并為幾個(gè)段,默認(rèn)1
only_expunge_deletes 是否只合并含有刪除文檔的段,默認(rèn)false
flush 合并后是否刷新,默認(rèn)true
POST /kimchy,elasticsearch/_forcemerge
POST /_forcemerge
三、映射詳解
1. Mapping 映射是什么
映射定義索引中有什么字段、字段的類型等結(jié)構(gòu)信息。相當(dāng)于數(shù)據(jù)庫(kù)中表結(jié)構(gòu)定義,或 solr中的schema。因?yàn)閘ucene索引文檔時(shí)需要知道該如何來(lái)索引存儲(chǔ)文檔的字段。
ES中支持手動(dòng)定義映射,動(dòng)態(tài)映射兩種方式。
1.1. 為索引創(chuàng)建mapping
PUT test
{
<!--映射定義 -->
"mappings" : {
<!--名為type1的映射類別 mapping type-->
"type1" : {
<!-- 字段定義 -->
"properties" : {
<!-- 名為field1的字段,它的field datatype 為 text -->
"field1" : { "type" : "text" }
}
}
}
}
說(shuō)明:映射定義后續(xù)可以修改
2. 映射類別 Mapping type 廢除說(shuō)明
ES最先的設(shè)計(jì)是用索引類比關(guān)系型數(shù)據(jù)庫(kù)的數(shù)據(jù)庫(kù),用mapping type 來(lái)類比表,一個(gè)索引中可以包含多個(gè)映射類別。這個(gè)類比存在一個(gè)嚴(yán)重的問(wèn)題,就是當(dāng)多個(gè)mapping type中存在同名字段時(shí)(特別是同名字段還是不同類型的),在一個(gè)索引中不好處理,因?yàn)樗阉饕嬷兄挥?索引-文檔的結(jié)構(gòu),不同映射類別的數(shù)據(jù)都是一個(gè)一個(gè)的文檔(只是包含的字段不一樣而已)
從6.0.0開(kāi)始限定僅包含一個(gè)映射類別定義( "index.mapping.single_type": true ),兼容5.x中的多映射類別。從7.0開(kāi)始將移除映射類別。
為了與未來(lái)的規(guī)劃匹配,請(qǐng)現(xiàn)在將這個(gè)唯一的映射類別名定義為“_doc”,因?yàn)樗饕恼?qǐng)求地址將規(guī)范為: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" }
}
}
}
}
多映射類別數(shù)據(jù)轉(zhuǎn)儲(chǔ)到獨(dú)立的索引中:
ES 提供了reindex API 來(lái)做這個(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 核心類型
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 復(fù)合類型
Array datatype
數(shù)組就是多值,不需要專門(mén)的類型
Object datatype
object :表示值為一個(gè)JSON 對(duì)象
Nested datatype
nested:for arrays of JSON objects(表示值為JSON對(duì)象數(shù)組 )
3.3 Geo datatypes 地理數(shù)據(jù)類型
Geo-point datatype
geo_point: for lat/lon points (經(jīng)緯坐標(biāo)點(diǎn))
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)定義了如何索引存儲(chǔ)字段值,還有一些屬性可以讓我們根據(jù)需要來(lái)覆蓋默認(rèn)的值或進(jìn)行特別定義。請(qǐng)參考官網(wǎng)介紹詳細(xì)了解: https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html
analyzer 指定分詞器
normalizer 指定標(biāo)準(zhǔn)化器
boost 指定權(quán)重值
coerce 強(qiáng)制類型轉(zhuǎn)換
copy_to 值復(fù)制給另一字段
doc_values 是否存儲(chǔ)docValues
dynamic
enabled 字段是否可用
fielddata
eager_global_ordinals
format 指定時(shí)間值的格式
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"
}
}
}
}
}
5. Multi Field 多重字段
當(dāng)我們需要對(duì)一個(gè)字段進(jìn)行多種不同方式的索引時(shí),可以使用fields多重字段定義。如一個(gè)字符串字段即需要進(jìn)行text分詞索引,也需要進(jìn)行keyword 關(guān)鍵字索引來(lái)支持排序、聚合;或需要用不同的分詞器進(jìn)行分詞索引。
示例:
定義多重字段:
說(shuō)明:raw是一個(gè)多重版本名(自定義)
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. 元字段
官網(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)行索引:
PUT data/_doc/1
{ "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í)間偵測(cè)
所謂時(shí)間偵測(cè)是指我們往ES里面插入數(shù)據(jù)的時(shí)候會(huì)去自動(dòng)檢測(cè)我們的數(shù)據(jù)是不是日期格式的,是的話就會(huì)給我們自動(dòng)轉(zhuǎn)為設(shè)置的格式
date_detection 默認(rèn)是開(kāi)啟的,默認(rèn)的格式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
自定義時(shí)間格式:
PUT my_index
{
"mappings": {
"_doc": {
"dynamic_date_formats": ["MM/dd/yyyy"]
}
}
}
禁用時(shí)間偵測(cè):
PUT my_index
{
"mappings": {
"_doc": {
"date_detection": false
}
}
}
7.3 Numeric detection 數(shù)值偵測(cè)
開(kāi)啟數(shù)值偵測(cè)(默認(rèn)是禁用的)
PUT my_index
{
"mappings": {
"_doc": {
"numeric_detection": true
}
}
}
PUT my_index/_doc/1
{
"my_float": "1.0",
"my_integer": "1"
}
四、索引別名
1. 別名的用途
如果希望一次查詢可查詢多個(gè)索引。
如果希望通過(guò)索引的視圖來(lái)操作索引,就像數(shù)據(jù)庫(kù)庫(kù)中的視圖一樣。
索引的別名機(jī)制,就是讓我們可以以視圖的方式來(lái)操作集群中的索引,這個(gè)視圖可是多個(gè)索引,也可是一個(gè)索引或索引的一部分。
2. 新建索引時(shí)定義別名
PUT /logs_20162801
{
"mappings" : {
"type" : {
"properties" : {
"year" : {"type" : "integer"}
}
}
},
<!-- 定義了兩個(gè)別名 -->
"aliases" : {
"current_day" : {},
"2016" : {
"filter" : {
"term" : {"year" : 2016 }
}
}
}
}
3. 創(chuàng)建別名 /_aliases
為索引test1創(chuàng)建別名alias1
POST /_aliases
{
"actions" : [
{ "add" : { "index" : "test1", "alias" : "alias1" } }
]
}
4. 刪除別名
POST /_aliases
{
"actions" : [
{ "remove" : { "index" : "test1", "alias" : "alias1" } }
]
}
還可以這樣寫(xiě)
DELETE /{index}/_alias/{name}
5. 批量操作別名
刪除索引test1的別名alias1,同時(shí)為索引test2添加別名alias1
POST /_aliases
{
"actions" : [
{ "remove" : { "index" : "test1", "alias" : "alias1" } },
{ "add" : { "index" : "test2", "alias" : "alias1" } }
]
}
6. 為多個(gè)索引定義一樣的別名
方式1:
POST /_aliases
{
"actions" : [
{ "add" : { "index" : "test1", "alias" : "alias1" } },
{ "add" : { "index" : "test2", "alias" : "alias1" } }
]
}
方式2:
POST /_aliases
{
"actions" : [
{ "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } }
]
}
注意:只可通過(guò)多索引別名進(jìn)行搜索,不可進(jìn)行文檔索引和根據(jù)id獲取文檔。
方式3:通過(guò)統(tǒng)配符*模式來(lái)指定要?jiǎng)e名的索引
POST /_aliases
{
"actions" : [
{ "add" : { "index" : "test*", "alias" : "all_test_indices" } }
]
}
注意:在這種情況下,別名是一個(gè)點(diǎn)時(shí)間別名,它將對(duì)所有匹配的當(dāng)前索引進(jìn)行別名,當(dāng)添加/刪除與此模式匹配的新索引時(shí),它不會(huì)自動(dòng)更新。
7. 帶過(guò)濾器的別名
索引中需要有字段
PUT /test1
{
"mappings": {
"type1": {
"properties": {
"user" : {
"type": "keyword"
}
}
}
}
}
過(guò)濾器通過(guò)Query DSL來(lái)定義,將作用于通過(guò)該別名來(lái)進(jìn)行的所有Search, Count, Delete By Query and More Like This 操作。
POST /_aliases
{
"actions" : [
{
"add" : {
"index" : "test1",
"alias" : "alias2",
"filter" : { "term" : { "user" : "kimchy" } }
}
}
]
}
8. 帶routing的別名
可在別名定義中指定路由值,可和filter一起使用,用來(lái)限定操作的分片,避免不需要的其他分片操作。
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"
}
}
]
}
9. 以PUT方式來(lái)定義一個(gè)別名
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
}
}
}
10. 查看別名定義信息
GET /{index}/_alias/{alias}
GET /logs_20162801/_alias/*
GET /_alias/2016
GET /_alias/20*
elasticsearch系列三:索引詳解(分詞器、文檔管理、路由詳解(集群))
一、分詞器
1. 認(rèn)識(shí)分詞器
1.1 Analyzer 分析器
在ES中一個(gè)Analyzer 由下面三種組件組合而成:
character filter :字符過(guò)濾器,對(duì)文本進(jìn)行字符過(guò)濾處理,如處理文本中的html標(biāo)簽字符。處理完后再交給tokenizer進(jìn)行分詞。一個(gè)analyzer中可包含0個(gè)或多個(gè)字符過(guò)濾器,多個(gè)按配置順序依次進(jìn)行處理。
tokenizer:分詞器,對(duì)文本進(jìn)行分詞。一個(gè)analyzer必需且只可包含一個(gè)tokenizer。
token filter:詞項(xiàng)過(guò)濾器,對(duì)tokenizer分出的詞進(jìn)行過(guò)濾處理。如轉(zhuǎn)小寫(xiě)、停用詞處理、同義詞處理。一個(gè)analyzer可包含0個(gè)或多個(gè)詞
項(xiàng)過(guò)濾器,按配置順序進(jìn)行過(guò)濾。
1.2 如何測(cè)試分詞器
POST _analyze
{
"analyzer": "whitespace",
"text": "The quick brown fox."
}
POST _analyze
{
"tokenizer": "standard",
"filter": [ "lowercase", "asciifolding" ],
"text": "Is this déja vu?"
}
position:第幾個(gè)詞
offset:詞的偏移位置
2. 內(nèi)建的character filter
HTML Strip Character Filter
html_strip :過(guò)濾html標(biāo)簽,解碼HTML entities like &.
Mapping Character Filter
mapping :用指定的字符串替換文本中的某字符串。
Pattern Replace Character Filter
pattern_replace :進(jìn)行正則表達(dá)式替換。
2.1 HTML Strip Character Filter
POST _analyze
{
"tokenizer": "keyword",
"char_filter": [ "html_strip" ],
"text": "<p>I'm so <b>happy</b>!</p>"
}
在索引中配置:
PUT my_index
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "keyword",
"char_filter": ["my_char_filter"]
}
},
"char_filter": {
"my_char_filter": {
"type": "html_strip",
"escaped_tags": ["b"]
}
}
}
}
}
escaped_tags 用來(lái)指定例外的標(biāo)簽。 如果沒(méi)有例外標(biāo)簽需配置,則不需要在此進(jìn)行客戶化定義,在上面的my_analyzer中直接使用 html_strip
測(cè)試:
POST my_index/_analyze
{
"analyzer": "my_analyzer",
"text": "<p>I'm so <b>happy</b>!</p>"
}
2.2 Mapping character filter
官網(wǎng)鏈接:https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-mapping-charfilter.html
PUT my_index
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "keyword",
"char_filter": [
"my_char_filter"
]
}
},
"char_filter": {
"my_char_filter": {
"type": "mapping",
"mappings": [
"? => 0",
"? => 1",
"? => 2",
"? => 3",
"? => 4",
"? => 5",
"? => 6",
"? => 7",
"? => 8",
"? => 9"
]
}
}
}
}
}
測(cè)試
POST my_index/_analyze
{
"analyzer": "my_analyzer",
"text": "My license plate is ?????"
}
2.3 Pattern Replace Character Filter
官網(wǎng)鏈接:https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pattern-replace-charfilter.html
PUT my_index
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "standard",
"char_filter": [
"my_char_filter"
]
}
},
"char_filter": {
"my_char_filter": {
"type": "pattern_replace",
"pattern": "(\\d+)-(?=\\d)",
"replacement": "$1_"
}
}
}
}
}
測(cè)試
POST my_index/_analyze
{
"analyzer": "my_analyzer",
"text": "My credit card is 123-456-789"
}
3. 內(nèi)建的Tokenizer
官網(wǎng)鏈接:https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-tokenizers.html
Standard Tokenizer
Letter Tokenizer
Lowercase Tokenizer
Whitespace Tokenizer
UAX URL Email Tokenizer
Classic Tokenizer
Thai Tokenizer
NGram Tokenizer
Edge NGram Tokenizer
Keyword Tokenizer
Pattern Tokenizer
Simple Pattern Tokenizer
Simple Pattern Split Tokenizer
Path Hierarchy Tokenizer
前面集成的中文分詞器Ikanalyzer中提供的tokenizer:ik_smart 、 ik_max_word
測(cè)試tokenizer
POST _analyze
{
"tokenizer": "standard",
"text": "張三說(shuō)的確實(shí)在理"
}
POST _analyze
{
"tokenizer": "ik_smart",
"text": "張三說(shuō)的確實(shí)在理"
}
4. 內(nèi)建的Token Filter
ES中內(nèi)建了很多Token filter ,詳細(xì)了解:https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-tokenizers.html
Lowercase Token Filter :lowercase 轉(zhuǎn)小寫(xiě)
Stop Token Filter :stop 停用詞過(guò)濾器
Synonym Token Filter: synonym 同義詞過(guò)濾器
說(shuō)明:中文分詞器Ikanalyzer中自帶有停用詞過(guò)濾功能。
4.1 Synonym Token Filter 同義詞過(guò)濾器
PUT /test_index
{
"settings": {
"index" : {
"analysis" : {
"analyzer" : {
"my_ik_synonym" : {
"tokenizer" : "ik_smart",
"filter" : ["synonym"]
}
},
"filter" : {
"synonym" : {
"type" : "synonym",
<!-- synonyms_path:指定同義詞文件(相對(duì)config的位置)-->
"synonyms_path" : "analysis/synonym.txt"
}
}
}
}
}
}
同義詞定義格式
ES同義詞格式支持 solr、 WordNet 兩種格式。
在analysis/synonym.txt中用solr格式定義如下同義詞
張三,李四
電飯煲,電飯鍋 => 電飯煲
電腦 => 計(jì)算機(jī),computer
注意:
文件一定要UTF-8編碼
一行一類同義詞,=> 表示標(biāo)準(zhǔn)化為
測(cè)試:通過(guò)例子的結(jié)果了解同義詞的處理行為
POST test_index/_analyze
{
"analyzer": "my_ik_synonym",
"text": "張三說(shuō)的確實(shí)在理"
}
POST test_index/_analyze
{
"analyzer": "my_ik_synonym",
"text": "我想買(mǎi)個(gè)電飯鍋和一個(gè)電腦"
}
5. 內(nèi)建的Analyzer
官網(wǎng)鏈接:
https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-analyzers.html
Standard Analyzer
Simple Analyzer
Whitespace Analyzer
Stop Analyzer
Keyword Analyzer
Pattern Analyzer
Language Analyzers
Fingerprint Analyzer
集成的中文分詞器Ikanalyzer中提供的Analyzer:ik_smart 、 ik_max_word
內(nèi)建的和集成的analyzer可以直接使用。如果它們不能滿足我們的需要,則我們可自己組合字符過(guò)濾器、分詞器、詞項(xiàng)過(guò)濾器來(lái)定義自定義的analyzer
5.1 自定義 Analyzer
配置參數(shù):
PUT my_index8
{
"settings": {
"analysis": {
"analyzer": {
"my_ik_analyzer": {
"type": "custom",
"tokenizer": "ik_smart",
"char_filter": [
"html_strip"
],
"filter": [
"synonym"
]
}
},
"filter": {
"synonym": {
"type": "synonym",
"synonyms_path": "analysis/synonym.txt"
}
} } }}
5.2 為字段指定分詞器
PUT my_index8/_mapping/_doc
{
"properties": {
"title": {
"type": "text",
"analyzer": "my_ik_analyzer"
}
}
}
如果該字段的查詢需要使用不同的analyzer
PUT my_index8/_mapping/_doc
{
"properties": {
"title": {
"type": "text",
"analyzer": "my_ik_analyzer",
"search_analyzer": "other_analyzer"
}
}
}
測(cè)試結(jié)果
PUT my_index8/_doc/1
{
"title": "張三說(shuō)的確實(shí)在理"
}
GET /my_index8/_search
{
"query": {
"term": {
"title": "張三"
}
}
}
5.3 為索引定義個(gè)default分詞器
PUT /my_index10
{
"settings": {
"analysis": {
"analyzer": {
"default": {
"tokenizer": "ik_smart",
"filter": [
"synonym"
]
}
},
"filter": {
"synonym": {
"type": "synonym",
"synonyms_path": "analysis/synonym.txt"
}
}
}
},
"mappings": {
"_doc": {
"properties": {
"title": {
"type": "text"
}
}
}
}
}
測(cè)試結(jié)果:
PUT my_index10/_doc/1
{
"title": "張三說(shuō)的確實(shí)在理"
}
GET /my_index10/_search
{
"query": {
"term": {
"title": "張三"
}
}
}
6. Analyzer的使用順序
我們可以為每個(gè)查詢、每個(gè)字段、每個(gè)索引指定分詞器。
在索引階段ES將按如下順序來(lái)選用分詞:
首先選用字段mapping定義中指定的analyzer
字段定義中沒(méi)有指定analyzer,則選用 index settings中定義的名字為default 的analyzer。
如index setting中沒(méi)有定義default分詞器,則使用 standard analyzer.
查詢階段ES將按如下順序來(lái)選用分詞:
The analyzer defined in a full-text query.
The search_analyzer defined in the field mapping.
The analyzer defined in the field mapping.
An analyzer named default_search in the index settings.
An analyzer named default in the index settings.
The standard analyzer.
二、文檔管理
1. 新建文檔
指定文檔id,新增/修改
PUT twitter/_doc/1
{
"id": 1,
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
新增,自動(dòng)生成文檔id
POST twitter/_doc/
{
"id": 1,
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
返回結(jié)果說(shuō)明:
2. 獲取單個(gè)文檔
HEAD twitter/_doc/11
GET twitter/_doc/1
不獲取文檔的source:
GET twitter/_doc/1?_source=false
獲取文檔的source:
GET twitter/_doc/1/_source
{
"_index": "twitter",
"_type": "_doc",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"id": 1,
"user": "kimchy",
"post_date": "2009-11-15T14:12:12",
"message": "trying out Elasticsearch"
}}
獲取存儲(chǔ)字段
PUT twitter11
{
"mappings": {
"_doc": {
"properties": {
"counter": {
"type": "integer",
"store": false
},
"tags": {
"type": "keyword",
"store": true
} } } }}
PUT twitter11/_doc/1
{
"counter" : 1,
"tags" : ["red"]
}
GET twitter11/_doc/1?stored_fields=tags,counter
3. 獲取多個(gè)文檔 _mget
方式1:
GET /_mget
{
"docs" : [
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "1"
},
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "2"
"stored_fields" : ["field3", "field4"]
}
]
}
方式2:
GET /twitter/_mget
{
"docs" : [
{
"_type" : "_doc",
"_id" : "1"
},
{
"_type" : "_doc",
"_id" : "2"
}
]
}
方式3:
GET /twitter/_doc/_mget
{
"docs" : [
{
"_id" : "1"
},
{
"_id" : "2"
}
]
}
方式4:
GET /twitter/_doc/_mget
{
"ids" : ["1", "2"]
}
4. 刪除文檔
指定文檔id進(jìn)行刪除
DELETE twitter/_doc/1
用版本來(lái)控制刪除
DELETE twitter/_doc/1?version=1
返回結(jié)果:
{
"_shards" : {
"total" : 2,
"failed" : 0,
"successful" : 2
},
"_index" : "twitter",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_primary_term": 1,
"_seq_no": 5,
"result": "deleted"
}
查詢刪除
POST twitter/_delete_by_query
{
"query": {
"match": {
"message": "some message"
}
}
}
當(dāng)有文檔有版本沖突時(shí),不放棄刪除操作(記錄沖突的文檔,繼續(xù)刪除其他復(fù)合查詢的文檔)
POST twitter/_doc/_delete_by_query?conflicts=proceed
{
"query": {
"match_all": {}
}
}
通過(guò)task api 來(lái)查看 查詢刪除任務(wù)
GET _tasks?detailed=true&actions=*/delete/byquery
查詢具體任務(wù)的狀態(tài)
GET /_tasks/taskId:1
取消任務(wù)
POST _tasks/task_id:1/_cancel
5. 更新文檔
指定文檔id進(jìn)行修改
PUT twitter/_doc/1
{
"id": 1,
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
樂(lè)觀鎖并發(fā)更新控制
PUT twitter/_doc/1?version=1
{
"id": 1,
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
返回結(jié)果
{
"_index": "twitter",
"_type": "_doc",
"_id": "1",
"_version": 3,
"result": "updated",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 3
}
6.Scripted update 通過(guò)腳本來(lái)更新文檔
6.1 準(zhǔn)備一個(gè)文檔
PUT uptest/_doc/1
{
"counter" : 1,
"tags" : ["red"]
}
6.2、對(duì)文檔1的counter + 4
POST uptest/_doc/1/_update
{
"script" : {
"source": "ctx._source.counter += params.count",
"lang": "painless",
"params" : {
"count" : 4
}
}
}
6.3、往數(shù)組中加入元素
POST uptest/_doc/1/_update
{
"script" : {
"source": "ctx._source.tags.add(params.tag)",
"lang": "painless",
"params" : {
"tag" : "blue"
}
}
}
腳本說(shuō)明:painless是es內(nèi)置的一種腳本語(yǔ)言,ctx執(zhí)行上下文對(duì)象(通過(guò)它還可訪問(wèn)_index, _type, _id, _version, _routing and _now (the current timestamp) ),params是參數(shù)集合
說(shuō)明:腳本更新要求索引的_source 字段是啟用的。更新執(zhí)行流程:
a、獲取到原文檔
b、通過(guò)_source字段的原始數(shù)據(jù),執(zhí)行腳本修改。
c、刪除原索引文檔
d、索引修改后的文檔
它只是降低了一些網(wǎng)絡(luò)往返,并減少了get和索引之間版本沖突的可能性。
6.4、添加一個(gè)字段
POST uptest/_doc/1/_update
{
"script" : "ctx._source.new_field = 'value_of_new_field'"
}
6.5、移除一個(gè)字段
POST uptest/_doc/1/_update
{
"script" : "ctx._source.remove('new_field')"
}
刪除值的某個(gè)元素
POST test/_doc/1/_update
{
"script" : {
"source": "if (ctx._source.tags.contains(params.tag)) { ctx._source.tags.remove(ctx._source.tags.indexOf(params.tag)) }",
"lang": "painless",
"params" : {
"tag" : "blue"
}
}
}
6.6、判斷刪除或不做什么
POST uptest/_doc/1/_update
{
"script" : {
"source": "if (ctx._source.tags.contains(params.tag)) { ctx.op = 'delete' } else { ctx.op = 'none' }",
"lang": "painless",
"params" : {
"tag" : "green"
}
}
}
設(shè)置默認(rèn)值
POST /test_index/basic/_update_by_query?conflicts=proceed
{
"script": {
"lang": "painless",
"inline": "if (ctx._source.obj_level == null) {ctx._source.obj_level= '0'} if (ctx._source.show_level == null) {ctx._source.show_level= '1'}"
},
"query": {
"bool": {
"must": [{
"term": {
"business_code": "003"
}
}, {
"term": {
"list_type": "01"
}
}],
"must_not": [],
"should": []
}
}
}
6.7、合并傳人的文檔字段進(jìn)行更新
POST uptest/_doc/1/_update
{
"doc" : {
"name" : "new_name"
}
}
6.8、再次執(zhí)行7,更新內(nèi)容相同,不需做什么
{
"_index": "uptest",
"_type": "_doc",
"_id": "1",
"_version": 4,
"result": "noop",
"_shards": {
"total": 0,
"successful": 0,
"failed": 0
}
}
6.9、設(shè)置不做noop檢測(cè)
POST uptest/_doc/1/_update
{
"doc" : {
"name" : "new_name"
},
"detect_noop": false
}
什么是noop檢測(cè)?
即已經(jīng)執(zhí)行過(guò)的腳本不再執(zhí)行
6.10、upsert 操作:如果要更新的文檔存在,則執(zhí)行腳本進(jìn)行更新,如不存在,則把 upsert中的內(nèi)容作為一個(gè)新文檔寫(xiě)入。
POST uptest/_doc/1/_update
{
"script" : {
"source": "ctx._source.counter += params.count",
"lang": "painless",
"params" : {
"count" : 4
}
},
"upsert" : {
"counter" : 1
}
}
7. 通過(guò)條件查詢來(lái)更新文檔
滿足查詢條件的才更新
POST twitter/_update_by_query
{
"script": {
"source": "ctx._source.likes++",
"lang": "painless"
},
"query": {
"term": {
"user": "kimchy"
}
}
}
8. 批量操作
批量操作API /_bulk 讓我們可以在一次調(diào)用中執(zhí)行多個(gè)索引、刪除操作。這可以大大提高索引數(shù)據(jù)的速度。批量操作內(nèi)容體需按如下以新行分割的json結(jié)構(gòu)格式給出:
語(yǔ)法:
action_and_meta_data\n
optional_source\n
action_and_meta_data\n
optional_source\n
....
action_and_meta_data\n
optional_source\n
說(shuō)明:
action_and_meta_data: action可以是 index, create, delete and update ,meta_data 指: _index ,_type,_id 請(qǐng)求端點(diǎn)可以是: /_bulk, /{index}/_bulk, {index}/{type}/_bulk
示例:
POST _bulk
{ "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "_doc", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "_doc", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "_doc", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
8.1 curl + json 文件 批量索引多個(gè)文檔
注意:accounts.json要放在執(zhí)行curl命令的同等級(jí)目錄下,后續(xù)學(xué)習(xí)的測(cè)試數(shù)據(jù)基本都使用這份銀行的數(shù)據(jù)了
curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_doc/_bulk?pretty&refresh" --data-binary "@accounts.json"
{"index":{"_id":"1"}}
{"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
{"index":{"_id":"6"}}
{"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"hattiebond@netagy.com","city":"Dante","state":"TN"}
{"index":{"_id":"13"}}
{"account_number":13,"balance":32838,"firstname":"Nanette","lastname":"Bates","age":28,"gender":"F","address":"789 Madison Street","employer":"Quility","email":"nanettebates@quility.com","city":"Nogal","state":"VA"}
{"index":{"_id":"18"}}
{"account_number":18,"balance":4180,"firstname":"Dale","lastname":"Adams","age":33,"gender":"M","address":"467 Hutchinson Court","employer":"Boink","email":"daleadams@boink.com","city":"Orick","state":"MD"}
{"index":{"_id":"20"}}
{"account_number":20,"balance":16418,"firstname":"Elinor","lastname":"Ratliff","age":36,"gender":"M","address":"282 Kings Place","employer":"Scentric","email":"elinorratliff@scentric.com","city":"Ribera","state":"WA"}
{"index":{"_id":"25"}}
{"account_number":25,"balance":40540,"firstname":"Virginia","lastname":"Ayala","age":39,"gender":"F","address":"171 Putnam Avenue","employer":"Filodyne","email":"virginiaayala@filodyne.com","city":"Nicholson","state":"PA"}
{"index":{"_id":"32"}}
{"account_number":32,"balance":48086,"firstname":"Dillard","lastname":"Mcpherson","age":34,"gender":"F","address":"702 Quentin Street","employer":"Quailcom","email":"dillardmcpherson@quailcom.com","city":"Veguita","state":"IN"}
{"index":{"_id":"37"}}
{"account_number":37,"balance":18612,"firstname":"Mcgee","lastname":"Mooney","age":39,"gender":"M","address":"826 Fillmore Place","employer":"Reversus","email":"mcgeemooney@reversus.com","city":"Tooleville","state":"OK"}
{"index":{"_id":"44"}}
{"account_number":44,"balance":34487,"firstname":"Aurelia","lastname":"Harding","age":37,"gender":"M","address":"502 Baycliff Terrace","employer":"Orbalix","email":"aureliaharding@orbalix.com","city":"Yardville","state":"DE"}
{"index":{"_id":"49"}}
{"account_number":49,"balance":29104,"firstname":"Fulton","lastname":"Holt","age":23,"gender":"F","address":"451 Humboldt Street","employer":"Anocha","email":"fultonholt@anocha.com","city":"Sunriver","state":"RI"}
{"index":{"_id":"51"}}
{"account_number":51,"balance":14097,"firstname":"Burton","lastname":"Meyers","age":31,"gender":"F","address":"334 River Street","employer":"Bezal","email":"burtonmeyers@bezal.com","city":"Jacksonburg","state":"MO"}
{"index":{"_id":"56"}}
{"account_number":56,"balance":14992,"firstname":"Josie","lastname":"Nelson","age":32,"gender":"M","address":"857 Tabor Court","employer":"Emtrac","email":"josienelson@emtrac.com","city":"Sunnyside","state":"UT"}
{"index":{"_id":"63"}}
{"account_number":63,"balance":6077,"firstname":"Hughes","lastname":"Owens","age":30,"gender":"F","address":"510 Sedgwick Street","employer":"Valpreal","email":"hughesowens@valpreal.com","city":"Guilford","state":"KS"}
{"index":{"_id":"68"}}
{"account_number":68,"balance":44214,"firstname":"Hall","lastname":"Key","age":25,"gender":"F","address":"927 Bay Parkway","employer":"Eventex","email":"hallkey@eventex.com","city":"Shawmut","state":"CA"}
{"index":{"_id":"70"}}
{"account_number":70,"balance":38172,"firstname":"Deidre","lastname":"Thompson","age":33,"gender":"F","address":"685 School Lane","employer":"Netplode","email":"deidrethompson@netplode.com","city":"Chestnut","state":"GA"}
{"index":{"_id":"75"}}
{"account_number":75,"balance":40500,"firstname":"Sandoval","lastname":"Kramer","age":22,"gender":"F","address":"166 Irvington Place","employer":"Overfork","email":"sandovalkramer@overfork.com","city":"Limestone","state":"NH"}
{"index":{"_id":"82"}}
{"account_number":82,"balance":41412,"firstname":"Concetta","lastname":"Barnes","age":39,"gender":"F","address":"195 Bayview Place","employer":"Fitcore","email":"concettabarnes@fitcore.com","city":"Summerfield","state":"NC"}
{"index":{"_id":"87"}}
{"account_number":87,"balance":1133,"firstname":"Hewitt","lastname":"Kidd","age":22,"gender":"M","address":"446 Halleck Street","employer":"Isologics","email":"hewittkidd@isologics.com","city":"Coalmont","state":"ME"}
{"index":{"_id":"94"}}
{"account_number":94,"balance":41060,"firstname":"Brittany","lastname":"Cabrera","age":30,"gender":"F","address":"183 Kathleen Court","employer":"Mixers","email":"brittanycabrera@mixers.com","city":"Cornucopia","state":"AZ"}
{"index":{"_id":"99"}}
{"account_number":99,"balance":47159,"firstname":"Ratliff","lastname":"Heath","age":39,"gender":"F","address":"806 Rockwell Place","employer":"Zappix","email":"ratliffheath@zappix.com","city":"Shaft","state":"ND"}
{"index":{"_id":"102"}}
{"account_number":102,"balance":29712,"firstname":"Dena","lastname":"Olson","age":27,"gender":"F","address":"759 Newkirk Avenue","employer":"Hinway","email":"denaolson@hinway.com","city":"Choctaw","state":"NJ"}
{"index":{"_id":"107"}}
{"account_number":107,"balance":48844,"firstname":"Randi","lastname":"Rich","age":28,"gender":"M","address":"694 Jefferson Street","employer":"Netplax","email":"randirich@netplax.com","city":"Bellfountain","state":"SC"}
{"index":{"_id":"114"}}
{"account_number":114,"balance":43045,"firstname":"Josephine","lastname":"Joseph","age":31,"gender":"F","address":"451 Oriental Court","employer":"Turnabout","email":"josephinejoseph@turnabout.com","city":"Sedley","state":"AL"}
{"index":{"_id":"119"}}
{"account_number":119,"balance":49222,"firstname":"Laverne","lastname":"Johnson","age":28,"gender":"F","address":"302 Howard Place","employer":"Senmei","email":"lavernejohnson@senmei.com","city":"Herlong","state":"DC"}
{"index":{"_id":"121"}}
{"account_number":121,"balance":19594,"firstname":"Acevedo","lastname":"Dorsey","age":32,"gender":"M","address":"479 Nova Court","employer":"Netropic","email":"acevedodorsey@netropic.com","city":"Islandia","state":"CT"}
{"index":{"_id":"126"}}
{"account_number":126,"balance":3607,"firstname":"Effie","lastname":"Gates","age":39,"gender":"F","address":"620 National Drive","employer":"Digitalus","email":"effiegates@digitalus.com","city":"Blodgett","state":"MD"}
{"index":{"_id":"133"}}
{"account_number":133,"balance":26135,"firstname":"Deena","lastname":"Richmond","age":36,"gender":"F","address":"646 Underhill Avenue","employer":"Sunclipse","email":"deenarichmond@sunclipse.com","city":"Austinburg","state":"SC"}
{"index":{"_id":"138"}}
{"account_number":138,"balance":9006,"firstname":"Daniel","lastname":"Arnold","age":39,"gender":"F","address":"422 Malbone Street","employer":"Ecstasia","email":"danielarnold@ecstasia.com","city":"Gardiner","state":"MO"}
{"index":{"_id":"140"}}
{"account_number":140,"balance":26696,"firstname":"Cotton","lastname":"Christensen","age":32,"gender":"M","address":"878 Schermerhorn Street","employer":"Prowaste","email":"cottonchristensen@prowaste.com","city":"Mayfair","state":"LA"}
{"index":{"_id":"145"}}
{"account_number":145,"balance":47406,"firstname":"Rowena","lastname":"Wilkinson","age":32,"gender":"M","address":"891 Elton Street","employer":"Asimiline","email":"rowenawilkinson@asimiline.com","city":"Ripley","state":"NH"}
{"index":{"_id":"152"}}
{"account_number":152,"balance":8088,"firstname":"Wolfe","lastname":"Rocha","age":21,"gender":"M","address":"457 Guernsey Street","employer":"Hivedom","email":"wolferocha@hivedom.com","city":"Adelino","state":"MS"}
{"index":{"_id":"157"}}
{"account_number":157,"balance":39868,"firstname":"Claudia","lastname":"Terry","age":20,"gender":"F","address":"132 Gunnison Court","employer":"Lumbrex","email":"claudiaterry@lumbrex.com","city":"Castleton","state":"MD"}
{"index":{"_id":"164"}}
{"account_number":164,"balance":9101,"firstname":"Cummings","lastname":"Little","age":26,"gender":"F","address":"308 Schaefer Street","employer":"Comtrak","email":"cummingslittle@comtrak.com","city":"Chaparrito","state":"WI"}
{"index":{"_id":"169"}}
{"account_number":169,"balance":45953,"firstname":"Hollie","lastname":"Osborn","age":34,"gender":"M","address":"671 Seaview Court","employer":"Musaphics","email":"hollieosborn@musaphics.com","city":"Hanover","state":"GA"}
{"index":{"_id":"171"}}
{"account_number":171,"balance":7091,"firstname":"Nelda","lastname":"Hopper","age":39,"gender":"M","address":"742 Prospect Place","employer":"Equicom","email":"neldahopper@equicom.com","city":"Finderne","state":"SC"}
{"index":{"_id":"176"}}
{"account_number":176,"balance":18607,"firstname":"Kemp","lastname":"Walters","age":28,"gender":"F","address":"906 Howard Avenue","employer":"Eyewax","email":"kempwalters@eyewax.com","city":"Why","state":"KY"}
{"index":{"_id":"183"}}
{"account_number":183,"balance":14223,"firstname":"Hudson","lastname":"English","age":26,"gender":"F","address":"823 Herkimer Place","employer":"Xinware","email":"hudsonenglish@xinware.com","city":"Robbins","state":"ND"}
{"index":{"_id":"188"}}
{"account_number":188,"balance":41504,"firstname":"Tia","lastname":"Miranda","age":24,"gender":"F","address":"583 Ainslie Street","employer":"Jasper","email":"tiamiranda@jasper.com","city":"Summerset","state":"UT"}
{"index":{"_id":"190"}}
{"account_number":190,"balance":3150,"firstname":"Blake","lastname":"Davidson","age":30,"gender":"F","address":"636 Diamond Street","employer":"Quantasis","email":"blakedavidson@quantasis.com","city":"Crumpler","state":"KY"}
{"index":{"_id":"195"}}
{"account_number":195,"balance":5025,"firstname":"Kaye","lastname":"Gibson","age":31,"gender":"M","address":"955 Hopkins Street","employer":"Zork","email":"kayegibson@zork.com","city":"Ola","state":"WY"}
{"index":{"_id":"203"}}
{"account_number":203,"balance":21890,"firstname":"Eve","lastname":"Wyatt","age":33,"gender":"M","address":"435 Furman Street","employer":"Assitia","email":"evewyatt@assitia.com","city":"Jamestown","state":"MN"}
{"index":{"_id":"208"}}
{"account_number":208,"balance":40760,"firstname":"Garcia","lastname":"Hess","age":26,"gender":"F","address":"810 Nostrand Avenue","employer":"Quiltigen","email":"garciahess@quiltigen.com","city":"Brooktrails","state":"GA"}
{"index":{"_id":"210"}}
{"account_number":210,"balance":33946,"firstname":"Cherry","lastname":"Carey","age":24,"gender":"M","address":"539 Tiffany Place","employer":"Martgo","email":"cherrycarey@martgo.com","city":"Fairacres","state":"AK"}
{"index":{"_id":"215"}}
{"account_number":215,"balance":37427,"firstname":"Copeland","lastname":"Solomon","age":20,"gender":"M","address":"741 McDonald Avenue","employer":"Recognia","email":"copelandsolomon@recognia.com","city":"Edmund","state":"ME"}
{"index":{"_id":"222"}}
{"account_number":222,"balance":14764,"firstname":"Rachelle","lastname":"Rice","age":36,"gender":"M","address":"333 Narrows Avenue","employer":"Enaut","email":"rachellerice@enaut.com","city":"Wright","state":"AZ"}
{"index":{"_id":"227"}}
{"account_number":227,"balance":19780,"firstname":"Coleman","lastname":"Berg","age":22,"gender":"M","address":"776 Little Street","employer":"Exoteric","email":"colemanberg@exoteric.com","city":"Eagleville","state":"WV"}
{"index":{"_id":"234"}}
{"account_number":234,"balance":44207,"firstname":"Betty","lastname":"Hall","age":37,"gender":"F","address":"709 Garfield Place","employer":"Miraclis","email":"bettyhall@miraclis.com","city":"Bendon","state":"NY"}
{"index":{"_id":"239"}}
{"account_number":239,"balance":25719,"firstname":"Chang","lastname":"Boyer","age":36,"gender":"M","address":"895 Brigham Street","employer":"Qaboos","email":"changboyer@qaboos.com","city":"Belgreen","state":"NH"}
{"index":{"_id":"241"}}
{"account_number":241,"balance":25379,"firstname":"Schroeder","lastname":"Harrington","age":26,"gender":"M","address":"610 Tapscott Avenue","employer":"Otherway","email":"schroederharrington@otherway.com","city":"Ebro","state":"TX"}
{"index":{"_id":"246"}}
{"account_number":246,"balance":28405,"firstname":"Katheryn","lastname":"Foster","age":21,"gender":"F","address":"259 Kane Street","employer":"Quantalia","email":"katherynfoster@quantalia.com","city":"Bath","state":"TX"}
{"index":{"_id":"253"}}
{"account_number":253,"balance":20240,"firstname":"Melissa","lastname":"Gould","age":31,"gender":"M","address":"440 Fuller Place","employer":"Buzzopia","email":"melissagould@buzzopia.com","city":"Lumberton","state":"MD"}
{"index":{"_id":"258"}}
{"account_number":258,"balance":5712,"firstname":"Lindsey","lastname":"Hawkins","age":37,"gender":"M","address":"706 Frost Street","employer":"Enormo","email":"lindseyhawkins@enormo.com","city":"Gardners","state":"AK"}
{"index":{"_id":"260"}}
{"account_number":260,"balance":2726,"firstname":"Kari","lastname":"Skinner","age":30,"gender":"F","address":"735 Losee Terrace","employer":"Singavera","email":"kariskinner@singavera.com","city":"Rushford","state":"WV"}
{"index":{"_id":"265"}}
{"account_number":265,"balance":46910,"firstname":"Marion","lastname":"Schneider","age":26,"gender":"F","address":"574 Everett Avenue","employer":"Evidends","email":"marionschneider@evidends.com","city":"Maplewood","state":"WY"}
{"index":{"_id":"272"}}
{"account_number":272,"balance":19253,"firstname":"Lilly","lastname":"Morgan","age":25,"gender":"F","address":"689 Fleet Street","employer":"Biolive","email":"lillymorgan@biolive.com","city":"Sunbury","state":"OH"}
{"index":{"_id":"277"}}
{"account_number":277,"balance":29564,"firstname":"Romero","lastname":"Lott","age":31,"gender":"M","address":"456 Danforth Street","employer":"Plasto","email":"romerolott@plasto.com","city":"Vincent","state":"VT"}
{"index":{"_id":"284"}}
{"account_number":284,"balance":22806,"firstname":"Randolph","lastname":"Banks","age":29,"gender":"M","address":"875 Hamilton Avenue","employer":"Caxt","email":"randolphbanks@caxt.com","city":"Crawfordsville","state":"WA"}
{"index":{"_id":"289"}}
{"account_number":289,"balance":7798,"firstname":"Blair","lastname":"Church","age":29,"gender":"M","address":"370 Sutton Street","employer":"Cubix","email":"blairchurch@cubix.com","city":"Nile","state":"NH"}
{"index":{"_id":"291"}}
{"account_number":291,"balance":19955,"firstname":"Lynn","lastname":"Pollard","age":40,"gender":"F","address":"685 Pierrepont Street","employer":"Slambda","email":"lynnpollard@slambda.com","city":"Mappsville","state":"ID"}
{"index":{"_id":"296"}}
{"account_number":296,"balance":24606,"firstname":"Rosa","lastname":"Oliver","age":34,"gender":"M","address":"168 Woodbine Street","employer":"Idetica","email":"rosaoliver@idetica.com","city":"Robinson","state":"WY"}
{"index":{"_id":"304"}}
{"account_number":304,"balance":28647,"firstname":"Palmer","lastname":"Clark","age":35,"gender":"M","address":"866 Boulevard Court","employer":"Maximind","email":"palmerclark@maximind.com","city":"Avalon","state":"NH"}
{"index":{"_id":"309"}}
{"account_number":309,"balance":3830,"firstname":"Rosemarie","lastname":"Nieves","age":30,"gender":"M","address":"206 Alice Court","employer":"Zounds","email":"rosemarienieves@zounds.com","city":"Ferney","state":"AR"}
{"index":{"_id":"311"}}
{"account_number":311,"balance":13388,"firstname":"Vinson","lastname":"Ballard","age":23,"gender":"F","address":"960 Glendale Court","employer":"Gynk","email":"vinsonballard@gynk.com","city":"Fairforest","state":"WY"}
{"index":{"_id":"316"}}
{"account_number":316,"balance":8214,"firstname":"Anita","lastname":"Ewing","age":32,"gender":"M","address":"396 Lombardy Street","employer":"Panzent","email":"anitaewing@panzent.com","city":"Neahkahnie","state":"WY"}
{"index":{"_id":"323"}}
{"account_number":323,"balance":42230,"firstname":"Chelsea","lastname":"Gamble","age":34,"gender":"F","address":"356 Dare Court","employer":"Isosphere","email":"chelseagamble@isosphere.com","city":"Dundee","state":"MD"}
{"index":{"_id":"328"}}
{"account_number":328,"balance":12523,"firstname":"Good","lastname":"Campbell","age":27,"gender":"F","address":"438 Hicks Street","employer":"Gracker","email":"goodcampbell@gracker.com","city":"Marion","state":"CA"}
{"index":{"_id":"330"}}
{"account_number":330,"balance":41620,"firstname":"Yvette","lastname":"Browning","age":34,"gender":"F","address":"431 Beekman Place","employer":"Marketoid","email":"yvettebrowning@marketoid.com","city":"Talpa","state":"CO"}
{"index":{"_id":"335"}}
{"account_number":335,"balance":35433,"firstname":"Vera","lastname":"Hansen","age":24,"gender":"M","address":"252 Bushwick Avenue","employer":"Zanilla","email":"verahansen@zanilla.com","city":"Manila","state":"TN"}
{"index":{"_id":"342"}}
{"account_number":342,"balance":33670,"firstname":"Vivian","lastname":"Wells","age":36,"gender":"M","address":"570 Cobek Court","employer":"Nutralab","email":"vivianwells@nutralab.com","city":"Fontanelle","state":"OK"}
{"index":{"_id":"347"}}
{"account_number":347,"balance":36038,"firstname":"Gould","lastname":"Carson","age":24,"gender":"F","address":"784 Pulaski Street","employer":"Mobildata","email":"gouldcarson@mobildata.com","city":"Goochland","state":"MI"}
{"index":{"_id":"354"}}
{"account_number":354,"balance":21294,"firstname":"Kidd","lastname":"Mclean","age":22,"gender":"M","address":"691 Saratoga Avenue","employer":"Ronbert","email":"kiddmclean@ronbert.com","city":"Tioga","state":"ME"}
{"index":{"_id":"359"}}
{"account_number":359,"balance":29927,"firstname":"Vanessa","lastname":"Harvey","age":28,"gender":"F","address":"679 Rutledge Street","employer":"Zentime","email":"vanessaharvey@zentime.com","city":"Williston","state":"IL"}
{"index":{"_id":"361"}}
{"account_number":361,"balance":23659,"firstname":"Noreen","lastname":"Shelton","age":36,"gender":"M","address":"702 Tillary Street","employer":"Medmex","email":"noreenshelton@medmex.com","city":"Derwood","state":"NH"}
{"index":{"_id":"366"}}
{"account_number":366,"balance":42368,"firstname":"Lydia","lastname":"Cooke","age":31,"gender":"M","address":"470 Coleman Street","employer":"Comstar","email":"lydiacooke@comstar.com","city":"Datil","state":"TN"}
{"index":{"_id":"373"}}
{"account_number":373,"balance":9671,"firstname":"Simpson","lastname":"Carpenter","age":21,"gender":"M","address":"837 Horace Court","employer":"Snips","email":"simpsoncarpenter@snips.com","city":"Tolu","state":"MA"}
{"index":{"_id":"378"}}
{"account_number":378,"balance":27100,"firstname":"Watson","lastname":"Simpson","age":36,"gender":"F","address":"644 Thomas Street","employer":"Wrapture","email":"watsonsimpson@wrapture.com","city":"Keller","state":"TX"}
{"index":{"_id":"380"}}
{"account_number":380,"balance":35628,"firstname":"Fernandez","lastname":"Reid","age":33,"gender":"F","address":"154 Melba Court","employer":"Cosmosis","email":"fernandezreid@cosmosis.com","city":"Boyd","state":"NE"}
{"index":{"_id":"385"}}
{"account_number":385,"balance":11022,"firstname":"Rosalinda","lastname":"Valencia","age":22,"gender":"M","address":"933 Lloyd Street","employer":"Zoarere","email":"rosalindavalencia@zoarere.com","city":"Waverly","state":"GA"}
{"index":{"_id":"392"}}
{"account_number":392,"balance":31613,"firstname":"Dotson","lastname":"Dean","age":35,"gender":"M","address":"136 Ford Street","employer":"Petigems","email":"dotsondean@petigems.com","city":"Chical","state":"SD"}
{"index":{"_id":"397"}}
{"account_number":397,"balance":37418,"firstname":"Leonard","lastname":"Gray","age":36,"gender":"F","address":"840 Morgan Avenue","employer":"Recritube","email":"leonardgray@recritube.com","city":"Edenburg","state":"AL"}
{"index":{"_id":"400"}}
{"account_number":400,"balance":20685,"firstname":"Kane","lastname":"King","age":21,"gender":"F","address":"405 Cornelia Street","employer":"Tri@Tribalog","email":"kaneking@tri@tribalog.com","city":"Gulf","state":"VT"}
{"index":{"_id":"405"}}
{"account_number":405,"balance":5679,"firstname":"Strickland","lastname":"Fuller","age":26,"gender":"M","address":"990 Concord Street","employer":"Digique","email":"stricklandfuller@digique.com","city":"Southmont","state":"NV"}
{"index":{"_id":"412"}}
{"account_number":412,"balance":27436,"firstname":"Ilene","lastname":"Abbott","age":26,"gender":"M","address":"846 Vine Street","employer":"Typhonica","email":"ileneabbott@typhonica.com","city":"Cedarville","state":"VT"}
{"index":{"_id":"417"}}
{"account_number":417,"balance":1788,"firstname":"Wheeler","lastname":"Ayers","age":35,"gender":"F","address":"677 Hope Street","employer":"Fortean","email":"wheelerayers@fortean.com","city":"Ironton","state":"PA"}
{"index":{"_id":"424"}}
{"account_number":424,"balance":36818,"firstname":"Tracie","lastname":"Gregory","age":34,"gender":"M","address":"112 Hunterfly Place","employer":"Comstruct","email":"traciegregory@comstruct.com","city":"Onton","state":"TN"}
{"index":{"_id":"429"}}
{"account_number":429,"balance":46970,"firstname":"Cantu","lastname":"Lindsey","age":31,"gender":"M","address":"404 Willoughby Avenue","employer":"Inquala","email":"cantulindsey@inquala.com","city":"Cowiche","state":"IA"}
{"index":{"_id":"431"}}
{"account_number":431,"balance":13136,"firstname":"Laurie","lastname":"Shaw","age":26,"gender":"F","address":"263 Aviation Road","employer":"Zillanet","email":"laurieshaw@zillanet.com","city":"Harmon","state":"WV"}
{"index":{"_id":"436"}}
{"account_number":436,"balance":27585,"firstname":"Alexander","lastname":"Sargent","age":23,"gender":"M","address":"363 Albemarle Road","employer":"Fangold","email":"alexandersargent@fangold.com","city":"Calpine","state":"OR"}
{"index":{"_id":"443"}}
{"account_number":443,"balance":7588,"firstname":"Huff","lastname":"Thomas","age":23,"gender":"M","address":"538 Erskine Loop","employer":"Accufarm","email":"huffthomas@accufarm.com","city":"Corinne","state":"AL"}
{"index":{"_id":"448"}}
{"account_number":448,"balance":22776,"firstname":"Adriana","lastname":"Mcfadden","age":35,"gender":"F","address":"984 Woodside Avenue","employer":"Telequiet","email":"adrianamcfadden@telequiet.com","city":"Darrtown","state":"WI"}
{"index":{"_id":"450"}}
{"account_number":450,"balance":2643,"firstname":"Bradford","lastname":"Nielsen","age":25,"gender":"M","address":"487 Keen Court","employer":"Exovent","email":"bradfordnielsen@exovent.com","city":"Hamilton","state":"DE"}
{"index":{"_id":"455"}}
{"account_number":455,"balance":39556,"firstname":"Lynn","lastname":"Tran","age":36,"gender":"M","address":"741 Richmond Street","employer":"Optyk","email":"lynntran@optyk.com","city":"Clinton","state":"WV"}
{"index":{"_id":"462"}}
{"account_number":462,"balance":10871,"firstname":"Calderon","lastname":"Day","age":27,"gender":"M","address":"810 Milford Street","employer":"Cofine","email":"calderonday@cofine.com","city":"Kula","state":"OK"}
{"index":{"_id":"467"}}
{"account_number":467,"balance":6312,"firstname":"Angelica","lastname":"May","age":32,"gender":"F","address":"384 Karweg Place","employer":"Keeg","email":"angelicamay@keeg.com","city":"Tetherow","state":"IA"}
{"index":{"_id":"474"}}
{"account_number":474,"balance":35896,"firstname":"Obrien","lastname":"Walton","age":40,"gender":"F","address":"192 Ide Court","employer":"Suremax","email":"obrienwalton@suremax.com","city":"Crucible","state":"UT"}
{"index":{"_id":"479"}}
{"account_number":479,"balance":31865,"firstname":"Cameron","lastname":"Ross","age":40,"gender":"M","address":"904 Bouck Court","employer":"Telpod","email":"cameronross@telpod.com","city":"Nord","state":"MO"}
{"index":{"_id":"481"}}
{"account_number":481,"balance":20024,"firstname":"Lina","lastname":"Stanley","age":33,"gender":"M","address":"361 Hanover Place","employer":"Strozen","email":"linastanley@strozen.com","city":"Wyoming","state":"NC"}
{"index":{"_id":"486"}}
{"account_number":486,"balance":35902,"firstname":"Dixie","lastname":"Fuentes","age":22,"gender":"F","address":"991 Applegate Court","employer":"Portico","email":"dixiefuentes@portico.com","city":"Salix","state":"VA"}
{"index":{"_id":"493"}}
{"account_number":493,"balance":5871,"firstname":"Campbell","lastname":"Best","age":24,"gender":"M","address":"297 Friel Place","employer":"Fanfare","email":"campbellbest@fanfare.com","city":"Kidder","state":"GA"}
{"index":{"_id":"498"}}
{"account_number":498,"balance":10516,"firstname":"Stella","lastname":"Hinton","age":39,"gender":"F","address":"649 Columbia Place","employer":"Flyboyz","email":"stellahinton@flyboyz.com","city":"Crenshaw","state":"SC"}
9. reindex 重索引
Reindex API /_reindex 讓我們可以將一個(gè)索引中的數(shù)據(jù)重索引到另一個(gè)索引中(拷貝),要求源索引的_source 是開(kāi)啟的。目標(biāo)索引的setting 、mapping 信息與源索引無(wú)關(guān)。
什么時(shí)候需要重索引?
即當(dāng)需要做數(shù)據(jù)的拷貝的時(shí)候
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}
重索引要考慮的一個(gè)問(wèn)題:目標(biāo)索引中存在源索引中的數(shù)據(jù),這些數(shù)據(jù)的version如何處理。
1、如果沒(méi)有指定version_type 或指定為 internal,則會(huì)是采用目標(biāo)索引中的版本,重索引過(guò)程中,執(zhí)行的就是新增、更新操作。
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter",
"version_type": "internal"
}
}
2、如果想使用源索引中的版本來(lái)進(jìn)行版本控制更新,則設(shè)置 version_type 為extenal。重索引操作將寫(xiě)入不存在的,更新舊版本的數(shù)據(jù)。
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter",
"version_type": "external"
}
}
如果你只想從源索引中復(fù)制目標(biāo)索引中不存在的文檔數(shù)據(jù),可以指定 op_type 為 create 。此時(shí)存在的文檔將觸發(fā) 版本沖突(會(huì)導(dǎo)致放棄操作),可設(shè)置“conflicts”: “proceed“,跳過(guò)繼續(xù)
POST _reindex
{
"conflicts": "proceed",
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter",
"op_type": "create"
}
}
你也可以只索引源索引的一部分?jǐn)?shù)據(jù),通過(guò) type 或 查詢來(lái)指定你需要的數(shù)據(jù)
POST _reindex
{
"source": {
"index": "twitter",
"type": "_doc",
"query": {
"term": {
"user": "kimchy"
}
}
},
"dest": {
"index": "new_twitter"
}
}
可以從多個(gè)源獲取數(shù)據(jù)
POST _reindex
{
"source": {
"index": ["twitter", "blog"],
"type": ["_doc", "post"]
},
"dest": {
"index": "all_together"
}
}
可以限定文檔數(shù)量
POST _reindex
{
"size": 10000,
"source": {
"index": "twitter",
"sort": { "date": "desc" }
},
"dest": {
"index": "new_twitter"
}
}
可以選擇復(fù)制源文檔的哪些字段
POST _reindex
{
"source": {
"index": "twitter",
"_source": ["user", "_doc"]
},
"dest": {
"index": "new_twitter"
}
}
可以用script來(lái)改變文檔
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter",
"version_type": "external"
},
"script": {
"source": "if (ctx._source.foo == 'bar') {ctx._version++; ctx._source.remove('foo')}",
"lang": "painless"
}
}
可以指定路由值把文檔放到哪個(gè)分片上
POST _reindex
{
"source": {
"index": "source",
"query": {
"match": {
"company": "cat"
}
}
},
"dest": {
"index": "dest",
"routing": "=cat"
}
}
從遠(yuǎn)程源復(fù)制
POST _reindex
{
"source": {
"remote": {
"host": "http://otherhost:9200",
"username": "user",
"password": "pass"
},
"index": "source",
"query": {
"match": {
"test": "data"
}
}
},
"dest": {
"index": "dest"
}
}
或者
POST /_reindex
{
"source": {
"index": "users",
"remote": {
"host": "http://192.168.121.128:9200"
}
},
"dest": {
"index": "users_test"
}
}
增加查詢條件排除字段復(fù)制(刪除字段)
POST _reindex
{
"source": {
"index": "twitter_index",
"slice": {
"id": 1,
"max": 2
},
"_source": {
"excludes": ["*value", , "*name", "obj_key", "obj_level"]
},
"query": {
"term": {
"list_type": {
"value": 01
}
}
}
},
"dest": {
"index": "new_twitter_index"
}
}
通過(guò)_task 來(lái)查詢執(zhí)行狀態(tài)
GET _tasks?detailed=true&actions=*reindex
10. refresh
對(duì)于索引、更新、刪除操作如果想操作完后立馬重刷新可見(jiàn),可帶上refresh參數(shù)
PUT /test/_doc/1?refresh
{"test": "test"}
PUT /test/_doc/2?refresh=true
{"test": "test"}
refresh 可選值說(shuō)明
未給值或=true,則立馬會(huì)重刷新讀索引。
=false ,相當(dāng)于沒(méi)帶refresh 參數(shù),遵循內(nèi)部的定時(shí)刷新。
=wait_for ,登記等待刷新,當(dāng)?shù)怯浀恼?qǐng)求數(shù)達(dá)到index.max_refresh_listeners 參數(shù)設(shè)定的值時(shí)(defaults to 1000),將觸發(fā)重刷新。
11.數(shù)組長(zhǎng)度過(guò)濾查詢
數(shù)據(jù)樣式
{
"query": {
"bool": {
"filter": {
"script": {
"script": "doc['tag_code'].values.length > 3"
}
}
}
},
"size": 10
}
增加查詢條件查詢
GET /test_inde*/_search
{
"query": {
"filtered": {
"query": {
"match": {
"title": {
"query": "黃曉明",
"operator": "and",
"minimum_should_match": "90%"
}
}
},
"filter": {
"script" : {
"script" : "doc['tag_code'].size() < 9"
}
}
}
}
}
三、路由詳解
1. 集群組成
第一個(gè)節(jié)點(diǎn)啟動(dòng)
說(shuō)明:首先啟動(dòng)的一定是主節(jié)點(diǎn),主節(jié)點(diǎn)存儲(chǔ)的是集群的元數(shù)據(jù)信息
Node2啟動(dòng)
說(shuō)明:
Node2節(jié)點(diǎn)啟動(dòng)之前會(huì)配置集群的名稱Cluster-name:ess,然后配置可以作為主節(jié)點(diǎn)的ip地址信息discovery.zen.ping.unicast.hosts: [“10.0.1.11",“10.0.1.12"],配置自己的ip地址networ.host: 10.0.1.12;
Node2啟動(dòng)的過(guò)程中會(huì)去找到主節(jié)點(diǎn)Node1告訴Node1我要加入到集群里面了,主節(jié)點(diǎn)Node1接收到請(qǐng)求以后看Node2是否滿足加入集群的條件,如果滿足就把node2的ip地址加入的元信息里面,然后廣播給集群中的其他節(jié)點(diǎn)有
新節(jié)點(diǎn)加入,并把最新的元信息發(fā)送給其他的節(jié)點(diǎn)去更新
Node3..NodeN加入
說(shuō)明:集群中的所有節(jié)點(diǎn)的元信息都是和主節(jié)點(diǎn)一致的,因?yàn)橐坏┯行碌墓?jié)點(diǎn)加入進(jìn)來(lái),主節(jié)點(diǎn)會(huì)通知其他的節(jié)點(diǎn)同步元信息
2. 在集群中創(chuàng)建索引的流程
3. 有索引的集群
4. 集群有節(jié)點(diǎn)出現(xiàn)故障,如主節(jié)點(diǎn)掛了,會(huì)重新選擇主節(jié)點(diǎn)
5. 在集群中索引文檔
索引文檔的步驟:
1、node2計(jì)算文檔的路由值得到文檔存放的分片(假定路由選定的是分片0)。
2、將文檔轉(zhuǎn)發(fā)給分片0(P0)的主分片節(jié)點(diǎn) node1。
3、node1索引文檔,同步給副本(R0)節(jié)點(diǎn)node3索引文檔。
4、node1向node2反饋結(jié)果
5、node2作出響應(yīng)
6. 文檔是如何路由的
文檔該存到哪個(gè)分片上?
決定文檔存放到哪個(gè)分片上就是文檔路由。ES中通過(guò)下面的計(jì)算得到每個(gè)文檔的存放分片:
shard = hash(routing) % number_of_primary_shards
參數(shù)說(shuō)明:
routing 是用來(lái)進(jìn)行hash計(jì)算的路由值,默認(rèn)是使用文檔id值。我們可以在索引文檔時(shí)通過(guò)routing參數(shù)指定別的路由值
number_of_primary_shards:創(chuàng)建索引時(shí)指定的主分片數(shù)
POST twitter/_doc?routing=kimchy
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
在索引、刪除、更新、查詢中都可以使用routing參數(shù)(可多值)指定操作的分片。
創(chuàng)建索引時(shí)強(qiáng)制要求給定路由值:
PUT my_index2
{
"mappings": {
"_doc": {
"_routing": {
"required": true
}
}
}
}
7. 在集群中進(jìn)行搜索
搜索的步驟:如要搜索 索引 s0
1、node2解析查詢。
2、node2將查詢發(fā)給索引s0的分片/副本(R1,R2,R0)節(jié)點(diǎn)
3、各節(jié)點(diǎn)執(zhí)行查詢,將結(jié)果發(fā)給Node2
4、Node2合并結(jié)果,作出響應(yīng)。
8. Master節(jié)點(diǎn)的工作是什么?
1. 存儲(chǔ)集群的元信息,如集群名稱、集群中的節(jié)點(diǎn)
2. 轉(zhuǎn)發(fā)創(chuàng)建索引和索引文檔的請(qǐng)求
3. 和其他的節(jié)點(diǎn)進(jìn)行通信,告訴其他節(jié)點(diǎn)有新的節(jié)點(diǎn)加入等
原文鏈接: https://blog.csdn.net/ZYC88888/article/details/91463253