# Wrong dynamic template or issue 1181?

**URL:** <https://discuss.elastic.co/t/wrong-dynamic-template-or-issue-1181/5780>\
**Category:** Elasticsearch\
**Created:** [November 5, 2011, 1:08pm UTC](https://discuss.elastic.co/t/wrong-dynamic-template-or-issue-1181/5780 "2011-11-05T13:08:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [November 5, 2011, 1:08pm UTC](https://discuss.elastic.co/t/wrong-dynamic-template-or-issue-1181/5780/1 "2011-11-05T13:08:01Z")

</div>

Hi,

I'm having problems when upgrading to 0.18.\* (tried all versions, did  
not occur in \<0.18) and it seems to me I was either bitten via:

> <https://github.com/elastic/elasticsearch/issues/1181>

or via my template:  
{ "tweet" : {  
"dynamic\_templates" : [  
{  
"template\_1" : {  
"match" : "\*",  
"match\_mapping\_type" : "string",  
"mapping" : { "type" : "string", "omit\_norms" :  
"yes" }  
}  
}  
],  
(no mapping for my date field called 'dt')  
}}

When I remove this template all is fine but when not and I'm sorting  
by my date field 'dt' I do not get any documents\*\*:

count:2  
hits:2  
hits sorted:0

But why is now a date object of type string? Or is this related to the  
issue where Shay says: "Will revert this, as its tricky without  
changing default behavior now that I think about it. Need to think of  
a better way to suport optional timezone in this case." So is this  
issue already reverted (as it is closed) or is it in the 0.18  
releases?

Peter.

Settings s = ImmutableSettings.settingsBuilder().put("cluster.name",  
CLUSTER).build();  
TransportClient client = new TransportClient(s);  
client.addTransportAddress(new InetSocketTransportAddress("127.0.0.1",  
PORT));  
try {  
client.admin().indices().create(new  
CreateIndexRequest("tmp")).actionGet();  
} catch (IndexAlreadyExistsException ex) {  
}  
client.prepareDeleteByQuery("tmp").setTypes("tweet").setQuery(QueryBuilders.matchAllQuery());  
client.admin().indices().refresh(new RefreshRequest("tmp"));

BulkRequest br = new BulkRequest();  
XContentBuilder cb =  
JsonXContent.contentBuilder().startObject();  
cb.field("dt", new Date(new Date().getTime() - 10000));  
cb.field("text", "java");  
br.add(new IndexRequest("tmp").id("1").type("tweet").source(cb));

cb = JsonXContent.contentBuilder().startObject();  
cb.field("dt", new Date());  
cb.field("text", "java");  
br.add(new IndexRequest("tmp").id("2").type("tweet").source(cb));  
client.bulk(br).actionGet();

client.admin().indices().refresh(new RefreshRequest("tmp"));  
System.out.println("count:" + client.prepareCount("tmp").  
execute().actionGet().count());  
System.out.println("hits:" + client.prepareSearch("tmp").  
setQuery(QueryBuilders.queryString("java").field("text")).  
execute().actionGet().hits().totalHits());

