写点什么

Elasticsearch Mapping

用户头像
escray
关注
发布于: 2021 年 02 月 09 日
Elasticsearch Mapping

有关 Elasticsearch Mapping,文字内容来自 B 站中华石杉 Elasticsearch 高手进阶课程,英文内容来自官方文档。


插入几条数据,让 Elasticsearch 自动为我们建立一个索引


PUT /website/article/1{  "post_date": "2017-01-01",  "title": "my first article",  "content": "this is my first article in this website",  "author_id": 11400}
PUT /website/article/2{ "post_date": "2017-01-02", "title": "my second article", "content": "this is my second article in this website", "author_id": 11400}
PUT /website/article/3{ "post_date": "2017-01-03", "title": "my third article", "content": "this is my third article in this website", "author_id": 11400}
复制代码


尝试各种搜索


# 5.2GET /website/article/_search?q=2017											# 3条结果             GET /website/article/_search?q=2017-01-01        				# 3条结果GET /website/article/_search?q=post_date:2017-01-01   	# 1条结果GET /website/article/_search?q=post_date:2017         	# 1条结果
# 7.10GET website/_search?q=2017 # 1条结果GET website/_search?q=2017-01-01 # 1条结果GET website/_search?q=post_date:2017 # 1条结果GET website/_search?q=post_date:2017-01-01 # 1条结果
复制代码


在 7.10 版本的 Elasticsearch 上查了一下,都是一条结果,为什么呢?直接去看官方文档。

Mapping

Mapping is the process of defining how a document, and the fields it contains, are stored and indexed. For instance, use mappings to define:


  • which string fields should be treated as full text fields.

  • which fields contain numbers, dates, or geolocations.

  • the format of date values.

  • custom rules to control the mapping for dynamically added fields.

Settings to prevent mappings explosion

Defining too many fields in an index can lead to a mapping explosion, which can cause out of memory errors and difficult situations to recover from.


Consider a situation where every new document inserted introduces new fields, such as with dynamic mapping. Each new field is added to the index mapping, which can become a problem as the mapping grows.


# Create an index with an explicit mappingPUT /my-index-000001{  "mappings": {    "properties": {      "age":    { "type": "integer" },        "email":  { "type": "keyword"  },       "name":   { "type": "text"  }         }  }}
# Add a field to an existing mappingPUT /my-index-000001/_mapping{ "properties": { "employee-id": { "type": "keyword", "index": false } }}
# View the mapping of an indexGET /my-index-000001/_mapping
# View the mapping of specific fieldsGET /my-index-000001/_mapping/field/employee-id
复制代码


自动或手动为 index 中的 type 建立的一种数据结构和相关配置,简称为 mapping


dynamic mapping,自动为我们建立 index,创建 type,以及 type 对应的 mapping,mapping 中包含了每个 field 对应的数据类型,以及如何分词等设置

Dynamic mapping


Fields and mapping types do not need to be defined before being used. Thanks to dynamic mapping, new field names will be added automatically, just by indexing a document. New fields can be added both to the top-level mapping type, and to inner object and nested fields.


The dynamic mapping rules can be configured to customise the mapping that is used for new fields.



GET website/_mapping
{ "website" : { "mappings" : { "properties" : { "author_id" : { "type" : "long" }, "content" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "post_date" : { "type" : "date" }, "title" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } } }}
复制代码


搜索结果为什么不一致,因为 Elasticsearch 自动建立 mapping 的时候,设置了不同的 field 不同的 data type。不同的 data type 的分词、搜索等行为是不一样的。所以出现了_all field 和 post_date field 的搜索表现完全不一样。

遗留问题


没能解决为什么我的查询结果和教程中不一样的问题,应该是因为版本升级,倒是查询的条件变了,留待以后解决。


发布于: 2021 年 02 月 09 日阅读数: 45
用户头像

escray

关注

Let's Go 2017.11.19 加入

在学 Elasticsearch 的项目经理

评论

发布
暂无评论
Elasticsearch Mapping