背景知識(shí)
在Es中,字段的類型很關(guān)鍵:
在索引的時(shí)候,如果字段第一次出現(xiàn),會(huì)自動(dòng)識(shí)別某個(gè)類型,這種規(guī)則之前已經(jīng)講過了。
那么如果一個(gè)字段已經(jīng)存在了,并且設(shè)置為某個(gè)類型。再來一條數(shù)據(jù),字段的數(shù)據(jù)不與當(dāng)前的類型相符,就會(huì)出現(xiàn)字段沖突的問題。如果發(fā)生了沖突,在2.x版本會(huì)自動(dòng)拒絕。
如果自動(dòng)映射無法滿足需求,就需要使用者自己來設(shè)置映射類型,因此,就需要使用者了解ES中的類型。
下面就步入正題吧!
字段中的索引和存儲(chǔ)
其中需要說明的是:
index定義字段的分析類型以及檢索方式
如果是no,則無法通過檢索查詢到該字段;
如果設(shè)置為not_analyzed則會(huì)將整個(gè)字段存儲(chǔ)為關(guān)鍵詞,常用于漢字短語、郵箱等復(fù)雜的字符串;
如果設(shè)置為analyzed則將會(huì)通過默認(rèn)的standard分析器進(jìn)行分析,詳細(xì)的分析規(guī)則參考這里
store定義了字段是否存儲(chǔ)
在《ES IN ACTION》中有這樣一段描述:
This might be usefulwhenyou ask Elasticsearchfora particular field because retrieving asinglestored field will be faster than retrieving the entire _sourceandextracting that fieldfromit, especiallywhenyou have large documents.NOTEWhenyou store individual fieldsaswell, you shouldtakeintoaccount that the more you store, the bigger your index gets. Usually bigger indices imply slower indexingandslower searching.
意思是,在ES中原始的文本會(huì)存儲(chǔ)在_source里面(除非你關(guān)閉了它)。默認(rèn)情況下其他提取出來的字段都不是獨(dú)立存儲(chǔ)的,是從_source里面提取出來的。當(dāng)然你也可以獨(dú)立的存儲(chǔ)某個(gè)字段,只要設(shè)置store:true即可。
獨(dú)立存儲(chǔ)某個(gè)字段,在頻繁使用某個(gè)特殊字段時(shí)很常用。而且獲取獨(dú)立存儲(chǔ)的字段要比從_source中解析快得多,而且額外你還需要從_source中解析出來這個(gè)字段,尤其是_source特別大的時(shí)候。
不過需要注意的是,獨(dú)立存儲(chǔ)的字段越多,那么索引就越大;索引越大,索引和檢索的過程就會(huì)越慢....
string
字符串類型,es中最常用的類型,官方文檔
比較重要的參數(shù):
index分析
analyzed(默認(rèn))
not_analyzed
no
store存儲(chǔ)
true 獨(dú)立存儲(chǔ)
false(默認(rèn))不存儲(chǔ),從_source中解析
Numeric
數(shù)值類型,注意numeric并不是一個(gè)類型,它包括多種類型,比如:long,integer,short,byte,double,float,每種的存儲(chǔ)空間都是不一樣的,一般默認(rèn)推薦integer和float。官方文檔參考
重要的參數(shù):
index分析
not_analyzed(默認(rèn)) ,設(shè)置為該值可以保證該字段能通過檢索查詢到
no
store存儲(chǔ)
true 獨(dú)立存儲(chǔ)
false(默認(rèn))不存儲(chǔ),從_source中解析
date
日期類型,該類型可以接受一些常見的日期表達(dá)方式,官方文檔參考。
重要的參數(shù):
index分析
not_analyzed(默認(rèn)) ,設(shè)置為該值可以保證該字段能通過檢索查詢到
no
store存儲(chǔ)
true 獨(dú)立存儲(chǔ)
false(默認(rèn))不存儲(chǔ),從_source中解析
format格式化
strict_date_optional_time||epoch_millis(默認(rèn))
你也可以自定義格式化內(nèi)容,比如
"date": {"type":"date","format":"yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}
IP
這個(gè)類型可以用來標(biāo)識(shí)IPV4的地址,參考官方文檔
常用參數(shù):
index分析
not_analyzed(默認(rèn)) ,設(shè)置為該值可以保證該字段能通過檢索查詢到
no
store存儲(chǔ)
true 獨(dú)立存儲(chǔ)
false(默認(rèn))不存儲(chǔ),從_source中解析
boolean
布爾類型,所有的類型都可以標(biāo)識(shí)布爾類型,參考官方文檔
False: 表示該值的有:false, "false", "off", "no", "0", "" (empty string), 0, 0.0
True: 所有非False的都是true
重要的參數(shù):
index分析
not_analyzed(默認(rèn)) ,設(shè)置為該值可以保證該字段能通過檢索查詢到
no
store存儲(chǔ)
true 獨(dú)立存儲(chǔ)
false(默認(rèn))不存儲(chǔ),從_source中解析
例子
{
? ? "mappings": {
? ? ? ? "operate": {
? ? ? ? ? ? "dynamic": "false",
? ? ? ? ? ? "properties": {
? ? ? ? ? ? ? ? "time": {
? ? ? ? ? ? ? ? ? ? "index": "not_analyzed",
? ? ? ? ? ? ? ? ? ? "type": "date",
? ? ? ? ? ? ? ? ? ? "format": "yyyy-MM-dd HH:mm:ss"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? "dnum": {
? ? ? ? ? ? ? ? ? ? "index": "not_analyzed",
? ? ? ? ? ? ? ? ? ? "type": "string"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? "didtoken": {
? ? ? ? ? ? ? ? ? ? "index": "not_analyzed",
? ? ? ? ? ? ? ? ? ? "type": "string"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? "devmodel": {
? ? ? ? ? ? ? ? ? ? "index": "not_analyzed",
? ? ? ? ? ? ? ? ? ? "type": "string"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? "huanid": {
? ? ? ? ? ? ? ? ? ? "index": "not_analyzed",
? ? ? ? ? ? ? ? ? ? "type": "string"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? "operate": {
? ? ? ? ? ? ? ? ? ? "index": "not_analyzed",
? ? ? ? ? ? ? ? ? ? "type": "string"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? "result": {
? ? ? ? ? ? ? ? ? ? "index": "not_analyzed",
? ? ? ? ? ? ? ? ? ? "type": "string"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? "appname": {
? ? ? ? ? ? ? ? ? ? "index": "not_analyzed",
? ? ? ? ? ? ? ? ? ? "type": "string"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? "pkgname": {
? ? ? ? ? ? ? ? ? ? "index": "not_analyzed",
? ? ? ? ? ? ? ? ? ? "type": "string"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? "vername": {
? ? ? ? ? ? ? ? ? ? "index": "not_analyzed",
? ? ? ? ? ? ? ? ? ? "type": "string"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? "vercode": {
? ? ? ? ? ? ? ? ? ? "index": "not_analyzed",
? ? ? ? ? ? ? ? ? ? "type": "string"
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? "token": {
? ? ? ? ? ? ? ? ? ? "index": "not_analyzed",
? ? ? ? ? ? ? ? ? ? "type": "string"
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
}