当我试图从Elasticsearch中获取数据时,我目前遇到问题。
我有一个时间戳值,其保存方式如下:
Timestamp = DateTime.UtcNow;
Timestamp属性在我的对象中看起来像这样
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和elasticsearch 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;
}
答案 0 :(得分:1)
所以我只是再次查看了我的模型
public DateTime Timestamp { get; }
它没有二传手。添加一个解决了该问题。