Elasticsearch c# datetime 获取 datetime 默认值



当我尝试从中获取数据时,我的弹性搜索目前面临问题。

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

Timestamp = DateTime.UtcNow;

时间戳属性在我的对象中看起来像这样

public DateTime Timestamp { get; }

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

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

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

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)
            {
                resultList.Add(doc.Source);
            }
            searchResponse = await elastic.ScrollAsync<SupplierPricelistStatistic>("10m", searchResponse.ScrollId);
        }

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

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

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

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

我不知道为什么会发生这种情况。也许我在某处缺少一些配置?

编辑:

根据要求:

我使用的是 NEST 版本 6.6.0 和弹性搜索 6.6.2

完整的数据收集方法:

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;
    }

所以我只是再次查看了我的模型

public DateTime Timestamp { get; }

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

最新更新