Christian A
Christian A

Reputation: 501

Elasticsearch c# datetime gets datetime default value

I'm currently facing an issue with my elasticsearch, when i'm trying to fetch data from it.

I have a timestamp value that gets saved like the following:

Timestamp = DateTime.UtcNow;

And the Timestamp property is looking like this in my object

public DateTime Timestamp { get; }

When using Kibana to look at the data the timestamp has the following value:

timestamp:June 19th 2019, 16:29:24.997

When I try to retrieve the data with the following code:

var searchResponse = await elastic.SearchAsync<SupplierPricelistStatistic>(s => s
                .From(0)
                .Take(2000)
                .Query(q => +q
                    .Match(m => m
                        .Field(f => f.SupplierId)
                        .Query(id.ToString())
                    )
                )
                .Scroll("10m")
            ).ConfigureAwait(false);

And loop through it like this:

var resultList = new List<SupplierPricelistStatistic>();

        while (searchResponse.Documents.Any())
        {
            foreach (var doc in searchResponse.Hits)
            {
                resultList.Add(doc.Source);
            }
            searchResponse = await elastic.ScrollAsync<SupplierPricelistStatistic>("10m", searchResponse.ScrollId);
        }

All of my timestamps has the following value:

{01-01-0001 00:00:00}

Looking at the mapping for timestamp it looks like the following:

 "timestamp": {
          "type": "date"
        }

I can't figure out why this is happening. Maybe i'm missing some configuration somewhere?

EDIT:

Upon request:

I'm using NEST version 6.6.0 and elasticsearch 6.6.2

Complete method for collecting data:

public async Task <List<SupplierPricelistStatistic>> GetSupplierPricelistStatistic(Guid id, IElasticClient elastic, int year, string month)
        {
            var searchResponse = await elastic.SearchAsync<SupplierPricelistStatistic>(s => s
                .From(0)
                .Take(2000)
                .Query(q => +q
                    .Match(m => m
                        .Field(f => f.SupplierId)
                        .Query(id.ToString())
                    )
                )
                .Scroll("10m")
            ).ConfigureAwait(false);

        var resultList = new List<SupplierPricelistStatistic>();

        while (searchResponse.Documents.Any())
        {
            foreach (var doc in searchResponse.Hits)
            {
                var tempSupStat = doc.Source;
                DateTime dateTime;
                if (month != "0")
                {
                    int.TryParse(month, out int intMonth);
                    dateTime = new DateTime(year, intMonth, DateTime.Now.Day);
                    if (tempSupStat.Timestamp.Year == dateTime.Year && tempSupStat.Timestamp.Month == dateTime.Month)
                    {
                        resultList.Add(tempSupStat);
                    }
                }
                else
                {
                    dateTime = new DateTime(year, DateTime.Now.Month, DateTime.Now.Day);
                    if (tempSupStat.Timestamp.Year == dateTime.Year)
                    {
                        resultList.Add(tempSupStat);
                    }
                }
            }
            searchResponse = await elastic.ScrollAsync<SupplierPricelistStatistic>("10m", searchResponse.ScrollId);
        }

        return resultList;
    }

Upvotes: 1

Views: 631

Answers (1)

Christian A
Christian A

Reputation: 501

So i just looked over my model again

public DateTime Timestamp { get; }

It did not have a setter. Adding one fixed the issue.

Upvotes: 1

Related Questions