ELK stack

Elasticsearch入门必备——ES中的字段类型以及常用属

2018-08-09  本文已影响59人  为技术疯狂

背景知识

在Es中,字段的类型很关键:

在索引的时候,如果字段第一次出现,会自动识别某个类型,这种规则之前已经讲过了。

那么如果一个字段已经存在了,并且设置为某个类型。再来一条数据,字段的数据不与当前的类型相符,就会出现字段冲突的问题。如果发生了冲突,在2.x版本会自动拒绝。

如果自动映射无法满足需求,就需要使用者自己来设置映射类型,因此,就需要使用者了解ES中的类型。

下面就步入正题吧!

字段中的索引和存储

其中需要说明的是:

index定义字段的分析类型以及检索方式

如果是no,则无法通过检索查询到该字段;

如果设置为not_analyzed则会将整个字段存储为关键词,常用于汉字短语、邮箱等复杂的字符串;

如果设置为analyzed则将会通过默认的standard分析器进行分析,详细的分析规则参考这里

store定义了字段是否存储

在《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中原始的文本会存储在_source里面(除非你关闭了它)。默认情况下其他提取出来的字段都不是独立存储的,是从_source里面提取出来的。当然你也可以独立的存储某个字段,只要设置store:true即可。

独立存储某个字段,在频繁使用某个特殊字段时很常用。而且获取独立存储的字段要比从_source中解析快得多,而且额外你还需要从_source中解析出来这个字段,尤其是_source特别大的时候。

不过需要注意的是,独立存储的字段越多,那么索引就越大;索引越大,索引和检索的过程就会越慢....

string

字符串类型,es中最常用的类型,官方文档

比较重要的参数:

index分析

analyzed(默认)

not_analyzed

no

store存储

true 独立存储

false(默认)不存储,从_source中解析

Numeric

数值类型,注意numeric并不是一个类型,它包括多种类型,比如:long,integer,short,byte,double,float,每种的存储空间都是不一样的,一般默认推荐integer和float。官方文档参考

重要的参数:

index分析

not_analyzed(默认) ,设置为该值可以保证该字段能通过检索查询到

no

store存储

true 独立存储

false(默认)不存储,从_source中解析

date

日期类型,该类型可以接受一些常见的日期表达方式,官方文档参考

重要的参数:

index分析

not_analyzed(默认) ,设置为该值可以保证该字段能通过检索查询到

no

store存储

true 独立存储

false(默认)不存储,从_source中解析

format格式化

strict_date_optional_time||epoch_millis(默认)

你也可以自定义格式化内容,比如

"date": {"type":"date","format":"yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}

更多的时间表达式可以参考这里

IP

这个类型可以用来标识IPV4的地址,参考官方文档

常用参数:

index分析

not_analyzed(默认) ,设置为该值可以保证该字段能通过检索查询到

no

store存储

true 独立存储

false(默认)不存储,从_source中解析

boolean

布尔类型,所有的类型都可以标识布尔类型,参考官方文档

False: 表示该值的有:false, "false", "off", "no", "0", "" (empty string), 0, 0.0

True: 所有非False的都是true

重要的参数:

index分析

not_analyzed(默认) ,设置为该值可以保证该字段能通过检索查询到

no

store存储

true 独立存储

false(默认)不存储,从_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"

                }

            }

        }

    }

}

上一篇下一篇

猜你喜欢

热点阅读