ElasticSearch 常用字段類型及增刪改查
ES常用的數據類型可分為3大類
- 核⼼數據類型
- 復雜數據類型
- 專⽤數據類型
核心數據類型
(1)字符串
- text ⽤於全⽂索引,搜索時會自動使用分詞器進⾏分詞再匹配
- keyword 不分詞,搜索時需要匹配完整的值
(2)數值型
- 整型: byte,short,integer,long
- 浮點型: float, half_float, scaled_float,double
(3)日期類型
- date
json沒有date類型,插入|更新文檔|字段時怎么表示date類型?
#mapping,將字段類型設置為date
"type" : "date"
#插入|更新此字段的值時,有3種表示方式
#使用固定格式的字符串
"2020-04-18"、"2020/04/18 09:00:00"
#值使用長整型的時間戳,1970-01-01 00:00:00,s
1610350870
#值使用長整型的時間戳,ms
1641886870000
(4)范圍型
integer_range, long_range, float_range,double_range,date_range
比如招聘要求年齡在[20, 40]上,mapping:
age_limit :{
"type" : "integer_range"
}
插入|更新文檔|字段時,值寫成json對象的形式:
"age_limit" : {
"gte" : 20,
"lte" : 40
}
gt是大於,lt是小於,e是equals等於。
按此字段搜索時,值寫常量:
"term" : {
"age_limit" : 30
}
age_limit的區間包含了此值的文檔都算是匹配。
(5)布爾
- boolean #true、false
(6)⼆進制
- binary 會把值當做經過 base64 編碼的字符串,默認不存儲,且不可搜索
復雜數據類型
(1)對象
- object
#定義mapping
"user" : {
"type":"object"
}
#插入|更新字段的值,值寫成json對象的形式
"user" : {
"name":"chy",
"age":12
}
#搜索時,字段名使用點號連接
"match":{
"user.name":"chy"
}
一個對象中可以嵌套對象。
(2)數組
#ES沒有專門的數組類型,定義mapping,寫成元素的類型
"arr" : {
"type":"integer"
}
#插入|更新字段的值。元素可以是各種類型,但元素的類型要相同
"arr" : [1,3,4]
專用數據類型
- ip
#定義mapping
"ip_address" : {
"type":"ip"
}
#插入|更新字段的值,值寫成字符串形式
"ip" : "192.168.1.1"
#搜索
"match":{
"ip_address":"192.168.1.1"
}
#ip在192.168.0.0 ~ 192.168.255.255上的文檔都匹配
"match":{
"ip_address":"192.168.0.0/16"
}
ElasticSearch 索引查詢
- 我們通常用用
_cat
API檢測集群是否健康。 確保9200端口號可用:
curl 'localhost:9200/_cat/health?v'
綠色表示一切正常, 黃色表示所有的數據可用但是部分副本還沒有分配,紅色表示部分數據因為某些原因不可用.
2.通過如下語句,我們可以獲取集群的節點列表:
curl 'localhost:9200/_cat/nodes?v'
3.通過如下語句,列出所有索引:
curl 'localhost:9200/_cat/indices?v'
4.創建索引
現在我們創建一個名為“customer”的索引,然后再查看所有的索引:
curl -XPUT 'localhost:9200/customer?pretty'
curl 'localhost:9200/_cat/indices?v'
5.插入
現在我么插入一些數據到集群索引。我們必須給ES指定索引的類型。如下語句:"external" type, ID:1:
主體為JSON格式的語句: { "name": "John Doe" }
curl -H "Content-Type: application/json" -XPUT 'localhost:9200/customer/external/1?pretty' -d '{"name":"join"}'
上述命令語句是:先新增id為1,name為John Doe的數據,然后將id為1的name修改為join。
6.獲取GET
curl -XGET 'localhost:9200/customer/external/1?pretty'
其中含義為:獲取customer索引下類型為external,id為1的數據,pretty參數表示返回結果格式美觀。
7.刪除索引 DELETE
curl -XDELETE 'localhost:9200/customer?pretty'
curl 'localhost:9200/_cat/indices?v'
8.通過以上命令語句的學習,我們發現索引的增刪改查有一個類似的格式,總結如下:
curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>
<REST Verb>:REST風格的語法謂詞
<Node>:節點ip
<port>:節點端口號,默認9200
<Index>:索引名
<Type>:索引類型
<ID>:操作對象的ID號
9.更新數據
這個例子展示如何將id為1文檔的name字段更新為hello world:
curl -H "Content-Type:application/json" -XPOST 'localhost:9200/customer/external/1?pretty' -d '{"name":"john"}'
curl -H "Content-Type:application/json" -XPOST 'localhost:9200/customer/external/1?pretty' -d '{"name":"hello world"}'
10、刪除數據
刪除數據那是相當的直接. 下面的語句將執行刪除Customer中ID為1的數據:
curl -XDELETE 'localhost:9200/customer/external/1?pretty'
11.批處理
舉例:
在一個批量操作中執行創建索引:
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'
批處理執行更新id為1的數據然后執行刪除id為2的數據
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
13.查詢
curl 'localhost:9200/bank/_search?q=*&pretty'
上面示例返回所有bank中的索引數據。其中 q=* 表示匹配索引中所有的數據。
等價於:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} }
}'
- 查詢語言
匹配所有數據,但只返回1個:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"size": 1
}'
注意:如果size不指定,則默認返回10條數據。
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"from": 10,
"size": 10
}'
返回從11到20的數據。(索引下標從0開始)
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }
}'
上述示例匹配所有的索引中的數據,按照balance字段降序排序,並且返回前10條(如果不指定size,默認最多返回10條)。
返回account_number 為20 的數據:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "account_number": 20 } }
}'
返回address中包含mill的所有數據::
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "address": "mill" } }
}'
返回地址中包含mill或者lane的所有數據:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "address": "mill lane" } }
}'
和上面匹配單個詞語不同,下面這個例子是多匹配(match_phrase短語匹配),返回地址中包含短語 “mill lane”的所有數據:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_phrase": { "address": "mill lane" } }
}'
以下是布爾查詢,布爾查詢允許我們將多個簡單的查詢組合成一個更復雜的布爾邏輯查詢。
這個例子將兩個查詢組合,返回地址中含有mill和lane的所有記錄數據:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}'
上述例子中,must表示所有查詢必須都為真才被認為匹配。
相反, 這個例子組合兩個查詢,返回地址中含有mill或者lane的所有記錄數據:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}'
上述例子中,bool表示查詢列表中只要有任何一個為真則認為匹配。
下面例子組合兩個查詢,返回地址中既沒有mill也沒有lane的所有數據:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}'
上述例子中,must_not表示查詢列表中沒有為真的(也就是全為假)時則認為匹配。
我們可以組合must、should、must_not來實現更加復雜的多級邏輯查詢。
下面這個例子返回年齡大於40歲、不居住在ID的所有數據:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}'
16.過濾filter(查詢條件設置)
下面這個例子使用了布爾查詢返回balance在20000到30000之間的所有數據。
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}'
17 聚合 Aggregations
下面這個例子: 將所有的數據按照state分組(group),然后按照分組記錄數從大到小排序,返回前十條(默認):
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state"
}
}
}
}'