Field Type
Type JSON data types are mapped to ElasticSearch defined, simple common types are:
JSON type | ElasticSearch type |
---|---|
Text Types | Text/Keyword |
Integer type | long/integer |
Floating-point type | float/double |
Time Type | date |
Boolean value | boolean |
Array | Text/Keyword |
It is noted that the time above type, JSON and no time type, here mainly refers to the type of time format of the data.
Define the mapping
In a relational database, before storing data, we will first create the table structure, the existence of a specified type to the field. Similarly ElasticSearch before performing data storage, may be define structure Mapping good store data.
First define a simple person Mapping:
Mapping the figure above is a definition, if it is before ElasticSearch7, mappings where there _type property.
Dynamic map
When there is no pre-defined well Mapping, add data, ElasticSearch will automatically corresponding to the type of conversion according to the field, but not necessarily the type of conversion out of the field is the type we want, or need human intervention to modify the desired Mapping.
Updated maps
Use dynamic control mappings may be updated.
dynamic-true
Dynamic set to true
the default dynamic default values, the new data can be written into the field, but can also be indexed, the Mapping structure will be updated.
Add data, but not more than add a defined gender
field.
# 向 person 中添加数据
PUT person/_doc/1
{
"uId": 1,
"name": "ytao",
"age": 18,
"address": "广东省珠海市",
"birthday": "2020-01-15T12:00:00Z",
"money": 108.2,
"isStrong": true,
"gender": "男" # Mapping 中未定义的字段
}
Added successfully, Search gender
field:
View Mapping structure:
The new field value added during the addition Mapping automatically add the field.
dynamic-false
Dynamic is set false
, the new data may be written to the field, can not be indexed, the Mapping structure will be updated.
Also first dynamic set to false, then add data to the inside, and other steps as above true operation. Defined Mapping, add data.
Search gender
field:
At this new field data it can not be indexed, but the data can be written.
Mappnig will not add new fields:
dynamic-strict
Dynamic set to strict
the time, can be seen from the literal meaning, the dynamic mapping is more stringent, the new field of data can not be written to, may not be indexed, the Mapping structure is not updated. Only add data Mapping defined according to the structure.
When adding a new data field, immediately throws an exception:
Automatic Identification Date Type
In the above, when the dynamic set to true, add a new data field updates the Mapping automatically recognize the type, date type if it is, then we can specify the type of identification.
The designated person dynamic_date_formats format:
PUT person/_mapping
{
"dynamic_date_formats": ["yyyy/MM/dd"]
}
Here you can specify multiple time format.
Add new data to a person, they are today and firstDate:
PUT person/_doc/2
{
"today": "2020-01-15",
"firstDate": "2020/01/15"
}
Add Mapping the new field data:
Due to the above we specify the format for the time yyyy/MM/dd
when time is recognized as a format, so today is the field yyyy-MM-dd
format is not recognized as a time type, sentenced to type text.
Multi-Field
Mapping can be defined fields multiple field properties to meet achieve different scenarios. For example address
defined text
type, which have defined Fields keyword
type, where the main use is different to distinguish between two different scenarios.
text
Creates word inverted index for full text search.keyword
It does not create a word inverted index for sorting and aggregation.
adding data:
# 向 person 中添加数据
PUT person/_doc/1
{
"uId": 1,
"name": "ytao",
"age": 18,
"address": "广东省珠海市",
"birthday": "2020-01-15T12:00:00Z",
"money": 108.2,
"isStrong": true
}
Query address
data.
Query address.keyword
data.
By keyword
retrieving, because the index does not create a word, and did not get to the data.
Control Index
Use in the field index to specify the current field if the index can be searched. Specify the type boolean type, false as not to search, true to be searched.
Delete Mapping before:
DELETE person
Creating Mapping, set the name
property index
to false.
After adding the above data again search name
field:
After the index field is set false, because there is no indexed, so the search can not get to the index.
Null handling
Now add an empty address data to the ElasticSearch in:
PUT person/_doc/2
{
"uId": 2,
"name": "Jack",
"age": 22,
"address": null,
"birthday": "2020-01-15T12:00:00Z",
"money": 68.7,
"isStrong": true
}
Search address.keyword is empty of data:
Search returns an exception, the default is not allowed to search NUll.
It is necessary to specify Mapping null_value property, and can not text
declare types.
Search address.keyword is empty of data:
Set "null_value": "NULL"
, the search process can be null.
Polymerizing a plurality of fields
Polymerized into a plurality of index fields using copy_to polymerization. For example, we in the multi-field queries, it is not required to filter screening for each field, just to the polymerization field.
In use copy_to, it is polymerized by specifying the name.
In fact, copy_to not add a name using an array format, it will be automatically converted into data format.
Adding two data search to be verified:
# 向 person 中添加数据
PUT person/_doc/1
{
"uId": 1,
"name": "ytao",
"age": 18,
"address": "广东省珠海市",
"birthday": "2020-01-15T12:00:00Z",
"money": 108.2,
"isStrong": true
}
PUT person/_doc/2
{
"uId": 2,
"name": "杨广东",
"age": 22,
"address": null,
"birthday": "2020-01-15T12:00:00Z",
"money": 68.7,
"isStrong": true
}
Query full_name
value, will return name and address of the associated object values.
Return to see the results, _source in the field does not have a corresponding increase in copy_to field names, so copy_to only copy field contents to the index, and will not change the field contains from above.
to sum up
Mapping created by the usual paper documents and practical operation of the presentation, but also can master these basic daily use. Learn Mapping functions operate, I believe that the design of the store will also help.