松哥原创的 Spring Boot 视频教程已经杀青,感兴趣的小伙伴戳这里-->Spring Boot+Vue+微人事视频教程
hello 各位小伙伴,昨天和大家分享了 Es 中数据类型的前两种:核心类型和复合类型,今天我们来继续学习剩下的两种:地理类型和特殊类型。
这个周末圆满了!
本文是ElasticSearch 系列第十三篇,和大家聊一聊索引的基本操作,前十二篇传送门:
地理类型:
特殊类型:
如果大家觉得视频风格还能接受,也可以看看松哥的付费视频:Spring Boot+Vue+微人事视频教程
以下是视频笔记:
注意,笔记只是视频内容的一个简要记录,因此笔记内容比较简单,完整的内容可以查看视频。
使用场景:
geo_point 就是一个坐标点,定义方式如下:
PUT people
{
"mappings": {
"properties": {
"location":{
"type": "geo_point"
}
}
}
}
创建时指定字段类型,存储的时候,有四种方式:
PUT people/_doc/1
{
"location":{
"lat": 34.27,
"lon": 108.94
}
}
PUT people/_doc/2
{
"location":"34.27,108.94"
}
PUT people/_doc/3
{
"location":"uzbrgzfxuzup"
}
PUT people/_doc/4
{
"location":[108.94,34.27]
}
注意,使用数组描述,先经度后纬度。
地址位置转 geo_hash:http://www.csxgame.top/#/
指定 geo_shape 类型:
PUT people
{
"mappings": {
"properties": {
"location":{
"type": "geo_shape"
}
}
}
}
添加文档时需要指定具体的类型:
PUT people/_doc/1
{
"location":{
"type":"point",
"coordinates": [108.94,34.27]
}
}
如果是 linestring,如下:
PUT people/_doc/2
{
"location":{
"type":"linestring",
"coordinates": [[108.94,34.27],[100,33]]
}
}
存储 IP 地址,类型是 ip:
PUT blog
{
"mappings": {
"properties": {
"address":{
"type": "ip"
}
}
}
}
添加文档:
PUT blog/_doc/1
{
"address":"192.168.91.1"
}
搜索文档:
GET blog/_search
{
"query": {
"term": {
"address": "192.168.0.0/16"
}
}
}
用于统计字符串分词后的词项个数。
PUT blog
{
"mappings": {
"properties": {
"title":{
"type": "text",
"fields": {
"length":{
"type":"token_count",
"analyzer":"standard"
}
}
}
}
}
}
相当于新增了 title.length 字段用来统计分词后词项的个数。
添加文档:
PUT blog/_doc/1
{
"title":"zhang san"
}
可以通过 token_count 去查询:
GET blog/_search
{
"query": {
"term": {
"title.length": 2
}
}
}