[[mapping-object-type]]
=== Object Type

JSON documents are hierarchical in nature, allowing them to define inner
"objects" within the actual JSON. Elasticsearch completely understands
the nature of these inner objects and can map them easily, providing
query support for their inner fields. Because each document can have
objects with different fields each time, objects mapped this way are
known as "dynamic". Dynamic mapping is enabled by default. Let's take
the following JSON as an example:

[source,js]
--------------------------------------------------
{
    "tweet" : {
        "person" : {
            "name" : {
                "first_name" : "Shay",
                "last_name" : "Banon"
            },
            "sid" : "12345"
        },
        "message" : "This is a tweet!"
    }
}
--------------------------------------------------

The above shows an example where a tweet includes the actual `person`
details. A `person` is an object, with a `sid`, and a `name` object
which has `first_name` and `last_name`. It's important to note that
`tweet` is also an object, although it is a special
<<mapping-root-object-type,root object type>>
which allows for additional mapping definitions.

The following is an example of explicit mapping for the above JSON:

[source,js]
--------------------------------------------------
{
    "tweet" : {
        "properties" : {
            "person" : {
                "type" : "object",
                "properties" : {
                    "name" : {
                        "properties" : {
                            "first_name" : {"type" : "string"},
                            "last_name" : {"type" : "string"}
                        }
                    },
                    "sid" : {"type" : "string", "index" : "not_analyzed"}
                }
            },
            "message" : {"type" : "string"}
        }
    }
}
--------------------------------------------------

In order to mark a mapping of type `object`, set the `type` to object.
This is an optional step, since if there are `properties` defined for
it, it will automatically be identified as an `object` mapping.

[float]
==== properties

An object mapping can optionally define one or more properties using the
`properties` tag for a field. Each property can be either another
`object`, or one of the
<<mapping-core-types,core_types>>.

[float]
==== dynamic

One of the most important features of Elasticsearch is its ability to be
schema-less. This means that, in our example above, the `person` object
can be indexed later with a new property -- `age`, for example -- and it
will automatically be added to the mapping definitions. Same goes for
the `tweet` root object.

This feature is by default turned on, and it's the `dynamic` nature of
each object mapped. Each object mapped is automatically dynamic, though
it can be explicitly turned off:

[source,js]
--------------------------------------------------
{
    "tweet" : {
        "properties" : {
            "person" : {
                "type" : "object",
                "properties" : {
                    "name" : {
                        "dynamic" : false,
                        "properties" : {
                            "first_name" : {"type" : "string"},
                            "last_name" : {"type" : "string"}
                        }
                    },
                    "sid" : {"type" : "string", "index" : "not_analyzed"}
                }
            },
            "message" : {"type" : "string"}
        }
    }
}
--------------------------------------------------

In the above example, the `name` object mapped is not dynamic, meaning
that if, in the future, we try to index JSON with a `middle_name` within
the `name` object, it will get discarded and not added.

There is no performance overhead if an `object` is dynamic, the ability
to turn it off is provided as a safety mechanism so "malformed" objects
won't, by mistake, index data that we do not wish to be indexed.

If a dynamic object contains yet another inner `object`, it will be
automatically added to the index and mapped as well.

When processing dynamic new fields, their type is automatically derived.
For example, if it is a `number`, it will automatically be treated as
number <<mapping-core-types,core_type>>. Dynamic
fields default to their default attributes, for example, they are not
stored and they are always indexed.

Date fields are special since they are represented as a `string`. Date
fields are detected if they can be parsed as a date when they are first
introduced into the system. The set of date formats that are tested
against can be configured using the `dynamic_date_formats` on the root object,
which is explained later.

Note, once a field has been added, *its type can not change*. For
example, if we added age and its value is a number, then it can't be
treated as a string.

The `dynamic` parameter can also be set to `strict`, meaning that not
only new fields will not be introduced into the mapping, parsing
(indexing) docs with such new fields will fail.

[float]
==== enabled

The `enabled` flag allows to disable parsing and indexing a named object
completely. This is handy when a portion of the JSON document contains
arbitrary JSON which should not be indexed, nor added to the mapping.
For example:

[source,js]
--------------------------------------------------
{
    "tweet" : {
        "properties" : {
            "person" : {
                "type" : "object",
                "properties" : {
                    "name" : {
                        "type" : "object",
                        "enabled" : false
                    },
                    "sid" : {"type" : "string", "index" : "not_analyzed"}
                }
            },
            "message" : {"type" : "string"}
        }
    }
}
--------------------------------------------------

In the above, `name` and its content will not be indexed at all.


[float]
==== include_in_all

`include_in_all` can be set on the `object` type level. When set, it
propagates down to all the inner mapping defined within the `object`
that do no explicitly set it.

