用于全文索引,该类型的字段将通过分词器进行分词
不分词,只能搜索该字段的完整的值
long, integer, short, byte, double, float, half_float, scaled_float
该类型的字段把值当做经过 base64 编码的字符串,默认不存储,且不可搜索
integer_range
, 那么值可以是 {“gte”:20, “lte”: 40}:搜索 “term” {“age”:21} 可以搜索该值实例:
删除已经存在的索引 curl -X DELETE "localhost:9200/nba"
创建索引
curl -X PUT "localhost:9200/nba" -H 'Content-Type:application/json' -d '
{
"mappings":{
"properties":{
"jerse_no":{
"type":"keyword"
},
"name":{
"type":"text"
},
"play_year":{
"type":"long"
},
"position":{
"type":"text"
},
"team_name":{
"type":"text"
},
"age_range":{
"type":"integer_range"
}
}
}
}
'
添加数据
curl -X PUT "localhost:9200/nba/_doc/1" -H 'Content-Type:application/json' -d '
{
"name":"哈登",
"team_name":"湖人",
"position":"得分后卫",
"play_year":10,
"jerse_no":"33",
"age_range":{
"gte":20,
"lte":40
}
}
'
所有 age_range 满足在20到40之间的球员
curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"term":{
"age_range":21
}
}
}
'
返回一个 json
{
"took": 1035,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "哈登",
"team_name": "湖人",
"position": "得分后卫",
"play_year": 10,
"jerse_no": "33",
"age_range": {
"gte": 20,
"lte": 40
}
}
}
]
}
}
修改索引映射
curl -X PUT "localhost:9200/nba/_mapping" -H 'Content-Type:application/json' -d '
{
"properties":{
"jerse_no":{
"type":"keyword"
},
"name":{
"type":"text"
},
"play_year":{
"type":"long"
},
"position":{
"type":"text"
},
"team_name":{
"type":"text"
},
"age_range":{
"type":"integer_range"
},
"title":{
"type":"text"
},
"date":{
"type":"date"
}
}
}
‘
curl -X PUT "localhost:9200/nba/_doc/2" -H 'Content-Type:application/json' -d '
{
"name":"猪八戒",
"team_name":"勇士",
"position":"得分后卫",
"play_year":10,
"jerse_no":"31",
"title":"打球最帅的明星",
"date":"2020-01-01"
}
'
curl -X PUT "localhost:9200/nba/_doc/3" -H 'Content-Type:application/json' -d '
{
"name":"沙和尚",
"team_name":"取经小分队",
"position":"得分后卫",
"play_year":10,
"jerse_no":"32",
"title":"打球最可爱的明星",
"date":1610350870
}
'
curl -X PUT "localhost:9200/nba/_doc/4" -H 'Content-Type:application/json' -d '
{
"name":"唐僧",
"team_name":"取经小分队",
"position":"得分后卫",
"play_year":10,
"jerse_no":"33",
"title":"最会唱歌的明星",
"date":1641886870000
}
'
curl -X PUT "localhost:9200/nba/_doc/5" -H 'Content-Type:application/json' -d '
{
"name":"唐僧",
"team_name":"取经小分队",
"position":"得分后卫",
"play_year":10,
"jerse_no":"33",
"title":"最会唱歌的明星",
"date":1641886870000,
"array_test":["1","2"]
}
'
curl -X PUT "localhost:9200/nba/_doc/8" -H 'Content-Type:application/json' -d '
{
"name":"唐僧",
"team_name":"取经小分队",
"position":"得分后卫",
"play_year":10,
"jerse_no":"33",
"title":"最会唱歌的明星",
"date":1641886870000,
"array_test":["1","2"],
"address":{
"region":"China",
"location":{
"province":"Guangdong",
"city":"GuangZhou"
}
}
}
'
查询数据
curl -X PUT "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"match":{
"address.region":"china"
}
}
}
'
IP类型用于存储IPV4 或 IPV6 的地址,本质上是一个长整型字段
对已有的索引进行修改
curl -X POST "localhost:9200/nba/_mapping" -H 'Content-Type:application/json' -d '
{
"properties":{
"jerse_no":{
"type":"keyword"
},
"name":{
"type":"text"
},
"play_year":{
"type":"long"
},
"position":{
"type":"text"
},
"team_name":{
"type":"text"
},
"age_range":{
"type":"integer_range"
},
"title":{
"type":"text"
},
"date":{
"type":"date"
},
"ip_addr":{
"type":"ip"
}
}
}
‘
插入文档
curl -X PUT "localhost:9200/nba/_doc/9" -H 'Content-Type:application/json' -d '
{
"name":"猪八戒",
"team_name":"勇士",
"position":"得分后卫",
"play_year":10,
"jerse_no":"31",
"title":"打球最帅的明星",
"ip_addr":"192.168.1.1"
}
'
查询 ip地址 在 192.168.0.0 ~ 192.168.255.255 之间的数据
curl -X PUT "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"term":{
"ip_addr":"192.168.0.0/16"
}
}
}
'