gpt4 book ai didi

c# - DateRange搜索在Elastic search NEST API中不起作用

转载 作者:行者123 更新时间:2023-12-03 00:48:48 32 4
gpt4 key购买 nike

我有一张日志记录表,我想按日期进行简单搜索。

例如,我想搜索01.06.2019 00:00:00(mm.DD.yyyy hh:mm:ss)之前的所有查询,然后编写了以下查询:

var query = client.Search<SearchEventDto>(s => s
.AllIndices()
.AllTypes()
.Query(q => q
.MatchAll() && +q
.DateRange(r =>r
.Field(f => f.timestamp)
.LessThanOrEquals(new DateTime(2019,06,01, 0, 0, 0))
)
)
);

我的Dto看起来像这样:
 public class SearchEventDto : IDto
{
[KendoColumn(Hidden = true, Editable = true)]
public string id { get; set; }

[KendoColumn(Order = 2, DisplayName = "Level")]
public string level { get; set; }
[KendoColumn(Order = 4, DisplayName = "Message")]
public string message { get; set; }

[KendoColumn(Hidden = true)]
public string host { get; set; }
[KendoColumn(Order = 3, DisplayName = "Source")]
public string src { get; set; }

[KendoColumn(Order = 1, DisplayName = "Timestamp", UIType = UIType.DateTime)]
public DateTime timestamp { get; set; }

[KendoColumn(Hidden = true)]
public DateTime time { get; set; }

}

不幸的是,它返回所有记录而不过滤任何内容。
我在哪里错了?

提前致谢!

PS:ES版本:6.7.0,NEST:6.8

PS:我已经将日志与Nlog集成在一起。因此,现在每天都会插入一个以日期为名称的新索引。这是219-06-28的映射(我正在使用@timestamp):
{
"logstash-2019-06-28": {
"mappings": {
"logevent": {
"properties": {
"@timestamp": {
"type": "date"
},
"host": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"level": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"message": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"src": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"time": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}

最佳答案

我将在评论中指出的内容作为答案发布,因为我认为可以进行一些改进以提高性能和可读性。

解:

从问题中查询的是.Field(f => f.timestamp),它已由NEST翻译为使用timestamp字段而不是@timestamp。更改.Field("@timestamp")即可解决此问题,因为这是索引映射中的正确字段名称。

{
"logstash-2019-06-28": {
"mappings": {
"logevent": {
"properties": {
"@timestamp": {
"type": "date"
},
..
}
}
}
}
}

我们还可以使用 timestamp属性标记 PropertyName属性,以告知NEST使用 @timestamp代替 timestamp作为名称
 public class SearchEventDto : IDto
{
[KendoColumn(Order = 1, DisplayName = "Timestamp", UIType = UIType.DateTime)]
[PropertyName("@timestamp")]
public DateTime timestamp { get; set; }

}

和查询
var query = client.Search<SearchEventDto>(s => s
.AllIndices()
.AllTypes()
.Query(q => q
.MatchAll() && +q
.DateRange(r =>r
.Field(f => f.timestamp)
.LessThanOrEquals(new DateTime(2019,06,01, 0, 0, 0))
)
)
);

也会工作得很好。

改进之处:

仅查询特定索引:
var query = client.Search<SearchEventDto>(s => s
.AllIndices()
.AllTypes()
..

通过使用 AllIndices(),我们告诉elasticsearch尝试从所有索引中收集文档,我们可以对其进行一些更改以仅查询带有日志数据的索引:
var query = client.Search<SearchEventDto>(s => s
.Index("logstash-*")
.Type("logevent")
..

将过滤器上下文用于日期范围过滤器:
.Query(q => q.Bool(b => b.Filter(f => f.DateRange(..))))

这样,您的查询应该会更快,因为它无需考虑计算搜索相关性得分。您可以阅读有关它的更多信息 here

希望能有所帮助。

关于c# - DateRange搜索在Elastic search NEST API中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56869624/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com