1 回答
TA贡献1818条经验 获得超3个赞
该DateRange查询需要FieldElasticsearch 中的字段的值才能运行。如果未为此提供值,NEST 会认为查询是无条件的,并且不会将其序列化为发送的查询的一部分。
例如,给定
var term = "term";
var to = DateTime.Now;
var from = to.AddDays(-7);
您当前的查询序列化为
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "term"
}
}
]
}
}
}
如果Field添加
var result = client.Search<Ticket>(s => s
.Query(q => q
.Bool(b => b
.Must(ms => ms
.QueryString(qs => qs
.Query(term)
)
)
.Filter(f => f
.Bool(bb => bb
.Must(ms => ms
.DateRange(dr => dr
.Field(df => df.CreateDate)
.GreaterThanOrEquals(from)
.LessThanOrEquals(to)
)
)
)
)
)
)
);
这现在序列化为
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "term"
}
}
],
"filter": [
{
"bool": {
"must": [
{
"range": {
"createDate": {
"gte": "2018-07-17T12:20:02.8659934+10:00",
"lte": "2018-07-24T12:20:02.8659934+10:00"
}
}
}
]
}
}
]
}
}
}
对查询使用运算符重载,这可以写得更简洁
var result = client.Search<Ticket>(s => s
.Query(q => q
.QueryString(qs => qs
.Query(term)
) && +q
.DateRange(dr => dr
.Field(df => df.CreateDate)
.GreaterThanOrEquals(from)
.LessThanOrEquals(to)
)
)
);
序列化为
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "term"
}
}
],
"filter": [
{
"range": {
"createDate": {
"gte": "2018-07-17T12:21:50.2175114+10:00",
"lte": "2018-07-24T12:21:50.2175114+10:00"
}
}
}
]
}
}
}
- 1 回答
- 0 关注
- 168 浏览
添加回答
举报