核心數據類型
字符串
text
用於全文索引,該類型的字段將通過分詞器進行分詞
keyword
不分詞,只能搜索該字段的完整的值
數值型
long, integer, short, byte, double, float, half_float, scaled_float
布爾- boolean
二進制 -binary
該類型的字段把值當做經過 base64 編碼的字符串,默認不存儲,且不可搜索
範圍類型
- 範圍類型表示值是一個範圍,而不是一個具體的值
- integer_range, float_range, long_range, double_range, date_range
- 譬如 age 的類型是
integer_range
, 那麼值可以是 {“gte”:20, “lte”: 40}:搜索 “term” {“age”:21} 可以搜索該值
實例:
刪除已經存在的索引 curl -X DELETE "localhost:9200/nba"
創建索引
curl -X PUT "localhost:9200/nba" -H 'Content-Type:application/json' -d '
{
"mappings":{
"properties":{
"jerse_no":{
"type":"keyword"
},
"name":{
"type":"text"
},
"play_year":{
"type":"long"
},
"position":{
"type":"text"
},
"team_name":{
"type":"text"
},
"age_range":{
"type":"integer_range"
}
}
}
}
'
添加數據
curl -X PUT "localhost:9200/nba/_doc/1" -H 'Content-Type:application/json' -d '
{
"name":"哈登",
"team_name":"湖人",
"position":"得分後衛",
"play_year":10,
"jerse_no":"33",
"age_range":{
"gte":20,
"lte":40
}
}
'
所有 age_range 滿足在20到40之間的球員
curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"term":{
"age_range":21
}
}
}
'
返回一個 json
{
"took": 1035,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "哈登",
"team_name": "湖人",
"position": "得分後衛",
"play_year": 10,
"jerse_no": "33",
"age_range": {
"gte": 20,
"lte": 40
}
}
}
]
}
}
日期 -date
- 由於 Json 沒有date 類型,所以 es 通過識別字符串是否符合format定義的格式來判斷是否爲date類型
- format 默認爲:strict_date_optional_time | epoch_mills
- 格式:
- “2022-01-01” “2022/01/01 12:10:30” 這種字符串格式
- 從開始紀元 (1970年1月1日0點) 開始的毫秒數
- 從開始紀元開始的秒數
修改索引映射
curl -X PUT "localhost:9200/nba/_mapping" -H 'Content-Type:application/json' -d '
{
"properties":{
"jerse_no":{
"type":"keyword"
},
"name":{
"type":"text"
},
"play_year":{
"type":"long"
},
"position":{
"type":"text"
},
"team_name":{
"type":"text"
},
"age_range":{
"type":"integer_range"
},
"title":{
"type":"text"
},
"date":{
"type":"date"
}
}
}
‘
實例1: 插入時間數據
curl -X PUT "localhost:9200/nba/_doc/2" -H 'Content-Type:application/json' -d '
{
"name":"豬八戒",
"team_name":"勇士",
"position":"得分後衛",
"play_year":10,
"jerse_no":"31",
"title":"打球最帥的明星",
"date":"2020-01-01"
}
'
實例2: 插入秒數時間數據
curl -X PUT "localhost:9200/nba/_doc/3" -H 'Content-Type:application/json' -d '
{
"name":"沙和尚",
"team_name":"取經小分隊",
"position":"得分後衛",
"play_year":10,
"jerse_no":"32",
"title":"打球最可愛的明星",
"date":1610350870
}
'
實例3: 插入時間戳數據
curl -X PUT "localhost:9200/nba/_doc/4" -H 'Content-Type:application/json' -d '
{
"name":"唐僧",
"team_name":"取經小分隊",
"position":"得分後衛",
"play_year":10,
"jerse_no":"33",
"title":"最會唱歌的明星",
"date":1641886870000
}
'
複雜數據類型
數組類型 Array
- ES 中沒有專門的數組類型,直接使用 [ ] 定義即可,數組中所有的值必須是同一種數據類型,不支持混合數據類型的數組
- 字符串數組 [“One”,“two”]
- 整數數組 [1,2]
- Object 對象數組 [{“name”:“Louis”,“age”:18}, {“name”:“Daniel”,“age”:17}]
- 同一個數組只能存同類型的數據,不能混存,譬如 [10, “some string”] 是錯誤的
curl -X PUT "localhost:9200/nba/_doc/5" -H 'Content-Type:application/json' -d '
{
"name":"唐僧",
"team_name":"取經小分隊",
"position":"得分後衛",
"play_year":10,
"jerse_no":"33",
"title":"最會唱歌的明星",
"date":1641886870000,
"array_test":["1","2"]
}
'
對象類型 Object
curl -X PUT "localhost:9200/nba/_doc/8" -H 'Content-Type:application/json' -d '
{
"name":"唐僧",
"team_name":"取經小分隊",
"position":"得分後衛",
"play_year":10,
"jerse_no":"33",
"title":"最會唱歌的明星",
"date":1641886870000,
"array_test":["1","2"],
"address":{
"region":"China",
"location":{
"province":"Guangdong",
"city":"GuangZhou"
}
}
}
'
查詢數據
curl -X PUT "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"match":{
"address.region":"china"
}
}
}
'
專用數據類型
IP類型
IP類型用於存儲IPV4 或 IPV6 的地址,本質上是一個長整型字段
對已有的索引進行修改
curl -X POST "localhost:9200/nba/_mapping" -H 'Content-Type:application/json' -d '
{
"properties":{
"jerse_no":{
"type":"keyword"
},
"name":{
"type":"text"
},
"play_year":{
"type":"long"
},
"position":{
"type":"text"
},
"team_name":{
"type":"text"
},
"age_range":{
"type":"integer_range"
},
"title":{
"type":"text"
},
"date":{
"type":"date"
},
"ip_addr":{
"type":"ip"
}
}
}
‘
插入文檔
curl -X PUT "localhost:9200/nba/_doc/9" -H 'Content-Type:application/json' -d '
{
"name":"豬八戒",
"team_name":"勇士",
"position":"得分後衛",
"play_year":10,
"jerse_no":"31",
"title":"打球最帥的明星",
"ip_addr":"192.168.1.1"
}
'
查詢 ip地址 在 192.168.0.0 ~ 192.168.255.255 之間的數據
curl -X PUT "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"term":{
"ip_addr":"192.168.0.0/16"
}
}
}
'