一:数据类型
核心数据类型
复杂数据类型
专用数据类型
二:核心数据类型
1.字符串
text:⽤于全⽂索引,该类型的字段将通过分词器进⾏分词
keyword:不分词,只能搜索该字段的完整的值
2.数值型
long, integer, short, byte, double, float, half_float, scaled_float
3.二进制
该类型的字段把值当做经过 base64 编码的字符串,默认不存储,且不可搜索
4.范围类型
范围类型表示值是⼀个范围,⽽不是⼀个具体的值 integer_range, float_range, long_range, double_range, date_range
譬如 age 的类型是 integer_range,那么值可以是 {"gte" : 20, "lte" : 40};搜索 "term" : {"age": 21} 可以搜索该值
制造数据,然后添加:
PUT /wba/ { "mappings": { "properties": { "age_range": { "type": "integer_range" } } } } PUT /wba/_doc/1 { "age_range":{ "gte":10, "lte":15 } } PUT /wba/_doc/2 { "age_range":{ "gte":15, "lte":20 } } PUT /wba/_doc/3 { "age_range":{ "gte":20, "lte":30 } }
查询:
GET /wba/_search { "query": { "term":{ "age_range":15 } } }
效果:
{ "took" : 247, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "wba", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "age_range" : { "gte" : 10, "lte" : 15 } } }, { "_index" : "wba", "_type" : "_doc", "_id" : "2", "_score" : 1.0, "_source" : { "age_range" : { "gte" : 15, "lte" : 20 } } } ] } }
5.日期类型
由于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 /cba/ { "mappings": { "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" } } } }
添加数据
PUT /cba/_doc/1 { "name": "蔡x坤", "team_name": "勇⼠", "position": "得分后卫", "play_year": 10, "jerse_no": "31", "title": "打球最帅的明星", "date": "2020-01-01" } PUT /cba/_doc/2 { "name": "杨超越", "team_name": "猴急", "position": "得分后卫", "play_year": 10, "jerse_no": "32", "title": "打球最可爱的明星", "date": 1610350870 }
直接查询:
GET /cba/_search { "query": { "term": { "date": { "value": "2020-01-01" } } } }
结果:
{ "took" : 541, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "cba", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "name" : "蔡x坤", "team_name" : "勇⼠", "position" : "得分后卫", "play_year" : 10, "jerse_no" : "31", "title" : "打球最帅的明星", "date" : "2020-01-01" } } ] } }
三:复杂数据类型
1.数组类型Array
ES中没有专⻔的数组类型, 直接使⽤[]定义即可,
数组中所有的值必须是同⼀种数据类 型, 不⽀持混合数据类型的数组:
字符串数组 [ "one", "two" ] 整数数组 [ 1, 2 ] Object对象数组 [ { "name": "Louis", "age": 18 }, { "name": "Daniel", "age": 17 }]
同⼀个数组只能存同类型的数据,不能混存,譬如 [ 10, "some string" ] 是错误的
2.对象类型Object
对象类型可能有内部对象
{ "name": "吴亦凡", "team_name": "湖⼈", "position": "得分后卫", "play_year": 10, "jerse_no": "33", "title": "最会说唱的明星", "date": "1641886870", "array": [ "one", "two" ], "address": { "region": "China", "location": { "province": "GuangDong", "city": "GuangZhou" } } }
索引方式:
{ "query": { "match": { "address.region": "china" } } }