System.out.println("hits sorted:" + client.prepareSearch("tmp").  
setQuery(QueryBuilders.queryString("java").field("text")).  
addSort("dt", SortOrder.DESC).  
execute().actionGet().hits().totalHits());

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [November 5, 2011, 1:13pm UTC](https://discuss.elastic.co/t/wrong-dynamic-template-or-issue-1181/5780/2 "2011-11-05T13:13:48Z")

</div>

ok, when I'm explicitely map the date field then it returns:  
count:0  
hits:2  
hits sorted:2

for the count query I need to set the type =\> then all is fine (all  
count==2) ... although this is not expected behaviour, right 🙂 ?

Peter.

On 5 Nov., 14:08, Karussell [tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com) wrote:

> Hi,
> 
> I'm having problems when upgrading to 0.18.\* (tried all versions, did  
> not occur in \<0.18) and it seems to me I was either bitten via:[Mapping: Root object non ISO date formats to support timezone · Issue #1181 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/1181)
> 
> or via my template:  
> { "tweet" : {  
> "dynamic\_templates" : [  
> {  
> "template\_1" : {  
> "match" : "\*",  
> "match\_mapping\_type" : "string",  
> "mapping" : { "type" : "string", "omit\_norms" :  
> "yes" }  
> }  
> }  
> ],  
> (no mapping for my date field called 'dt')
> 
> }}
> 
> When I remove this template all is fine but when not and I'm sorting  
> by my date field 'dt' I do not get any documents\*\*:
> 
> count:2  
> hits:2  
> hits sorted:0
> 
> But why is now a date object of type string? Or is this related to the  
> issue where Shay says: "Will revert this, as its tricky without  
> changing default behavior now that I think about it. Need to think of  
> a better way to suport optional timezone in this case." So is this  
> issue already reverted (as it is closed) or is it in the 0.18  
> releases?
> 
> Peter.
> 
> Settings s = ImmutableSettings.settingsBuilder().put("cluster.name",  
> CLUSTER).build();  
> TransportClient client = new TransportClient(s);  
> client.addTransportAddress(new InetSocketTransportAddress("127.0.0.1",  
> PORT));  
> try {  
> client.admin().indices().create(new  
> CreateIndexRequest("tmp")).actionGet();} catch (IndexAlreadyExistsException ex) {  
> }
> 
> client.prepareDeleteByQuery("tmp").setTypes("tweet").setQuery(QueryBuilders.matchAllQuery());  
> client.admin().indices().refresh(new RefreshRequest("tmp"));
> 
> BulkRequest br = new BulkRequest();  
> XContentBuilder cb =  
> JsonXContent.contentBuilder().startObject();  
> cb.field("dt", new Date(new Date().getTime() - 10000));  
> cb.field("text", "java");  
> br.add(new IndexRequest("tmp").id("1").type("tweet").source(cb));
> 
> cb = JsonXContent.contentBuilder().startObject();  
> cb.field("dt", new Date());  
> cb.field("text", "java");  
> br.add(new IndexRequest("tmp").id("2").type("tweet").source(cb));  
> client.bulk(br).actionGet();
> 
> client.admin().indices().refresh(new RefreshRequest("tmp"));  
> System.out.println("count:" + client.prepareCount("tmp").  
> execute().actionGet().count());  
> System.out.println("hits:" + client.prepareSearch("tmp").  
> setQuery(QueryBuilders.queryString("java").field("text")).  
> execute().actionGet().hits().totalHits());
> 
> System.out.println("hits sorted:" + client.prepareSearch("tmp").  
> setQuery(QueryBuilders.queryString("java").field("text")).  
> addSort("dt", SortOrder.DESC).  
> execute().actionGet().hits().totalHits());

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [November 9, 2011, 6:15am UTC](https://discuss.elastic.co/t/wrong-dynamic-template-or-issue-1181/5780/3 "2011-11-09T06:15:34Z")

</div>

Heya,

Yea, its not related to this issue, but there was another issue where  
dynamic templates on attachment did not work, which probably caused this  
behavior. Basically, for a new field introduced, and its of type string, it  
will first go and try and find a template for it (under string type). So,  
in your case, it will be checked and applied as string, even though it  
matches a date pattern. I opened an issue:  
[Mapping: Improve applying guessed types on dynamic templates · Issue #1446 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/1446).

-shay.banon

On Sat, Nov 5, 2011 at 3:13 PM, Karussell [tableyourtime@googlemail.com](mailto:tableyourtime@googlemail.com)wrote:

> ok, when I'm explicitely map the date field then it returns:  
> count:0  
> hits:2  
> hits sorted:2
> 
> for the count query I need to set the type =\> then all is fine (all  
> count==2) ... although this is not expected behaviour, right 🙂 ?
> 
> Peter.
> 
> On 5 Nov., 14:08, Karussell [tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com) wrote:
> 
> > Hi,
> > 
> > I'm having problems when upgrading to 0.18.\* (tried all versions, did  
> > not occur in \<0.18) and it seems to me I was either bitten via:  
> > [Mapping: Root object non ISO date formats to support timezone · Issue #1181 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/1181)
> > 
> > or via my template:  
> > { "tweet" : {  
> > "dynamic\_templates" : [  
> > {  
> > "template\_1" : {  
> > "match" : "\*",  
> > "match\_mapping\_type" : "string",  
> > "mapping" : { "type" : "string", "omit\_norms" :  
> > "yes" }  
> > }  
> > }  
> > ],  
> > (no mapping for my date field called 'dt')
> > 
> > }}
> > 
> > When I remove this template all is fine but when not and I'm sorting  
> > by my date field 'dt' I do not get any documents\*\*:
> > 
> > count:2  
> > hits:2  
> > hits sorted:0
> > 
> > But why is now a date object of type string? Or is this related to the  
> > issue where Shay says: "Will revert this, as its tricky without  
> > changing default behavior now that I think about it. Need to think of  
> > a better way to suport optional timezone in this case." So is this  
> > issue already reverted (as it is closed) or is it in the 0.18  
> > releases?
> > 
> > Peter.
> > 
> > Settings s = ImmutableSettings.settingsBuilder().put("cluster.name",  
> > CLUSTER).build();  
> > TransportClient client = new TransportClient(s);  
> > client.addTransportAddress(new InetSocketTransportAddress("127.0.0.1",  
> > PORT));  
> > try {  
> > client.admin().indices().create(new  
> > CreateIndexRequest("tmp")).actionGet();} catch  
> > (IndexAlreadyExistsException ex) {  
> > }
> 
> client.prepareDeleteByQuery("tmp").setTypes("tweet").setQuery(QueryBuilders.matchAllQuery());
> 
> > client.admin().indices().refresh(new RefreshRequest("tmp"));
> > 
> > BulkRequest br = new BulkRequest();  
> > XContentBuilder cb =  
> > JsonXContent.contentBuilder().startObject();  
> > cb.field("dt", new Date(new Date().getTime() - 10000));  
> > cb.field("text", "java");  
> > br.add(new IndexRequest("tmp").id("1").type("tweet").source(cb));
> > 
> > cb = JsonXContent.contentBuilder().startObject();  
> > cb.field("dt", new Date());  
> > cb.field("text", "java");  
> > br.add(new IndexRequest("tmp").id("2").type("tweet").source(cb));  
> > client.bulk(br).actionGet();
> > 
> > client.admin().indices().refresh(new RefreshRequest("tmp"));  
> > System.out.println("count:" + client.prepareCount("tmp").  
> > execute().actionGet().count());  
> > System.out.println("hits:" + client.prepareSearch("tmp").  
> > setQuery(QueryBuilders.queryString("java").field("text")).  
> > execute().actionGet().hits().totalHits());
> > 
> > System.out.println("hits sorted:" + client.prepareSearch("tmp").  
> > setQuery(QueryBuilders.queryString("java").field("text")).  
> > addSort("dt", SortOrder.DESC).  
> > execute().actionGet().hits().totalHits());

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 3:49am UTC](https://discuss.elastic.co/t/wrong-dynamic-template-or-issue-1181/5780/4 "2017-07-06T03:49:27Z")

</div>

Closed on July 6, 2017, 3:49am UTC


