網(wǎng)站建設(shè)的一般步驟精準(zhǔn)粉絲引流推廣
一、前言
上一篇文章中我們學(xué)習(xí)了ES中的基礎(chǔ)操作,包括索引和映射,同時也學(xué)習(xí)了ES中的基礎(chǔ)數(shù)據(jù)類型,今天我們繼續(xù)學(xué)習(xí)其他的數(shù)據(jù)類型。
二、復(fù)雜數(shù)據(jù)類型
1、數(shù)組(Array)
在ES中沒有特別指定數(shù)據(jù)類型,換句話說任何類型的字段都可以組成數(shù)組。對于一個數(shù)組內(nèi)所有數(shù)據(jù)的類型必須一致。例如 array1 [1,2,3,4,5] ,對于對象數(shù)組(數(shù)組內(nèi)的元素是對象)有些特別,ES不支持獨(dú)立的查詢數(shù)組內(nèi)的對象,如果想使用則使用 Nested(嵌套類型),這個放到后面學(xué)習(xí)。
案例:
PUT my-index-000001/_doc/1
{"message": "some arrays in this document...","tags": [ "elasticsearch", "wow" ], "lists": [ {"name": "prog_list","description": "programming list"},{"name": "cool_list","description": "cool stuff list"}]
}PUT my-index-000001/_doc/2
{"message": "no arrays in this document...","tags": "elasticsearch","lists": {"name": "prog_list","description": "programming list"}
}GET my-index-000001/_search
{"query": {"match": {"tags": "elasticsearch" }}
}
這里可以看到文檔2并沒有數(shù)組,但是也可以正常寫入,再解釋一下上面說的無法單獨(dú)查詢數(shù)組內(nèi)的對象,因?yàn)镋S會把數(shù)組扁平化,所以我搜索“prog_list”,也會把“cool_list”帶出來,因?yàn)樗麄冊谝粋€數(shù)組內(nèi),而不是單獨(dú)的對象。
2、Object
對于JSON對象來說很多時候是分層的,也就是說有內(nèi)部對象,此時就可以使用Object類型
案例:
PUT my-index-000001/_doc/1
{ "region": "US","manager": { "age": 30,"name": { "first": "John","last": "Smith"}}
}當(dāng)然也可以換一種方式創(chuàng)建
PUT my-index-000001
{"mappings": {"properties": { "region": {"type": "keyword"},"manager": { "properties": {"age": { "type": "integer" },"name": { "properties": {"first": { "type": "text" },"last": { "type": "text" }}}}}}}
}
3、Nested
嵌套類型是對象數(shù)據(jù)類型的特殊版本,它允許以可以彼此獨(dú)立查詢的方式對對象數(shù)組進(jìn)行索引。
案例
PUT my-index-000001
{"mappings": {"properties": {"user": {"type": "nested" }}}
}PUT my-index-000001/_doc/1
{"group" : "fans","user" : [{"first" : "John","last" : "Smith"},{"first" : "Alice","last" : "White"}]
}GET my-index-000001/_search
{"query": {"nested": {"path": "user","query": {"bool": {"must": [{ "match": { "user.first": "Alice" }},{ "match": { "user.last": "Smith" }} ]}}}}
}GET my-index-000001/_search
{"query": {"nested": {"path": "user","query": {"bool": {"must": [{ "match": { "user.first": "Alice" }},{ "match": { "user.last": "White" }} ]}},"inner_hits": { "highlight": {"fields": {"user.first": {}}}}}}
}
與上面數(shù)組很像,不同的是Nested支持對數(shù)組內(nèi)的數(shù)據(jù)單獨(dú)進(jìn)行搜索,這里不多贅述。
4、其他
ES中還有很多其他的數(shù)據(jù)類型,具體的可以參考 Field data types | Elasticsearch Guide [7.17] | Elastic
https://www.elastic.co/guide/en/elasticsearch/reference/7.17/mapping-types.html
三、文檔操作
1、創(chuàng)建文檔
語法:PUT /索引名/_doc/ID 或者 POST /索引名/_doc/ID
解釋:ID可選,不一定要填寫,如果不填寫則由。
案例1:創(chuàng)建一個文檔,不帶ID
POST my-index-000001/_doc/
{"@timestamp": "2099-11-15T13:12:00","message": "GET /search HTTP/1.1 200 1070000","user": {"id": "kimchy"}
}
上面這個案例我們創(chuàng)建了一個文檔,同時沒有指定ID,由ES為我們自動創(chuàng)建文檔ID。
案例2:創(chuàng)建文檔并且自定義文檔
POST my-index-000001/_doc/1
{"@timestamp": "2099-11-15T13:12:00","message": "GET /search HTTP/1.1 200 1070000","user": {"id": "hardy"}
}
這樣我們就創(chuàng)建了一個自定義ID的文檔
2、查詢文檔
查詢是ES中最核心的功能,這部分內(nèi)容后續(xù)將單獨(dú)抽取出來,這里暫時不講述
3、刪除文檔
1、根據(jù)ID刪除
DELETE /索引名/ID
2、根據(jù)條件刪除
根據(jù)條件刪除和搜索是很類型的,這部分也放到后續(xù)學(xué)習(xí)搜索時一起講
4、更新文檔
1、根據(jù)ID更新
POST /<index>/_update/<_id>
{"doc": {"field1": "new_value1","field2": "new_value2"}
}
2、根據(jù)條件更新
根據(jù)條件更新和搜索是很類型的,這部分也放到后續(xù)學(xué)習(xí)搜索時一起講
四、結(jié)束語
今天學(xué)習(xí)了剩下的常見的數(shù)據(jù)類型,還有簡單的文檔操作。這些相對來說簡單,ES中最復(fù)雜的是搜索,搜索會放到后面詳細(xì)講解,希望對你有所幫助。