Does Elasticsearch Dynamic Mapping Reindex Documents?

1k Views Asked by At

THE PROBLEM

I'm new to elasticsearch, and I would like to understand the relation between dynamic mapping and reindexing documents.

From my experiments when dynamic mapping is on, the reindexing of documents is done automatically. which means if you add a new field to a document it gets automatically indexed.

In other hand, if the index does not have dynamic mapping turned on, new mappings will not be indexed and a manual reindexing will have to be performed. Else documents will not be found in the searches.

Is this a correct assumption. I tried to find out clear information about this but could not find it.

You can find my experiments Bellow

TEST1 - Index With Mapping

# 1.1 create index with mapping dynamic

    PUT /product_dyn_on
        {
            "mappings" : {
                "dynamic": true,
                "properties": {
                    "price": { "type": "integer" }
                }
            }
        }


# 1.2 create a document

        POST /product_dyn_on/_doc/1
        { "price": 100 } 


# 1.3 create a document with a field that was not mapped at the beginning

        POST /product_dyn_on/_doc/2
        { "price": 100, "discount" : 20}


# 1.4 browse document to see if it has was mapped
      GET /product_dyn_on/_search

        GET /product_dyn_on/_search
        { 
          "query": {"term" : {"discount" : 20}}

        }


        GET /product_dyn_on/_search
        { 
          "query": {"match" : {"price" : 100}}

        }


# 1.5 - update first object


    PUT /product_dyn_on/_doc/1
    {
      "price" : 200,
  "discount" : 20
    }

# 1.6 - check results (both objects are returned)


        GET /product_dyn_on/_search
        { 
          "query": {"match" : {"discount" : 20}}

        }

TEST2 - Index Without Mapping

# 2.1 create index with mapping dynamic

        PUT /product_dyn_off
            {
                "mappings" : {
                    "dynamic": false,
                    "properties": {
                        "price": { "type": "integer" },
                        "is_active": {"type": "boolean" },
                        "price": { "type": "integer"},
                        "sold": { "type": "long"}
                    }
                }
            }


    # 2.2 create a document

            POST /product_dyn_off/_doc/1
            { "price": 100 } 


    # 2.3 create a document with a field that was not mapped at the beginning

            POST /product_dyn_off/_doc/2
            { "price": 100, "discount" : 20}


    # 2.4 browse document to see if it has was mapped
          GET /product_dyn_off/_search

            GET /product_dyn_off/_search
            { 
              "query": {"term" : {"discount" : 20}}

            }


            GET /product_dyn_off/_search
            { 
              "query": {"match" : {"price" : 100}}

            }


    # 2.5 - Update first object


        PUT /product_dyn_off/_doc/1
        {
          "price" : 200,
      "discount" : 20
        }

    # 2.6 - Check results

            GET /product_dyn_off/_search

            GET /product_dyn_off/_search
            { 
              "query": {"match" : {"discount" : 20}}

            }

            GET /product_dyn_off/_search
            { 
              "query": {"match" : {"price" : 200}}

            }


    # Clean Everything

        DELETE /product_dyn_on
        DELETE /product_dyn_off
1

There are 1 best solutions below

1
On

Dynamic index

By default elasticsearch will index documents if any new field(null,boolean(true/false),float,integer,object,array,string(date,long,double,text field, with a keyword sub-field)) found and same we can customize using dynamic mapping and templates https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-mapping.html

Dynamic keyword

We can restrict index dynamic fields using this keyword. true(default),false(not indexed but stored) and strict(exception thrown and rejected) https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic.html