binary類型
binary類型存儲字符串base64處理之後的值
date類型
json格式沒有date類型,ES中表示date類型的有以下三種:
1)、字符串格式,如’2020-01-01’或’2020/05/26 09:45:00’;
2)、long類型數值,表示從毫秒開始的計數;
3)、integer類型數值,表示從秒開始的計數;
內部date會轉換成UTC時間並以毫秒錶示其值;
日期查詢會在內部轉換爲long類型形式的範圍查詢,並且聚合和存儲字段的結果將轉換爲字符串(具體取決於該字段的日期轉換格式);
日期將始終以字符串形式呈現,即使一開始在JSON文檔中提供的類型爲long;
//date類型字段定義
PUT /date_index_demo
{
"mappings": {
"properties": {
"date":{
"type": "date"
}
}
}
}
PUT /date_index_demo/_doc/1
{
"date":"2020-01-01"
}
PUT /date_index_demo/_doc/2
{
"date":"2020-01-01T12:00:00Z"
}
PUT /date_index_demo/_doc/3
{
"date":"2020-01-01T12:00:00"
}
PUT /date_index_demo/_doc/4
{
"date":1420070400001
}
//排序查詢,內部會轉爲long類型進行處理
GET /date_index_demo/_search
{
"sort": [
{
"date": {
"order": "asc"
}
}
]
}
日期的多種格式化形式
可以使用||分隔符指定多種日期格式,es會依次嘗試每種格式,直到找到匹配的格式,將日期格式轉換成字符串會優先使用第一種格式轉化;
PUT /date_index_demo
{
"mappings": {
"properties": {
"date":{
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
}
}
}
}
join類型
存在的約束:
1)、一個索引當中僅允許一個join類型的映射;
2)、父文檔與子文檔一定要在同一個分片之上,這就意味着在做get/delete/update操作的時候需要指定相同的routing值;
3)、一個元素可以有多個子級,只允許有一個父級;
4)、允許在join類型的字段上添加一個新的關係;
5)、允許在一個元素中添加一個子級(要求元素需要存在父級);
使用父級join查詢
父級join查詢及聚合操作
參考has_child和has_parent查詢,children聚合及inner hits;
join標識字段可以使用聚合和腳本查詢,也可以使用parent_id查詢;
同一父級對應多個子級
keyword類型
針對類似於ID、郵箱、域名、狀態、郵政編碼和標籤之類的表示字段,在查詢操作時以精確值作爲條件,同時可作爲排序和聚合操作;
如果針對文本內容做全文索引,使用text字段表示更合適;
數值數據不一定需要用數值類型來表示,若不用於range查詢則可以使用keyword代替,因爲keyword類型字段針對term或term-level的查詢更加友好;
考慮將字段設置成keyword類型可考慮以下因素:
1)、針對該字段有沒有range操作的需求;
2)、查詢效率上的考慮,因爲字段類型爲keyword相比數值類型效率更高;
nested類型
nested類型是object類型的一種特殊形式,其允許對象數組被索引且可以獨立進行查詢;
es沒有內部對象的概念,因此其將對象繼承結構轉成簡單的key-value的list結構;
PUT /nested_type_index/_doc/1
{
"group": "fans",
"user": [
{
"first": "John",
"last": "Smith"
},
{
"first": "Alice",
"last": "White"
}
]
}
自動映射會自動將user添加爲object類型,內部會將文檔轉成以下格式:
{
"group" : "fans",
"user.first" : [ "alice", "john" ],
"user.last" : [ "smith", "white" ]
}
這種情況下會將user.first和user.last轉化成多值字段,這樣會導致alice與white之間的關聯關係丟失,從而造成查詢時結果的不準確:
GET /nested_type_index/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"user.first": "Alice"
}
},
{
"match": {
"user.last": "Smith"
}
}
]
}
}
}
使用nested字段表示對象數組
可以表示對象數組同時能夠保證數組中的對象相互獨立,內部將數組中對象看做不同的doc,這就表示可以針對這些對象可使用nested query進行獨立查詢;
PUT /nested_type_index_1
{
"mappings": {
"properties": {
"user":{
"type": "nested"
}
}
}
}
PUT /nested_type_index_1/_doc/1
{
"group": "fans",
"user": [
{
"first": "John",
"last": "Smith"
},
{
"first": "Alice",
"last": "White"
}
]
}
//查看映射字段類型
GET /nested_type_index_1/_mapping
//
GET /nested_type_index_1/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"user.first": "Alice"
}
},
{
"match": {
"user.last": "Smith"
}
}
]
}
}
}
GET /nested_type_index_1/_search
{
"query": {
"nested": {
"path": "user",
"query": {
"bool": {
"must": [
{
"match": {
"user.first": "Alice"
}
},
{
"match": {
"user.last": "White"
}
}
]
}
},
"inner_hits": {
"highlight": {
"fields": {
"user.first": {}
}
}
}
}
}
}
文檔字段類型爲nested的可行操作:
1)、使用nested查詢;
2)、使用nested和reverse_nested聚合查詢;
3)、使用nested sorting進行排序;
4)、使用nested inner hits進行檢索或高亮匹配;
nested類型映射及對象的限制
因爲nested對象在Lucene中是以單獨的文檔進行索引的,故而若一個索引結構中存在nested類型字段且索引時nested類型字段對應有100個,這時Lucene會創建101個doc;因爲nested類型關聯代價比較大,es提供以下配置解決性能問題:
序號 | 參數 | 說明 |
---|---|---|
1 | index.mapping.nested_fields.limit | 限制nested類型字段數量 |
2 | index.mapping.nested_objects.limit | 限制nested類型字段可包含的對象數量 |
numeric類型
序號 | 類型 | 範圍 |
---|---|---|
1 | long | -2^63 ~ 2^63-1 |
2 | integer | -2^31 ~ 2^31-1 |
3 | short | -32768 ~ 32767 |
4 | byte | -128 ~ 127 |
5 | double | 64位精度 |
6 | float | 32位精度 |
7 | half_float | 16位精度 |
8 | scaled_float | 可配置scaling_factor參數 |
PUT numeric_type_index
{
"mappings": {
"properties": {
"number_of_bytes": {
"type": "integer"
},
"time_in_seconds": {
"type": "float"
},
"price": {
"type": "scaled_float",
"scaling_factor": 100
}
}
}
}
range類型
range類型細分有:integer_range、float_rangellong_range、double_range、date_range、ip_range
//索引定義
PUT range_type_index
{
"mappings": {
"properties": {
"expected_attendees":{
"type": "integer_range"
},
"time_frame":{
"type": "date_range",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
PUT range_type_index/_doc/1
{
"expected_attendees":{
"gte":10,
"lte":20
},
"time_frame":{
"gte":"2020-05-28 12:00:00",
"lte":"2020-07-01"
}
}
GET /range_type_index/_search
{
"query": {
"term": {
"expected_attendees": {
"value": 20
}
}
}
}
GET /range_type_index/_search
{
"query": {
"range": {
"time_frame": {
"gte": "2020-05-29",
"lte": "2020-06-01",
"relation":"contains"
}
}
}
}
text類型
適用於全文索引,text類型的字段會首先被分詞器解析成一個個詞元(term),然後才被索引;若一個字段既想作爲text類型又想作爲keyword類型,可以使用多字段映射進行處理;
token count類型
PUT token_count_type_index
{
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"length": {
"type": "token_count",
"analyzer": "standard"
}
}
}
}
}
}
PUT token_count_type_index/_doc/1
{
"name":"John Smith"
}
PUT token_count_type_index/_doc/2
{
"name":"Rachel Alice Williams"
}
GET token_count_type_index/_search
{
"query": {
"term": {
"name.length": {
"value": 3
}
}
}
}