尝试使用时间戳字段按范围筛选数据。查询如下:
var scanResults = await _elasticClient.SearchAsync<OemCatalogModel>(s => s.Index(indexName)
.Source(sf => sf
.Includes(i => i
.Fields(
f => f.Event,
f => f.MemberId,
f => f.IsInternalUser,
f => f.IndexName,
f => f.IsMobile,
f => f.VinNumber,
f => f.Timestamp
)
)
)
.Query(q => q.Range(p => p.Field<Timestamp>(t=>t.Timestamp)
.GreaterThanOrEquals(sd)
.LessThanOrEquals(ed)
))
.Size(10000).Scroll("60s"));
字符串
从DatePicker获取startDate
和endDate
作为DateTimeOffset,并像下面这样初始化sd
和ed
:
var sd = startDate.Value.ToUnixTimeSeconds();
var ed = endDate.Value.ToUnixTimeSeconds();
型
时间戳字段在Map模型中看起来如下
[Date(Name = "timestamp")]
public Int64 Timestamp { get; set; }
型
此查询引发“parse_exception”异常:
"reason" : {
"type" : "parse_exception",
"reason" : "failed to parse date field [1.6540308E9] with format [epoch_second]: [failed to parse date field [1.6540308E9] with format [epoch_second]]",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "failed to parse date field [1.6540308E9] with format [epoch_second]",
"caused_by" : {
"type" : "date_time_parse_exception",
"reason" : "date_time_parse_exception: Failed to parse with all enclosed parsers"
}
}
}
型
NEST生成的ElasticSearch查询如下:
{“查询”:{“range”:{“timestamp”:{“gte”:1654030800.0,“lte”:1654203599.0}}},“size”:10000,"_source”:{“includes”:[“event”,“member_id”,“is_internal_user”,“indexName”,“is_移动的”,“vin_number”,“timestamp”]}}
这个过滤器不工作,因为范围参数添加.0
.我检查了这个witout .0
和它的作品.例如结果为_source
看起来像:
"_source" : {
"member_id" : 69500,
"is_mobile" : false,
"event" : "close_unit_card",
"is_internal_user" : false,
"timestamp" : 1654066236
}
型
如何正确Map范围过滤器与时间戳?可能我必须使用DataRange
?如果是,如何将sd
和ed
转换为ElasticSearch中使用的时间戳?
也尝试执行以下操作,但得到不同的异常:
查询类似于:
.Query(q => q.DateRange(p => p.Field(t=>t.Timestamp)
.GreaterThanOrEquals(DateTime.Parse(startDate.Value.ToString()))
.LessThanOrEquals(DateTime.Parse(endDate.Value.ToString()))))
型
以下例外情况上涨:
"reason" : {
"type" : "parse_exception",
"reason" : "failed to parse date field [2022-05-31T21:00:00Z] with format [epoch_second]: [failed to parse date field [2022-05-31T21:00:00Z] with format [epoch_second]]",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "failed to parse date field [2022-05-31T21:00:00Z] with format [epoch_second]",
"caused_by" : {
"type" : "date_time_parse_exception",
"reason" : "date_time_parse_exception: Failed to parse with all enclosed parsers"
}
}
}
型
2条答案
按热度按时间b1payxdu1#
奇怪的解决方案,但工作:
初始化开始和结束日期:
字符串
DateRange看起来像:
型
模型看起来像:
型
31moq8wy2#
通过DateRange,您可以使用DateTime结构:
字符串