首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Elasticsearch c# datetime获取DateTime默认值

Elasticsearch c# datetime获取DateTime默认值
EN

Stack Overflow用户
提问于 2019-06-20 04:32:26
回答 1查看 207关注 0票数 1

当我试图从elasticsearch中获取数据时,我目前正面临着一个问题。

我有一个保存的时间戳值,如下所示:

代码语言:javascript
复制
Timestamp = DateTime.UtcNow;

在我的对象中,时间戳属性如下所示

代码语言:javascript
复制
public DateTime Timestamp { get; }

使用Kibana查看数据时,时间戳具有以下值:

时间戳:2019年6月19日16:29:24.997

当我尝试使用以下代码检索数据时:

代码语言:javascript
复制
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);

然后像这样循环遍历:

代码语言:javascript
复制
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);
        }

我的所有时间戳都有以下值:

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

查看时间戳的映射,如下所示:

代码语言:javascript
复制
 "timestamp": {
          "type": "date"
        }

我不明白为什么会发生这样的事情。也许我遗漏了一些配置?

编辑:

在请求时:

我使用的是NEST版本6.6.0和elasticsearch 6.6.2

完整的数据收集方法:

代码语言:javascript
复制
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;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-20 18:42:36

所以我又看了一遍我的模型

代码语言:javascript
复制
public DateTime Timestamp { get; }

它没有二传手。添加一个解决了这个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56675444

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档