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"
}
}
}
}
}
1597

被折叠的 条评论
为什么被折叠?



