字段类型:Text v.s Keyword
● Text
- ⽤于全⽂本字段,⽂本会被 Analyzer 分词
- 默认不⽀持聚合分析及排序。需要设置 fielddata 为 true
● Keyword
- ⽤于 id,枚举及不需要分词的⽂本。例如电话号码,email地址,⼿机号码,邮政编码,性别等
- 适⽤于 Filter(精确匹配),Sorting 和 Aggregations
● 设置多字段类型
- 默认会为⽂本类型设置成 text,并且设置⼀个 keyword 的⼦字段
- 在处理⼈类语⾔时,通过增加"英⽂","拼⾳"和"标准"分词器,提⾼搜索结构
字段类型 :结构化数据
● 数值类型
- 尽量选择贴近的类型。例如可以⽤ byte,就不要⽤ long
● 枚举类型
- 设置为 keyword。即便是数字,也应该设置成 keyword,获取更加好的性能
● 其他
- ⽇期 / 布尔 / 地理信息
检索
● 如不需要检索,排序和聚合分析
Enable 设置成 false
● 如不需要检索
Index 设置成 false ,无法检索,依然能聚合
● 对需要检索的字段,可以通过如下配置,设定存储粒度
Index_options / Norms :不需要归⼀化数据时,可以关闭
聚合及排序
● 如不需要检索,排序和聚合分析
Enable 设置成 false
● 如不需要排序或者聚合分析功能
Doc_values / fielddata 设置成 false
● 更新频繁,聚合查询频繁的 keyword 类型的字段
推荐将 eager_global_ordinals 设置为 true
额外的存储
● 是否需要专⻔存储当前字段数据
Store 设置成 true,可以存储该字段的原始内容
⼀般结合 _source 的 enabled 为 false 时候使⽤
● Disable _source:节约磁盘;适⽤于指标型数据
⼀般建议先考虑增加压缩⽐
⽆法看到 _source字段,⽆法做 ReIndex,⽆法做Update
Kibana 中⽆法做 discovery
避免过多字段
● ⼀个⽂档中,最好避免⼤量的字段
- 过多的字段数不容易维护
- Mapping 信息保存在 Cluster State 中,数据量过⼤,对集群性能会有影响 (Cluster State 信息需要和所有的节点同步)
- 删除或者修改数据需要 reindex
● 默认最⼤字段数是 1000,可以设置 index.mapping.total_fields.limt 限定最⼤字段数。
● 什么原因会导致⽂档中有成百上千的字段?
Dynamic v.s Strict
● Dynamic(⽣产环境中,尽量不要打开 Dynamic)
- true - 未知字段会被⾃动加⼊
- false - 新字段不会被索引。但是会保存在 _source
- strict - 新增字段不会被索引,⽂档写⼊失败
● Strict
- 可以控制到字段级别
通过 Nested 对象保存 Key/Value 的⼀些不⾜
● 可以减少字段数量,解决 Cluster State 中保存过多 Meta 信息的问题,但是
- 导致查询语句复杂度增加
- Nested 对象,不利于在 Kibana 中实现可视化分析
实现Filter 过滤,避免正则查询。
⼤⼤提升性能
避免空值引起的聚合不准
使⽤ Null_Value 解决空值的问题
为索引的 Mapping 加⼊ Meta 信息
● Mappings 设置⾮常重要,需要从两个维度进⾏考虑
- 功能:搜索,聚合,排序
- 性能:存储的开销;内存的开销;搜索的性能
● Mappings 设置是⼀个迭代的过程
- 
加⼊新的字段很容易(必要时需要 update_by_query) 
- 
更新删除字段不允许(需要 Reindex 重建数据) 
- 
最好能对 Mappings 加⼊ Meta 信息,更好的进⾏版本管理 
- 
可以考虑将 Mapping ⽂件上传 git 进⾏管理 Index 一本书的信息PUT books/_doc/1 
 {
 "title":"Mastering ElasticSearch 5.0",
 "description":"Master the searching, indexing, and aggregation features in ElasticSearch Improve users' search experience with Elasticsearch's functionalities and develop your own Elasticsearch plugins",
 "author":"Bharvi Dixit",
 "public_date":"2017",
 "cover_url":"https://images-na.ssl-images-amazon.com/images/I/51OeaMFxcML.jpg"
 }#查询自动创建的Mapping 
 GET books/_mappingDELETE books #优化字段类型 
 PUT books
 {
 "mappings" : {
 "properties" : {
 "author" : {"type" : "keyword"},
 "cover_url" : {"type" : "keyword","index": false},
 "description" : {"type" : "text"},
 "public_date" : {"type" : "date"},
 "title" : {
 "type" : "text",
 "fields" : {
 "keyword" : {
 "type" : "keyword",
 "ignore_above" : 100
 }
 }
 }
 }
 }
 }#Cover URL index 设置成false,无法对该字段进行搜索 
 POST books/_search
 {
 "query": {
 "term": {
 "cover_url": {
 "value": "https://images-na.ssl-images-amazon.com/images/I/51OeaMFxcML.jpg"
 }
 }
 }
 }#Cover URL index 设置成false,依然支持聚合分析 
 POST books/_search
 {
 "aggs": {
 "cover": {
 "terms": {
 "field": "cover_url",
 "size": 10
 }
 }
 }
 }DELETE books #新增 Content字段。数据量很大。选择将Source 关闭 
 PUT books
 {
 "mappings" : {
 "_source": {"enabled": false},
 "properties" : {
 "author" : {"type" : "keyword","store": true},
 "cover_url" : {"type" : "keyword","index": false,"store": true},
 "description" : {"type" : "text","store": true},
 "content" : {"type" : "text","store": true},
 "public_date" : {"type" : "date","store": true},
 "title" : {
 "type" : "text",
 "fields" : {
 "keyword" : {
 "type" : "keyword",
 "ignore_above" : 100
 }
 },
 "store": true
 }
 }
 }
 }Index 一本书的信息,包含ContentPUT books/_doc/1 
 {
 "title":"Mastering ElasticSearch 5.0",
 "description":"Master the searching, indexing, and aggregation features in ElasticSearch Improve users' search experience with Elasticsearch's functionalities and develop your own Elasticsearch plugins",
 "content":"The content of the book......Indexing data, aggregation, searching. something else. something in the way............",
 "author":"Bharvi Dixit",
 "public_date":"2017",
 "cover_url":"https://images-na.ssl-images-amazon.com/images/I/51OeaMFxcML.jpg"
 }#查询结果中,Source不包含数据 
 POST books/_search
 {}#搜索,通过store 字段显示数据,同时高亮显示 conent的内容 
 POST books/_search
 {
 "stored_fields": ["title","author","public_date"],
 "query": {
 "match": {
 "content": "searching"
 }
 },"highlight": { "fields": { "content":{} } }} Cookie Service##索引数据,dynamic mapping 会不断加入新增字段 
 PUT cookie_service/_doc/1
 {
 "url":"www.google.com",
 "cookies":{
 "username":"tom",
 "age":32
 }
 }PUT cookie_service/_doc/2 
 {
 "url":"www.amazon.com",
 "cookies":{
 "login":"2019-01-01",
 "email":"xyz@abc.com"
 }
 }DELETE cookie_service #使用 Nested 对象,增加key/value 
 PUT cookie_service
 {
 "mappings": {
 "properties": {
 "cookies": {
 "type": "nested",
 "properties": {
 "name": {
 "type": "keyword"
 },
 "dateValue": {
 "type": "date"
 },
 "keywordValue": {
 "type": "keyword"
 },
 "IntValue": {
 "type": "integer"
 }
 }
 },
 "url": {
 "type": "text",
 "fields": {
 "keyword": {
 "type": "keyword",
 "ignore_above": 256
 }
 }
 }
 }
 }
 }##写入数据,使用key和合适类型的value字段 
 PUT cookie_service/_doc/1
 {
 "url":"www.google.com",
 "cookies":[
 {
 "name":"username",
 "keywordValue":"tom"
 },
 {
 "name":"age",
 "intValue":32} ]} PUT cookie_service/_doc/2 
 {
 "url":"www.amazon.com",
 "cookies":[
 {
 "name":"login",
 "dateValue":"2019-01-01"
 },
 {
 "name":"email",
 "IntValue":32} ]} Nested 查询,通过bool查询进行过滤POST cookie_service/_search 
 {
 "query": {
 "nested": {
 "path": "cookies",
 "query": {
 "bool": {
 "filter": [
 {
 "term": {
 "cookies.name": "age"
 }},
 {
 "range":{
 "cookies.intValue":{
 "gte":30
 }
 }
 }
 ]
 }
 }
 }
 }
 }在Mapping中加入元信息,便于管理PUT softwares/ 
 {
 "mappings": {
 "_meta": {
 "software_version_mapping": "1.0"
 }
 }
 }GET softwares/_mapping PUT softwares/_doc/1 
 {
 "software_version":"7.1.0"
 }DELETE softwares 优化,使用inner objectPUT softwares/ 
 {
 "mappings": {
 "_meta": {
 "software_version_mapping": "1.1"
 },
 "properties": {
 "version": {
 "properties": {
 "display_name": {
 "type": "keyword"
 },
 "hot_fix": {
 "type": "byte"
 },
 "marjor": {
 "type": "byte"
 },
 "minor": {
 "type": "byte"
 }
 }
 }
 }
 }
 }#通过 Inner Object 写入多个文档 
 PUT softwares/_doc/1
 {
 "version":{
 "display_name":"7.1.0",
 "marjor":7,
 "minor":1,
 "hot_fix":0
 }} PUT softwares/_doc/2 
 {
 "version":{
 "display_name":"7.2.0",
 "marjor":7,
 "minor":2,
 "hot_fix":0
 }
 }PUT softwares/_doc/3 
 {
 "version":{
 "display_name":"7.2.1",
 "marjor":7,
 "minor":2,
 "hot_fix":1
 }
 }通过 bool 查询,POST softwares/_search 
 {
 "query": {
 "bool": {
 "filter": [
 {
 "match":{
 "version.marjor":7
 }
 },
 {
 "match":{
 "version.minor":2
 }
 }] } }} DELETE ratings Not Null 解决聚合的问题PUT ratings 
 {
 "mappings": {
 "properties": {
 "rating": {
 "type": "float",
 "null_value": 1.0
 }
 }
 }
 }PUT ratings/_doc/1 
 {
 "rating":5
 }PUT ratings/_doc/2 
 {
 "rating":null
 }POST ratings/_search POST ratings/_search 
 {
 "size": 0,
 "aggs": {
 "avg": {
 "avg": {
 "field": "rating"
 }
 }
 }
 }POST ratings/_search 
 {
 "query": {
 "term": {
 "rating": {
 "value": 1
 }
 }
 }
 }