Elk Learning Summary (4-1) Elasticsearch Change Mapping (non-stop Service Rebuild index)

Source: Internet
Author: User

Elasticsearch Change Mapping (non-stop Service Rebuild index)
Original http://donlianli.iteye.com/blog/1924721
Elasticsearch mapping once created, only fields can be added, not fields that have already been mapping. But the reality is often not so ah, sometimes add a field, as if to hit a patch, one can, but the more fill the more, finally I feel miserable. What to do??
Here's a way to modify mapping, which is to re-establish an index and then create a new mapping. You may ask, is this possible in a production environment? The answer is, if you've taken the right design in the first place, this can be a smooth transition.
What reasonable design should be taken? Is that when our program accesses the index library, it always uses synonyms to access it, rather than using the real indexname. After reindex the data, modify the previous synonyms. Do you understand?
Refer to the above ideas, we come step by step.

Step1, create an index, the name of the index preferably with a version number, such as MY_INDEX_V1,MY_INDEX_V2.
Step2, create a synonym that points to this index.
Curl-xpost localhost:9200/_aliases-d '
{
"Actions": [
{"Add": {
"Alias": "My_index",
"Index": "MY_INDEX_V1"
}}
]
}‘

At this point, you can access it through synonyms my_index. This includes creating indexes, deleting indexes, and so on.
Step3, needs come, need to change mapping, at this time, you need to create a new index, such as name MY_INDEX_V2 (Version upgrade). Create your new mapping structure in this index. The new data is then brushed into the new index. In the process of brushing the data, you may want to take the data directly from the old index and change the format. How to traverse all the old index data, please refer here.
Step4, modify synonyms. Change the synonym for V1 to point to V2. The HTTP interface is as follows:

Curl-xpost localhost:9200/_aliases-d '
{
"Actions": [
{"Remove": {
"Alias": "My_index",
"Index": "MY_INDEX_V1"
}},
{"Add": {
"Alias": "My_index",
"Index": "MY_INDEX_V2"
}}
]
}‘

STEP5, delete the old index.

Curl-xdelete LOCALHOST:9200/MY_INDEX_V1
In addition, there are several other ways to change the mapping.

1, modify the program, add fields.
Add a new field to the mapping and then you access the statistics search for the new field.
This is going to change two places, one is to modify the mapping add field, there is to modify your program, the field to change the new field.

2. Change the field type to Multi_field.
Multi_field allows you to set multiple data types for a single field.
One of the most typical scenarios for applying multi_field is that a type is defined as analyed, and this field can be searched for, and a type is defined as a non-participle, which is used for sorting.
Any field can be updated to Multi_field (except for types that are type object and nested).
Suppose you now have a field named created and the type is now string.
{"created": {"type": "String"}}
We can add it to a type so that he can be used both as a string and as a date type.

Curl-xput localhost:9200/my_index/my_type/_mapping-d '
{
"My_type": {
"Properties": {
"Created": {
"Type": "Multi_field",
"Fields": {
"Created": {"type": "String"},
"Date": {"type": "Date"}
}
}
}
}
}
‘
When using the standard method of rebuilding the index, we recommend that you create an index synonym for each type.
Even if multiple type is in the same index library, it is recommended to use the Create a synonym to access it.
That is, an index contains a type,
Because in Elasticsearch, it is convenient to query data across the index.
In this way, we can immediately take the type into effect after reindex a type,
Instead of rebuilding all the type below index, the synonym will take effect.

Elk Learning Summary (4-1) Elasticsearch Change Mapping (non-stop Service Rebuild index)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.