ElasticSearch 数据建模

公号:码农充电站pro
主页:https://codeshellme.github.iohtml

一般在使用 ES 构建数据模型时,须要考虑如下几点:git

  • 字段类型
  • 是否须要搜索与分词
  • 是否须要聚合与排序
  • 是否须要额外的存储

1,字段类型

对于不一样类型的数据,主要考虑下面几点:github

  • 对于 Text 类型:用于全文本字段,数据会被分词器分词。
    • 默认不支持聚合分析及排序,须要设置 fielddatatrue
  • 对于 Keyword 类型:用于不须要分词处理的文本,例如手机号,email 地址,性别等。
    • 适用于精确匹配,支持聚合与排序。
  • 对于多字段类型:默认状况下,ES 会为将文本设置为 text 类型,并添加一个 keyword 子字段。
    • 在处理人类语言时,能够经过增长“英文”,“拼音”和“标准”分词器,来知足搜索需求。
  • 对于数值类型:尽可能选择贴近的类型。好比 byte 类型能知足需求,就不要用 long

2,搜索需求

对于搜索需求,主要考虑如下几点:shell

  • 若是不须要检索,排序和聚合,可将 enabled 设置成 false,以减小没必要要的处理(磁盘开销),来提升性能。
  • 若是不须要检索,但须要排序与聚合,可将 index 设置成 false

3,聚合与排序

对于聚合与排序,主要考虑如下几点:网络

  • 若是不须要检索,排序和聚合,可将 enabled 设置成 false
  • 若是须要检索,但不须要排序与聚合,可将 doc_valuesfielddata 设置成 false
  • 对于keyword 类型的字段,若是更新与聚合比较频繁,推荐将 eager_global_ordinals 设置为 true(能够达到利用缓冲的目的,提升性能)。

4,额外存储

store 设置为 true(默认为 false),能够存储字段的原始内容;通常在 _sourceenabledfalse 时使用。app

5,示例

若是须要对一些图书信息进行建模,需求以下:elasticsearch

  • 书名:支持全文本及精确匹配
  • 简介:支持全文本
  • 做者:支持精确匹配
  • 出版日期:日期类型
  • 图书封面:不须要支持搜索

示例数据以下:ide

{
  "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,那么每一个字段将被 ES 设置为以下类型:性能

{
  "type" : "text",   # text 类型
  "fields" : {       # 并添加一个 keyword 子字段
    "keyword" : {
      "type" : "keyword",
      "ignore_above" : 256
    }
  }
}

5.1,手动设置 mapping

下面根据需求,手动设置 mapping:优化

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

5.2,增长需求

若是如今须要添加一个字段 content,用于存储图书的内容,所以该字段的信息量将很是大,这将致使 _source 的内容过大,致使过大的网络开销。

为了优化,能够将 _sourceenabled 设置为 false,而后将每一个字段的 store 设置为 true(打开额外存储)。

以下:

PUT books
{
	"mappings": {
		"_source": {
			"enabled": false    # enabled 为 false
		},
		"properties": {
			"author": {
				"type": "keyword",
				"store": true   # store 为 true
			},
			"cover_url": {
				"type": "keyword",
				"index": false,
				"store": true  # store 为 true
			},
			"description": {
				"type": "text",
				"store": true  # store 为 true
			},
			"content": {
				"type": "text",
				"store": true  # store 为 true
			},
			"public_date": {
				"type": "date",
				"store": true # store 为 true
			},
			"title": {
				"type": "text",
				"fields": {
					"keyword": {
						"type": "keyword",
						"ignore_above": 100
					}
				},
				"store": true # store 为 true
			}
		}
	}
}

_source 禁止掉以后,查询的结果中就没有了 _source 字段;若是须要哪些字段的内容,则须要设置 stored_fields,以下:

POST books/_search
{
  "stored_fields": ["title","author","public_date"],
  "query": {
    "match": {
      "content": "searching"
    }
  }
}

(本节完。)


推荐阅读:

ElasticSearch DSL 查询

ElasticSearch 文档及操做

ElasticSearch 搜索模板与建议

ElasticSearch 聚合分析

ElasticSearch 中的 Mapping


欢迎关注做者公众号,获取更多技术干货。

码农充电站pro

相关文章
相关标签/搜索