# .NET setting field as Date

**URL:** https://discuss.elastic.co/t/net-setting-field-as-date/126947
**Category:** Elasticsearch
**Created:** [April 5, 2018, 2:13pm UTC](https://discuss.elastic.co/t/net-setting-field-as-date/126947 "2018-04-05T14:13:18Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Neal\_Walters](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/neal_walters/32/29581_2.png) [@Neal\_Walters](https://discuss.elastic.co/u/Neal_Walters)
#### Post date: [April 5, 2018, 2:13pm UTC](https://discuss.elastic.co/t/net-setting-field-as-date/126947/1 "2018-04-05T14:13:18Z")

</div>

I'm trying to define the field createdDate below as a datetime, using .NET:  
 ![Kibana_CreatedDate](https://us1.discourse-cdn.com/elastic/original/3X/c/b/cbc147edd342c235e5d0e38f8377b322b645b242.jpg)

Here's my existing code; had trouble finding any examples for the .NET classes.  
Maybe my date format needs to have the milliseconds on it?

```
                 CreateIndexRequest request = new CreateIndexRequest(index.ToString().ToLower());
                 TypeMapping typeMapping = new TypeMapping();
                 typeMapping.DateDetection = false;
                 request.Mappings = new Mappings();
                 request.Mappings.Add("object", typeMapping);

                 TypeMapping dateTypeMapping = new TypeMapping();
                 dateTypeMapping.DateDetection = true; 
                 string[] formats = new string[1]; 
                 formats[0] = "YYYY-MM-DD HH:MM:SS"; 
                 dateTypeMapping.DynamicDateFormats = formats; 
                 request.Mappings.Add("createdDate", dateTypeMapping );
                 shared.CreateIndex(request);

```

If I used the field typeMapping instead of the newDateTypeMapping that I added, it was being stored as "t" (text) and was not sortable in Kibana.

I tried to simpify, added a new "sortDate" with an easier date/time.

```
                 CreateIndexRequest request = new CreateIndexRequest(index.ToString().ToLower());
                 TypeMapping typeMapping = new TypeMapping();
                 typeMapping.DateDetection = false;
                 request.Mappings = new Mappings();
                 request.Mappings.Add("object", typeMapping);

                 TypeMapping dateTypeMapping = new TypeMapping();
                 dateTypeMapping.DateDetection = true; 
                 string[] formats = new string[1];
                 //2018-04-05T09:11:29.5267304-05:00 createdDate
                 //yyyy-MM-dd HH:mm:ss sortDate 
                 formats[0] = "yyyy-MM-dd HH:mm:ss"; 
                 dateTypeMapping.DynamicDateFormats = formats; 

                 request.Mappings.Add("createdDate", typeMapping );
                 request.Mappings.Add("sortDate", dateTypeMapping);
                 shared.CreateIndex(request);

```

It's showing up as a ? for data type.  
This is how dates were set in C#:

```
                Document<string> document = new Document<string>
                {
                    Message = message,
                    CreatedDate = DateTime.Now,
                    SortDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    TransactionType = index
                };
```

---

<div class="post-metadata">

### Author: ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)
#### Post date: [April 6, 2018, 8:07am UTC](https://discuss.elastic.co/t/net-setting-field-as-date/126947/2 "2018-04-06T08:07:38Z")

</div>

With NEST, you typically represent a JSON document in Elasticsearch with a Plain Old CLR Object (POCO) in your .NET application.

Assuming you have a document that looks something like the following, based on the fields you have in Kibana

```auto
public class MyDocument
{
    public DateTime CreatedDate {get;set;}
    public string Message {get;set;}
    public string TransactionType {get;set;}
}

```

creating the index along with the mapping would be as follows

```auto
var client = new ElasticClient();

var createIndexResponse = client.CreateIndex("my-index", c => c
    .Mappings(m => m
        .Map<MyDocument>(mm => mm.AutoMap())
    )
);

```

which would generate the following request to Elasticsearch

```javascript
PUT http://localhost:9200/my-index
{
  "mappings": {
    "mydocument": {
      "properties": {
        "createdDate": {
          "type": "date"
        },
        "message": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "transactionType": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

```

take a look at [automapping](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/auto-map.html) for more details, and other [mapping documentation](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/mapping.html) to see how you can control mapping to suit your needs.

---

<div class="post-metadata">

### Author: ![Neal\_Walters](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/neal_walters/32/29581_2.png) [@Neal\_Walters](https://discuss.elastic.co/u/Neal_Walters)
#### Post date: [April 6, 2018, 3:00pm UTC](https://discuss.elastic.co/t/net-setting-field-as-date/126947/3 "2018-04-06T15:00:43Z")

</div>

Thanks. Your post helped me finalize realize that I was trying to format a date that was already in Elasticsearch, and that I had the createdDate was already defined as a DateTime,

I'm trying to improve some code that I inherited. So you have one person (me) not knowing what they are doing, updating code of maybe another person that didn't know what they were doing. I'm just in experimentation mode, seeing if I can improve. No official time to work on this yet.

The Document class has a generic type. I think the reason this was done was so that Elasticsearch could handle JSON in the message.

```
public class Document<T>
{
    public T Message { get; set; }
    public DateTime CreatedDate { get; set; }
    public string TransactionType { get; set; }
}

```

I tried making it non-generic, and defining Message as string (top example using your automapping, other example is from the original program):

 ![Kibana_Two_Examples](https://us1.discourse-cdn.com/elastic/original/3X/b/2/b23a166b5ef86e4842f47a13013b02fa07d199b6.jpg)  
Basically same except the serialization was slightly different, which I can change.  
Seem like once I saw Kibana show message.firstname, and message.lastname as a column name; but now I forgot which iteration that was.

Then i tried making it non-generic and defining Message as an object, but got this error:

> {Type: mapper\_parsing\_exception Reason: "object mapping for [message] tried to parse field [message] as object, but found a concrete value"}

You can see the date as the "t" for text beside it. I'd like to keep the current code as close as possible. That's why I tried adding the dateTypeMapping.

So even with automap, should I expect to see "d" by the createdDate?

Thanks,  
Neal

---

<div class="post-metadata">

### Author: ![Neal\_Walters](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/neal_walters/32/29581_2.png) [@Neal\_Walters](https://discuss.elastic.co/u/Neal_Walters)
#### Post date: [April 6, 2018, 3:20pm UTC](https://discuss.elastic.co/t/net-setting-field-as-date/126947/4 "2018-04-06T15:20:44Z")

</div>

From Kibana - Dev Tools, I ran  
GET local-neal-test-kibananew2c-2018.04.06  
which give sthe following - showing that createdDate is actually a date.

So now back to my original quandry - why can't I sort by it?

> [@How sort by date/time on Kibana Discover](https://discuss.elastic.co/t/how-sort-by-date-time-on-kibana-discover/126804):
>
> Below is an example of what I am seeing: I dont' see the "time" choice as shown in this thread: I'd like to sort by descending date/time ]the field createdDate (even by default if possible). It's one of the fields in every message: [Kibana\_Sort\_Fields] Thanks, Neal

```
> {
> "local-neal-test-kibananew2c-2018.04.06": {
> "aliases": {},
> "mappings": {
> "document2": {
> "properties": {
> "createdDate": {
> "type": "date"
> },
> "message": {
> "type": "text",
> "fields": {
> "keyword": {
> "type": "keyword",
> "ignore_above": 256
> }
> }
> },
> "transactionType": {
> "type": "text",
> "fields": {
> "keyword": {
> "type": "keyword",
> "ignore_above": 256
> }
> }
> }
> }
> }
> },
> "settings": {
> "index": {
> "creation_date": "1523026621059",
> "number_of_shards": "5",
> "number_of_replicas": "1",
> "uuid": "3DzKTpm7TYKk_u35pj3eBA",
> "version": {
> "created": "6000099"
> },
> "provided_name": "local-neal-test-kibananew2c-2018.04.06"
> }
> }
> }
> }

```

---

<div class="post-metadata">

### Author: ![Neal\_Walters](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/neal_walters/32/29581_2.png) [@Neal\_Walters](https://discuss.elastic.co/u/Neal_Walters)
#### Post date: [April 6, 2018, 4:44pm UTC](https://discuss.elastic.co/t/net-setting-field-as-date/126947/5 "2018-04-06T16:44:02Z")

</div>

Well, "I'll be switched" (as they used to say).

If I create an index pattern on "local-neal-test-kibananew4a-_", it works.  
I was using an index pattern on "local-neal-_" it doesn't, even though all the data is displayed. I presume that's because not all messages in local-neal-\* have the exact same type.

When I use the management tab to create the index pattern: local-neal-test-kibananew4a-\*  
it pops up the "Time Filter field name" box":

![Kibana_Create_IndexPatternSpecific](https://us1.discourse-cdn.com/elastic/original/3X/e/0/e089e51ed56361ecf3f9a9036f35f4a066c02dd4.jpg)

I had never seen that before. I guess I need to read up on what index patterns really do.  
I thought Kibana would be super user-friendly, but that are apparently tricks to be learned.

We have naming standards for our indexes. What I was hoping to do was watch the "trace" of my business process across different indexes and be able to get them in date/time sequence, since they all had the same createdDate field. Apparently this won't work??

Neal

---

<div class="post-metadata">

### Author: ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)
#### Post date: [April 9, 2018, 3:53am UTC](https://discuss.elastic.co/t/net-setting-field-as-date/126947/6 "2018-04-09T03:53:07Z")

</div>

`message` is a string, based on the mapping response provided. Looking at `Document<T>`, I guess the system was designed to handle different `T`, where `T` is some other type into which the JSON can be deserialized.

> [@Neal\_Walters](#):
>
> You can see the date as the "t" for text beside it. I'd like to keep the current code as close as possible. That's why I tried adding the dateTypeMapping.
> 
> So even with automap, should I expect to see "d" by the createdDate?

You would yes, if the index pattern is created after creating the index. Note that you cannot change the existing mappings in an existing index (save for a few exceptions, such as making a `text` field a multi-field), so if documents have been indexed with the wrong mapping, you would need to reindex the documents again (using the reindex API, if you have the `_source` for each document in Elasticsearch).

I'm not sure what happens in Kibana if you have multiple indices that match an index pattern, where those indices have a field with the same name but mapped differently.

> [@Neal\_Walters](#):
>
> When I use the management tab to create the index pattern: local-neal-test-kibananew4a-\*
> 
> it pops up the "Time Filter field name" box":

Glad you got this part sorted 👍

---

<div class="post-metadata">

### Author: ![Neal\_Walters](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/neal_walters/32/29581_2.png) [@Neal\_Walters](https://discuss.elastic.co/u/Neal_Walters)
#### Post date: [April 9, 2018, 3:52pm UTC](https://discuss.elastic.co/t/net-setting-field-as-date/126947/7 "2018-04-09T15:52:03Z")

</div>

> I'm not sure what happens in Kibana if you have multiple indices that match an index pattern, where those indices have a field with the same name but mapped differently.

createdDate is never mapped differently.  
Neal

---

<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: [May 7, 2018, 3:52pm UTC](https://discuss.elastic.co/t/net-setting-field-as-date/126947/8 "2018-05-07T15:52:31Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
