Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand ElasticSearch dynamic Mapping and static Mapping

2025-02-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to understand ElasticSearch dynamic mapping and static mapping". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

9.1 Mapping classification

Dynamic mapping

As the name implies, it is an automatically created mapping. According to the saved document, es automatically analyzes the type and storage mode of the fields in the document, which is called dynamic mapping.

To take a simple example, create a new index and look at the index information:

Image-20201106201219878

In the index information created, you can see that mappings is empty, and what is stored in this mappings is the mapping information.

Now let's add a document to the index as follows:

PUT blog/_doc/1

{

"title": "1111"

"date": "2020-11-11"

}

When the document is added successfully, the Mappings is automatically generated:

Image-20201106201516427

As you can see, there are two types of the date field with the type date,title, text and keyword.

By default, if a field is added to the document, it will be automatically added to the mappings.

Sometimes, if you want to add new fields, you can throw an exception to remind the developer, which can be configured through the dynamic property in mappings.

The dynamic property has three values:

True, which is the default. Automatically add new fields. False, ignore the new field. Strict, strict mode, will throw an exception when new fields are found.

The specific configuration is as follows: specify mappings when creating the index (this is actually static mapping):

PUT blog

{

"mappings": {

"dynamic": "strict"

"properties": {

"title": {

"type": "text"

}

"age": {

"type": "long"

}

}

}

}

Then add data to the index in blog:

PUT blog/_doc/2

{

"title": "1111"

"date": "2020-11-11"

"age": 99

}

In the added document, there is an extra date field, which is not predefined, so the add operation returns an error:

{

"error": {

"root_cause": [

{

"type": "strict_dynamic_mapping_exception"

"reason": "mapping set to strict, dynamic introduction of [date] within [_ doc] is not allowed"

}

]

"type": "strict_dynamic_mapping_exception"

"reason": "mapping set to strict, dynamic introduction of [date] within [_ doc] is not allowed"

}

Status: 400

}

Dynamic mapping also has a problem with date detection.

For example, create a new index, and then add a document with a date, as follows:

PUT blog/_doc/1

{

"remark": "2020-11-11"

}

After a successful addition, the remark field is inferred to be a date type.

Image-20201106203240406

At this point, the remark field cannot store other types.

PUT blog/_doc/1

{

"remark": "javaboy"

}

The error at this time is as follows:

{

"error": {

"root_cause": [

{

"type": "mapper_parsing_exception"

"reason": "failed to parse field [remark] of type [date] in document with id '1room.Preview of field's value:' javaboy'"

}

]

"type": "mapper_parsing_exception"

"reason": "failed to parse field [remark] of type [date] in document with id '1room.Preview of field's value:' javaboy'"

"caused_by": {

"type": "illegal_argument_exception"

"reason": "failed to parse date field [javaboy] with format [strict_date_optional_time | | epoch_millis]"

"caused_by": {

"type": "date_time_parse_exception"

"reason": "Failed to parse with all enclosed parsers"

}

}

}

Status: 400

}

To solve this problem, you can use static mapping, that is, when the index is defined, remark is specified as the text type. You can also turn off date detection.

PUT blog

{

"mappings": {

"date_detection": false

}

}

At this point, the date type is treated as text.

Static mapping

A little.

9.2 Type inference

The inference method of dynamic mapping type in es is as follows:

This is the end of "how to understand ElasticSearch dynamic Mapping and static Mapping". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report