[Elasticsearch] index management (4)-Dynamic ing

Source: Internet
Author: User

[Elasticsearch] index management (4)-Dynamic ing

Dynamic IC ing)

When ES encounters a field not previously seen in the document, it uses dynamic ing to determine the type of the field and automatically adds ing to the field.

Sometimes this is what you need, but sometimes it is not. You may not know which fields will be added to your documents in the future, but you want them to be automatically indexed. Maybe you just want to ignore them. Or-especially when you use elasticsearch as the primary data storage-you may expect these unknown fields to throw an exception to remind you of this issue.

Fortunately, you candynamicTo control this line, it can accept the following options:

  • true: Default value. Dynamically add fields
  • false: Ignore new fields
  • strict: If a strange field is encountered, an exception is thrown.

    dynamicThe setting can be applied to the root object orobjectAny field of the type. You should setdynamicSetstrictBut enable it for a specific internal object:

    PUT /my_index{    "mappings": {        "my_type": {            "dynamic":      "strict",             "properties": {                "title":  { "type": "string"},                "stash":  {                    "type":     "object",                    "dynamic":  true                 }            }        }    }}

    Inmy_typeIf an unknown field is encountered on the object, an exception is thrown. InstashNew fields are dynamically added to the object.

    With the above ing, you canstashAdd a new searchable field:

    PUT /my_index/my_type/1{  "title": "This doc adds a new field",  "stash": {    "new_field": "Success!"  }}

    However, if you try to add a new field to the top-level object, it will fail:

    PUT /my_index/my_type/1{    "title":     "This throws a StrictDynamicMappingException",    "new_field": "Fail!"}

    NOTE

    SetdynamicSetfalseIt will not change_sourceField content-_sourceThe field will still save the entire JSON document you indexed. A strange field will not be added to the ing, so that it cannot be searched.


    Custom dynamic ing

    If you know that you need to dynamically add new fields, you may enable dynamic ing. However, sometimes the dynamic ing rules are not flexible enough. Fortunately, you can adjust some settings to make dynamic ing rules more suitable for your data.

    date_detection

    When ES encounters a new string field, it checks whether the string contains a recognizable date, such2014-01-01. If yes, it will be recognized asdateType field. Otherwise, it will be usedstring.

    Sometimes this behavior may cause some problems. If you want to index a document like this:

    { "note": "2014-01-01" }

    Suppose this isnoteWhen a field is first discovered, it will be useddateField. However, if the next document is as follows:

    { "note": "Logged out" }

    This field is obviously not a date, but it is too late. This field is already of the date type. Therefore, an exception is thrown.

    You candate_detectionSetfalseTo disable date Detection:

    PUT /my_index{    "mappings": {        "my_type": {            "date_detection": false        }    }}

    With the above ing, a string will always be treatedstringType. If you needdateField, You need to manually add it.

    NOTE

    Elasticsearch supports date recognition throughdynamic_date_formatsSet Change.

    dynamic_templates

    Passdynamic_templatesYou can have full control over the dynamic ing rules of new fields. You can use a different ing rule based on the field name or type.

    Each template has a name that describes what the template has done. At the same time, it hasmappingSpecifies the specific ing information and at least one parameter (for examplematchSpecifies the fields to use this template.

    Template matching is ordered-the first matching template will be used. For examplestringThe field specifies two templates:

    • es:_esThe end field should bespanishParser
    • en: Use of all other fieldsenglishParser

      We needesThe template is placed in the first one, because it is betterenThe template is more specific:

      PUT /my_index{    "mappings": {        "my_type": {            "dynamic_templates": [                { "es": {                      "match":              "*_es",                       "match_mapping_type": "string",                      "mapping": {                          "type":           "string",                          "analyzer":       "spanish"                      }                }},                { "en": {                      "match":              "*",                       "match_mapping_type": "string",                      "mapping": {                          "type":           "string",                          "analyzer":       "english"                      }                }}            ]}}}

      match_mapping_typeAllows you to use templates only for specific fields, as in standard dynamic ing rules, suchstring,long.

      matchThe parameter only matches the field name,path_matchParameters are used to match the complete path of fields in the object, suchaddress.*.nameThe following fields can be matched:

      {    "address":        "city":            "name": "New York"        }    }}

      unmatchAndpath_unmatchMode can be used to exclude certain fields. fields that are not excluded will be matched.

      More configuration options can be found in the root object reference document.


Related Article

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.