ES 映射-Mapping

ES 的 文档字段 也是有类型存在的

不同类型的索引方式不同

{
    "broker": {  // 索引
        "mappings": { // 指定映射
            "properties": { // 字段
                "name": { 
                    "type": "text", // 类型
                    "fields": {   // 参数
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "birthdate": {
                    "type": "date"
                }
            }
        }
    }
}

类型

类型 数据类型 描述
text 字符串 会分词的文本,(通常以人类容易识别的语言书写,因为有语言处理机制,阿拉伯语、亚美尼亚语、巴斯克语、巴西语、保加利亚语、加泰罗尼亚语、中文······),例如一个推文的内容或一封邮件的内容。分析器 负责分析数据,建立倒排索引
keyword 字符串 不分词的文本,区分小大写,完全匹配
long 整数 -
integer 整数 -
short 整数 -
byte 整数 -
float 浮点数 -
double 浮点数 -
scaled_float 浮点数 用来存钱的,有固定小数位(指定 scaling_factor 参数为100,保留两位小数,这个参数本质的设定是为了节省存储空间)
boolean 布尔型 -
date 日期 -
Array 数组 根据存储的数据设置类型
binary 二进制 数据不能包含\n,不参与搜索
nested 嵌套 可做子查询的object
object object 直接指定 properties 来描述内部属性
join ??? 在同一索引下建立 父子关系 parent/child,比较复杂,可能需要聚合 和高级查询知识,和分析使用场景。
Completion 快速搜索建议 准确说它不是一个类型

分词解析
API:GET /_analyze

{
  "analyzer": "standard",
  "text": "35f142de-2931-431d-b939-111111111111"
}

参数

参数 名称 描述
analyzer 分析器 语言分析分词,一般采用默认 standard ,如果需要分析英文 使用 english
normalizer 指定 keyword 的一些查询逻辑,比如查询是否区分大小写
boost 在评分性查询中,增加字段的评分比重
coerce 数字强制 一个bool设置,默认为true,把传入的"5"自动转化成5,false 请求将被拒绝
copy_to 复制字段值 把某一字段中的值 复制到 其他一或多个字段中去,只能用来查询,不能用来显示
doc_values 关闭分词索引 默认true,false后 该字段不再建立分词索引,节省磁盘空间(最好用在排序和聚合字段)
dynamic 是否开启自动映射 默认为true
enablededit 禁用子对象解析映射
format 日期格式化
ignore_above 字符串最大长度 大于该长度的字符串将被忽略
ignore_malformed 忽略数据类型异常 默认false
index 是否为该字段 做查询索引 默认true
index_options 指定倒排索引模式
index_prefixes 指定数据前缀关键词,加速 term 查询
fields 把一个字段 处理成 多个字段 用来做不同的查询或聚合
null_value 处理null值 将null处理成想要的数据形式,方便查询或显示(处理前后 数据类型要一致)
properties 映射指定子字段
search_analyzer 指定查询时候的语言分析器

dynamic

为false 手动添加映射后,是否影响查询?

true 新检测到的字段将添加到映射中。(默认)
false 新检测到的字段将被忽略。这些字段不会被编入索引,因此无法搜索,但仍会出现在_source返回的匹配字段中。这些字段不会添加到映射中,必须显式添加新字段。
strict 如果检测到新字段,则抛出异常并拒绝该文档。必须将新字段显式添加到映射中。

数据模型

type Broker struct {
    Name             string    `json:"name"`
    Phone            string    `json:"phone"`
    Birthdate        time.Time `json:"birthdate"`        // 出生日期
    Age              int       `json:"age"`              // 年龄
    Weight           float32   `json:"weight"`           // 身高
    Motto            string    `json:"motto"`            // 个性签名
    Income           float64   `json:"income"`           // 收入
    IsAuthentication bool      `json:"isAuthentication"` // 是否认证
    City             string    `json:"city"`             // 城市
    CompanyId        uuid.UUID `json:"companyId"`        // 公司Id
    Company          string    `json:"company"`          // 公司
    Interests        []string  `json:"interests"`        // 兴趣爱好
    Resumes          []Resume  `json:"resumes"`          // 入职履历
    Logins           []Login   `json:"logins"`           // 入职履历
    Created          time.Time `json:"created"`          // 创建时间
}

创建索引指定的映射

{
    "settings": {
        "number_of_shards": 2,
        "number_of_replicas": 1
    },
    "mappings": {
        "properties": {
            "name": {
                "type": "keyword"
            },
            "phone": {
                "type": "keyword"
            },
            "birthdate": {
                "type": "date",
                "format": "yyyy-MM-dd"
            },
            "age": {
                "type": "long"
            },
            "weight": {
                "type": "float"
            },
            "motto": {
                "type": "text"
            },
            "income": {
                "type": "scaled_float",
                "scaling_factor": 100
            },
            "isAuthentication": {
                "type": "boolean"
            },
            "city": {
                "type": "keyword"
            },
            "companyId": {
                "type": "keyword"
            },
            "company": {
                "type": "keyword"
            },
            "interests": {
                "type": "text"
            },
            "resumes": {
                "properties": {
                    "city": {
                        "type": "keyword"
                    },
                    "companyId": {
                        "type": "keyword"
                    },
                    "company": {
                        "type": "keyword"
                    }
                }
            },
            "logins": {
                "type": "nested",
                "properties": {
                    "time": {
                        "type": "date"
                    },
                    "port": {
                        "type": "keyword"
                    }
                }
            },
            "created": {
                "type": "date"
            }
        }
    }
}

动态映射的映射结果

{
    "broker": {
        "mappings": {
            "properties": {
                "age": {
                    "type": "long"
                },
                "birthdate": {
                    "type": "date"
                },
                "city": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "company": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "companyId": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "created": {
                    "type": "date"
                },
                "income": {
                    "type": "float"
                },
                "interests": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "isAuthentication": {
                    "type": "boolean"
                },
                "logins": {
                    "properties": {
                        "port": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "time": {
                            "type": "date"
                        }
                    }
                },
                "motto": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "phone": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "resumes": {
                    "properties": {
                        "city": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "company": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "companyId": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                },
                "weight": {
                    "type": "float"
                }
            }
        }
    }
}

指定映射的结果

{
    "broker": {
        "mappings": {
            "properties": {
                "age": {
                    "type": "long"
                },
                "birthdate": {
                    "type": "date",
                    "format": "yyyy-MM-dd"
                },
                "city": {
                    "type": "keyword"
                },
                "company": {
                    "type": "keyword"
                },
                "companyId": {
                    "type": "keyword"
                },
                "created": {
                    "type": "date"
                },
                "income": {
                    "type": "scaled_float",
                    "scaling_factor": 100
                },
                "interests": {
                    "type": "text"
                },
                "isAuthentication": {
                    "type": "boolean"
                },
                "logins": {
                    "type": "nested",
                    "properties": {
                        "port": {
                            "type": "keyword"
                        },
                        "time": {
                            "type": "date"
                        }
                    }
                },
                "motto": {
                    "type": "text"
                },
                "name": {
                    "type": "keyword"
                },
                "phone": {
                    "type": "keyword"
                },
                "resumes": {
                    "properties": {
                        "city": {
                            "type": "keyword"
                        },
                        "company": {
                            "type": "keyword"
                        },
                        "companyId": {
                            "type": "keyword"
                        }
                    }
                },
                "weight": {
                    "type": "float"
                }
            }
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容