ElasticSearch_(7)常见的字段类型

核心数据类型

字符串

text

用于全文索引,该类型的字段将通过分词器进行分词

keyword

不分词,只能搜索该字段的完整的值

数值型

long, integer, short, byte, double, float, half_float, scaled_float

布尔- boolean

二进制 -binary

该类型的字段把值当做经过 base64 编码的字符串,默认不存储,且不可搜索

范围类型

  1. 范围类型表示值是一个范围,而不是一个具体的值
  2. integer_range, float_range, long_range, double_range, date_range
  3. 譬如 age 的类型是 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
					}
				}
			}
		]
	}
}

日期 -date

  1. 由于 Json 没有date 类型,所以 es 通过识别字符串是否符合format定义的格式来判断是否为date类型
  2. format 默认为:strict_date_optional_time | epoch_mills
  3. 格式:
    1. “2022-01-01” “2022/01/01 12:10:30” 这种字符串格式
    2. 从开始纪元 (1970年1月1日0点) 开始的毫秒数
    3. 从开始纪元开始的秒数

修改索引映射

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

实例1: 插入时间数据

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

实例2: 插入秒数时间数据

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
}
'

实例3: 插入时间戳数据

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
}
'

复杂数据类型

数组类型 Array

  1. ES 中没有专门的数组类型,直接使用 [ ] 定义即可,数组中所有的值必须是同一种数据类型,不支持混合数据类型的数组
  2. 字符串数组 [“One”,“two”]
  3. 整数数组 [1,2]
  4. Object 对象数组 [{“name”:“Louis”,“age”:18}, {“name”:“Daniel”,“age”:17}]
  5. 同一个数组只能存同类型的数据,不能混存,譬如 [10, “some string”] 是错误的
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"]
}
'

对象类型 Object

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类型

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

你可能感兴趣的:(ElasticSearch)