Mapping is mapping, which is used to define a document and how the fields contained in the document should be stored and indexed. Therefore, it is actually a bit similar to the definition of tables in relational databases.
Mapping classification
Dynamic mapping
As the name suggests, it is a map created automatically. es automatically analyzes the type and storage mode of fields in the document according to the stored document. This is dynamic mapping.
For a simple example, create a new index and view the index information:
Insert the code slice here
In the created index information, you can see that the mappings is empty, and the mapping information is saved in the mappings.
Now we add a document to the index as follows:
PUT blog/_doc/1 { "title":"1111", "date":"2020-11-11" }
After the document is successfully added, Mappings will be automatically generated:
You can see that the type of date field is date, and there are two types of title, text and keyword.
By default, if a field is added in the document, it will be automatically added in mappings.
Sometimes, if you want to add a field, you can throw an exception to remind the developer. This can be configured through the dynamic attribute in mappings.
The dynamic attribute has three values:
true, the default is this. Automatically add new fields.
false to ignore the new field.
Strict, strict mode. An exception will be thrown if a new field is found.
The specific configuration method is as follows: specify mappings when creating an index (this is actually static mapping):
PUT blog { "mappings": { "dynamic":"strict", "properties": { "title":{ "type": "text" }, "age":{ "type":"long" } } } }
Then add data to the blog index:
PUT blog/_doc/2 { "title":"1111", "date":"2020-11-11", "age":99 }
In the added document, there is an additional date field, which is not predefined, so the addition operation will report 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 of 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 successful addition, the remark field will be inferred as a date type.
At this point, the remark field cannot store other types.
PUT blog/_doc/1 { "remark":"javaboy" }
At this time, the error is reported as follows:
{ "error" : { "root_cause" : [ { "type" : "mapper_parsing_exception", "reason" : "failed to parse field [remark] of type [date] in document with id '1'. Preview of field's value: 'javaboy'" } ], "type" : "mapper_parsing_exception", "reason" : "failed to parse field [remark] of type [date] in document with id '1'. 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 defining the index, specify remark as text type. You can also turn off date detection.
PUT blog { "mappings": { "date_detection": false } }
At this time, the date type will be processed as a written document.
Type inference
The inference method of dynamic mapping type in es is as follows:
Data in json | Automatically inferred data type |
---|---|
null | No fields were added |
true/false | boolean |
Floating point type | float |
number | long |
json object | object |
string | text/keyword/date/double/long |