Elasticsearch入門必備——ES中的字段類型以及常用屬性

背景知識(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"}

更多的時(shí)間表達(dá)式可以參考這里

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"

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,892評(píng)論 0 13
  • 第3章 映射 映射是定義存儲(chǔ)和索引的文檔類型以及字段的過程。索引中的每一個(gè)文檔都有一個(gè)類型,每種類型都有它自己的映...
    MR_ChanHwang閱讀 2,365評(píng)論 0 1
  • 大茶網(wǎng)閱讀 189評(píng)論 0 0
  • 春雨悠然,瀝瀝心田。 靜聽窗臺(tái),三兩滴答假成線。 這夜乘雨正好眠。 熄聲平臥,又憶嶺南。 花開鳥語,溫濕回南衣未干...
    A1ex馬杰閱讀 219評(píng)論 0 0

友情鏈接更多精彩內(nèi)容