Dynamic templates combined with mappings

Hi,

I have an index that contains fields I'd like to set default dynamic
mappings for as well as a few customized mappings and have a couple
questions:

First, Is this the proper way to create the index?

$ curl -X POST "http://localhost:9200/users_test" -d '
{ "user": {
"dynamic_templates":[{
"long": {
"match_mapping_type":"long",
"match":"*",
"mapping":{ "omit_term_freq_and_positions":true,
"store":"no",
"type":"long",
"omit_norms":true,
"index":"analyzed",
"include_in_all":false }
}}]},
"mapping": {
"properties":{"birthdate":
{"null_value":-62167392000,"type":"long"}
}
}
}'
{"ok":true,"acknowledged":true}

I would expect to see the birthdate mapping show up when doing a get
on the _mapping. but it doesn't:

$ curl -X GET "http://localhost:9200/users_test/_mapping?pretty=true"
{
"users_test" : {
}

Instead it shows up in settings:

$ curl -X GET "http://localhost:9200/users_test/_settings?pretty=true"
{
"users_test" : {
"settings" : {
"index.mapping.properties.birthdate.null_value" :
"-62167392000",
"index.user.dynamic_templates.0.long.mapping.store" : "no",
"index.user.dynamic_templates.0.long.match_mapping_type" :
"long",
"index.user.dynamic_templates.0.long.mapping.include_in_all" :
"false",
"index.user.dynamic_templates.0.long.match" : "*",
"index.user.dynamic_templates.
0.long.mapping.omit_term_freq_and_positions" : "true",
"index.user.dynamic_templates.0.long.mapping.index" :
"analyzed",
"index.mapping.properties.birthdate.type" : "long",
"index.user.dynamic_templates.0.long.mapping.omit_norms" :
"true",
"index.user.dynamic_templates.0.long.mapping.type" : "long",
"index.number_of_shards" : "5",
"index.number_of_replicas" : "1"
}
}
}

Also, will the default "long" template values apply to birthday (ie.
birthday will have a null value set as well as omit_norms: true)?

Here is a gist if it's easier:

Thanks,
Dave

There are several problems in what you try to do, and a misunderstanding of
what dynamic_templates do...

First, the curl you use is to create the index, but, the mappings should be
under a "mappings" element, otherwise, those are used as settings for the
index, and not mappings.

Once we are over that, here is what dynamic_templates do: They apply to
dynamically added fields, not ones that are explicitly set. I think you
expect birthdate to have the settings you set in the long dynamic_template,
but that won't work, since it has its own mappings set already, so its not
dynamically introduced.

I think what you are after are custom types, which is something I have been
thinking on adding to elasticsearch. Something like defining a type called
"my_long", and then use it in explicit mappings.

Here is a gist of a working sample that uses dynamic templates for a
dynamically introduced birthdate: gist:1169471 · GitHub.

Here is a gist for explicit mapping for birthdate, that shows that the
dynamic template does not apply (and also to show another mistake you made
in the mappings): gist:1169474 · GitHub.

On Wed, Aug 24, 2011 at 11:32 PM, Dave Farkas dave@signalhq.com wrote:

Hi,

I have an index that contains fields I'd like to set default dynamic
mappings for as well as a few customized mappings and have a couple
questions:

First, Is this the proper way to create the index?

$ curl -X POST "http://localhost:9200/users_test" -d '
{ "user": {
"dynamic_templates":[{
"long": {
"match_mapping_type":"long",
"match":"*",
"mapping":{ "omit_term_freq_and_positions":true,
"store":"no",
"type":"long",
"omit_norms":true,
"index":"analyzed",
"include_in_all":false }
}}]},
"mapping": {
"properties":{"birthdate":
{"null_value":-62167392000,"type":"long"}
}
}
}'
{"ok":true,"acknowledged":true}

I would expect to see the birthdate mapping show up when doing a get
on the _mapping. but it doesn't:

$ curl -X GET "http://localhost:9200/users_test/_mapping?pretty=true"
{
"users_test" : {
}

Instead it shows up in settings:

$ curl -X GET "http://localhost:9200/users_test/_settings?pretty=true"
{
"users_test" : {
"settings" : {
"index.mapping.properties.birthdate.null_value" :
"-62167392000",
"index.user.dynamic_templates.0.long.mapping.store" : "no",
"index.user.dynamic_templates.0.long.match_mapping_type" :
"long",
"index.user.dynamic_templates.0.long.mapping.include_in_all" :
"false",
"index.user.dynamic_templates.0.long.match" : "*",
"index.user.dynamic_templates.
0.long.mapping.omit_term_freq_and_positions" : "true",
"index.user.dynamic_templates.0.long.mapping.index" :
"analyzed",
"index.mapping.properties.birthdate.type" : "long",
"index.user.dynamic_templates.0.long.mapping.omit_norms" :
"true",
"index.user.dynamic_templates.0.long.mapping.type" : "long",
"index.number_of_shards" : "5",
"index.number_of_replicas" : "1"
}
}
}

Also, will the default "long" template values apply to birthday (ie.
birthday will have a null value set as well as omit_norms: true)?

Here is a gist if it's easier:
dynamic templates combined with mappings · GitHub

Thanks,
Dave