Reputation: 63
I have the following C# model :
[ElasticType(Name = "myType")]
public class MyType
{
...
[ElasticProperty(Name = "ElasticId")]
[DataMember(Name = "ElasticId")]
public string ElasticId { get; set; }
...
[ElasticProperty(Name = "DateToBeUsed", Type = FieldType.Date, DateFormat = "date_hour_minute_second_millis")]
public string DateToBeUsed { get; set; }
...
}
The "date_hour_minute_second_millis" correspond to following format : yyyy-MM-dd’T'HH:mm:ss.SSS (http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-date-format.html)
The mapping ES is done with Nest using "map" method and correspond to that :
"mappings": {
"myType": {
"properties": {
...,
"ElasticId": {
"type": "string"
},
...,
"DateToBeUsed": {
"type": "date",
"format": "date_hour_minute_second_millis"
},
...
}
}
}
I insert an document inside this index:
"_source": {
...,
"ElasticId": "2",
...,
"DateToBeUsed": "2012-05-21T09:51:34.073",
...
}
My problem is when I want to retrieve this object through Nest.
The value of DateToBeUsed is always formatted with the following format : MM/dd/yyyy HH:mm:ss (ex : 05/21/2012 09:51:34)
(Using sense, the value is well formatted.)
1°) Is this normal?
I need to retrieve the same date format than the one I gave to ES. (And I think it should be normal to have the same format as described in the mapping)
2°) Is there a "clean" solution to resolve this problem? (Re-formatting the date after having retrieved the document is not a "clean" solution...)
Thanks for the answers! Bye.
Upvotes: 3
Views: 6900
Reputation: 512
I've tried to reproduce what you're seeing, using the following code, but the date value is being returned in the Get
call as expected:
string indexName = "so-27927069";
// --- create index ---
client.CreateIndex(cid => cid.Index(indexName));
Console.WriteLine("created index");
// --- define map ---
client.Map<MyType>(m => m
.Index(indexName)
.Type("myType")
.MapFromAttributes());
Console.WriteLine("set mapping");
// ---- index -----
client.Index<MyType>(
new MyType
{
DateToBeUsed = new DateTime(2012, 5, 21, 9, 51, 34, 73)
.ToString("yyyy-MM-ddThh:mm:ss.fff"),
ElasticId = "2"
},
i => i
.Index(indexName)
.Type("myType")
.Id(2)
);
Console.WriteLine("doc indexed");
// ---- get -----
var doc = client.Get<MyType>(i => i
.Index(indexName)
.Type("myType")
.Id(2)
);
Console.WriteLine();
Console.WriteLine("doc.Source.DateToBeUsed: ");
Console.WriteLine(doc.Source.DateToBeUsed);
Console.WriteLine();
Console.WriteLine("doc.RequestInformation.ResponseRaw: ");
Console.WriteLine(Encoding.UTF8.GetString(doc.RequestInformation.ResponseRaw));
I'm seeing the following result as output:
created index
set mapping
doc indexed
doc.Source.DateToBeUsed:
2012-05-21T09:51:34.073
doc.RequestInformation.ResponseRaw:
{"_index":"so-27927069","_type":"myType","_id":"2","_version":1,"found":true,"_source":{
"ElasticId": "2",
"DateToBeUsed": "2012-05-21T09:51:34.073"
}}
(Watching the traffic via Fiddler, I'm seeing an exact match between the ResponseRaw
value and the payload of the response to the Get
request.)
I'm on Elasticsearch version 1.5.2 and NEST version 1.6.0. (Maybe the issue you were seeing was fixed sometime in the interim....)
Upvotes: 2