Elasticsearch 常见的字段类型
数据类型
- 核心数据类型
- 复杂数据类型
- 专用数据类型
核心数据类型
- 字符串
text
用于全文索引,该类型的字段将通过分词器进行分词
keyword
不分词,只能搜索该字段的完整的值
- 数值型
long, integer, short, byte, double, float, half_float, scaled_float
- 布尔 - boolean
- 二进制 - binary
该类型的字段把值当做经过 base64 编码的字符串,默认不存储,且不可搜索
- 范围类型
范围类型表示值是一个范围,而不是一个具体的值
integer_range, float_range, long_range, double_range, date_range
譬如 age 的类型是 integer_range,那么值可以是 {"gte" : 20, "lte" : 40};搜索 "term" :{"age": 21} 可以搜索该值
- 日期 - date
由于Json没有date类型,所以es通过识别字符串是否符合format定义的格式来判断是否为date类型
format默认为:strict_date_optional_time||epoch_millis
格式:"2022-01-01" "2022/01/01 12:10:30" 这种字符串格式
从开始纪元(1970年年1月1日0点) 开始的毫秒数(从开始纪元开始的秒数)
PUT localhost:9200/nba/_mapping
{
"properties": {
"name": {
"type": "text"
},
"team_name": {
"type": "text"
},
"position": {
"type": "text"
},
"play_year": {
"type": "long"
},
"jerse_no": {
"type": "keyword"
},
"title": {
"type": "text"
},
"date": {
"type": "date"
}
}
}
POST localhost:9200/nba/_doc/4
{
"name": "蔡x坤",
"team_name": "勇士",
"position": "得分后卫",
"play_year": 10,
"jerse_no": "31",
"title": "打球最帅的明星",
"date": "2020-01-01"
}
POST localhost:9200/nba/_doc/5
{
"name": "杨超越",
"team_name": "美少女",
"position": "得分后卫",
"play_year": 10,
"jerse_no": "32",
"title": "打球最可爱的明星",
"date": 1610350870
}
POST localhost:9200/nba/_doc/6
{
"name": "吴亦凡",
"team_name": "湖人",
"position": "得分后卫",
"play_year": 10,
"jerse_no": "33",
"title": "最会说唱的明星",
"date": 1641886870000
}
复杂数据类型
- 数组类型 Array
ES中没有专门的数组类型, 直接使用[]定义即可,数组中所有的值必须是同一种数据类型, 不支持混合数据类型的数组:
字符串串数组 [ "one", "two" ]
整数数组 [ 1, 2 ]
Object对象数组 [ { "name": "Louis", "age": 18 }, { "name": "Daniel", "age": 17 }]
同一个数组只能存同类型的数据,不能混存,譬如 [ 10, "some string" ] 是错误的
- 对象类型 Object
对象类型可能有内部对象
POST localhost:9200/nba/_doc/8
{
"name": "吴亦凡",
"team_name": "湖人",
"position": "得分后卫",
"play_year": 10,
"jerse_no": "33",
"title": "最会说唱的明星",
"date": "1641886870",
"array": [
"one",
"two"
],
"address": {
"region": "China",
"location": {
"province": "GuangDong",
"city": "GuangZhou"
}
}
}
- 索引方式
"address.region": "China",
"address.location.province": "GuangDong",
"address.location.city": "GuangZhou"
POST localhost:9200/nba/_search
{
"query": {
"match": {
"address.region": "china"
}
}
}
{
"took": 231,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.2876821,
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "8",
"_score": 0.2876821,
"_source": {
"name": "吴亦凡",
"team_name": "湖人",
"position": "得分后卫",
"play_year": 10,
"jerse_no": "33",
"title": "最会说唱的明星",
"date": "1641886870",
"array": [
"one",
"two"
],
"address": {
"region": "China",
"location": {
"province": "GuangDong",
"city": "GuangZhou"
}
}
}
}
]
}
}
专用数据类型
- IP类型
IP类型的字段用于存储IPv4或IPv6的地址, 本质上是一个长整型字段
POST localhost:9200/nba/_mapping
{
"properties": {
"name": {
"type": "text"
},
"team_name": {
"type": "text"
},
"position": {
"type": "text"
},
"play_year": {
"type": "long"
},
"jerse_no": {
"type": "keyword"
},
"title": {
"type": "text"
},
"date": {
"type": "date"
},
"ip_addr": {
"type": "ip"
}
}
}
PUT localhost:9200/nba/_doc/9
{
"name": "吴亦凡",
"team_name": "湖人",
"position": "得分后卫",
"play_year": 10,
"jerse_no": "33",
"title": "最会说唱的明星",
"ip_addr": "192.168.1.1"
}
POST localhost:9200/nba/_search
{
"query": {
"term": {
"ip_addr": "192.168.0.0/16"
}
}
}
- 官网文档