【es】索引字段类型数值类型和日期类型

1.定义一个简单的表及结构 

curl  -u elastic:elastic  -k -XPUT http://192.168.1.1:9200/test001  -H 'Content-Type: application/json' -d  '
{
"settings":{"number_of_shards":"2","number_of_replicas":"1"},
"mappings":{"properties":{"userid":{"type":"text"}}}
}'

{"acknowledged":true,"shards_acknowledged":true,"index":"test001"}
settings:部分属于静态配置。但可以修改。
主分片的数量是静态的,创建后不能修改。副本分片的数量是动态的,可以修改。

修改副本数。
curl  -u elastic:elastic  -k -XPUT http://192.168.1.1:9200/test001/_settings  -H 'Content-Type: application/json' -d  '
{"settings":{"number_of_replicas":"2"}}'

{"acknowledged":true}

2.添加字段 

索引可以添加新的字段,无法删除旧的字段。

查看索引的结构。
curl  -u elastic:elastic  -k -XGET http://192.168.1.1:9200/test001/_settings
{
    "test001":{
        "settings":{
            "index":{
                "routing":{
                    "allocation":{
                        "include":{
                            "_tier_preference":"data_content"
                        }
                    }
                },
                "number_of_shards":"2",
                "provided_name":"test001",
                "creation_date":"1743145298881",
                "number_of_replicas":"2",
                "uuid":"6KN4zhIzRY2WRXKn9DjAnQ",
                "version":{
                    "created":"7170099"
                }
            }
        }
    }
}

curl  -u elastic:elastic  -k -XGET http://192.168.1.1:9200/test001/_mapping
{
    "test001":{
        "mappings":{
            "properties":{
                "userid":{
                    "type":"text"
                }
            }
        }
    }
}

添加key字段。
curl  -u elastic:elastic  -k -XPUT http://192.168.1.1:9200/test001/_mapping  -H 'Content-Type: application/json' -d  '
{"properties":{"key":{"type":"text"}}}'

curl  -u elastic:elastic  -k -XGET http://192.168.1.1:9200/test001/_mapping
{
    "test001":{
        "mappings":{
            "properties":{
                "key":{
                    "type":"text"
                },
                "userid":{
                    "type":"text"
                }
            }
        }
    }
}

3.es字段类型 

ES内置了20多种类型用于支持多种多样的结构化数据。
text文本类型默认会被分词。如果不指定分词器,ES会使用默认的分词器切分文本,并把切分后
的文本保存到索引中。

ES支持的数值类型:
long 有符号64位整数 -2^63~2^63-1 
integer 有符号32位整数 -2^31~2^31-1 
short 有符号16位整数 -32768~32767
byte 有符号8位整数,-128~127 
double:64位双精度浮点数。
float:32位双精度浮点数。

添加字段。
curl  -u elastic:elastic  -k -XPUT http://192.168.1.1:9200/test001/_mapping  -H 'Content-Type: application/json' -d  '
{"properties":{
"long_type":{"type":"long"},
"integer_type":{"type":"integer"},
"short_type":{"type":"short"},
"byte_type":{"type":"byte"},
"double_type":{"type":"double"},
"float_type":{"type":"float"}
}}'

--再次查看索引的结构。
curl  -u elastic:elastic  -k -XGET http://192.168.1.1:9200/test001/_mapping
{
    "test001":{
        "mappings":{
            "properties":{
                "byte_type":{
                    "type":"byte"
                },
                "double_type":{
                    "type":"double"
                },
                "float_type":{
                    "type":"float"
                },
                "integer_type":{
                    "type":"integer"
                },
                "key":{
                    "type":"text"
                },
                "long_type":{
                    "type":"long"
                },
                "short_type":{
                    "type":"short"
                },
                "userid":{
                    "type":"text"
                }
            }
        }
    }
}

4.日期字段。

默认情况下,索引中的日期为 UTC 时间格式,比北京时间晚8小时,使用时每次查询都需要格式转换,很不方便。
可以使用 format 参数自定义时间格式。
没有特别指定,索引中不带时区的日期都是指UTC时间。

curl  -u elastic:elastic  -k -XPUT http://192.168.1.1:9200/test001/_mapping  -H 'Content-Type: application/json' -d  '
{"properties":{
"date_type":{"type":"date","format":"yyyy-MM-dd HH:mm:ss || epoch_millis"}
}}'


curl  -u elastic:elastic  -k -XPUT http://192.168.1.1:9200/test001/_mapping  -H 'Content-Type: application/json' -d  '
{"properties":{
"date2_type":{"type": "date",
            "format": "yyyy-MM-dd HH:mm:ss||strict_date_optional_time||epoch_millis",
            "index": "true"}
}}'

{
    "test001":{
        "mappings":{
            "properties":{
                "byte_type":{
                    "type":"byte"
                },
                "date2_type":{
                    "type":"date",
                    "format":"yyyy-MM-dd HH:mm:ss||strict_date_optional_time||epoch_millis"
                },
                "date_type":{
                    "type":"date",
                    "format":"yyyy-MM-dd HH:mm:ss || epoch_millis"
                },
                "double_type":{
                    "type":"double"
                },
                "float_type":{
                    "type":"float"
                },
                "integer_type":{
                    "type":"integer"
                },
                "key":{
                    "type":"text"
                },
                "long_type":{
                    "type":"long"
                },
                "short_type":{
                    "type":"short"
                },
                "userid":{
                    "type":"text"
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值