搜索日期范围内的工单字段

我有以下型号


public class Ticket

{

    public string Id { get; set; }


    public string Question { get; set; }


    public DateTime CreateDate { get; set; }


    public DateTime ClosedDate { get; set; }


    public int Votes { get; set; }

}

我正在使用 ElasticSearch Nest 客户端来搜索任何字段包含日期范围内特定文本的票证。


我尝试了以下方法:


        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.GreaterThanOrEquals(from).LessThanOrEquals(to))

            ))))));

无论指定的时间如何,它都会返回所有票证。


它也只搜索完整的单词,而我想在文本中搜索单词的任何部分。


有任何想法吗?


绝地无双
浏览 169回答 1
1回答

泛舟湖上清波郎朗

该DateRange查询需要FieldElasticsearch 中的字段的值才能运行。如果未为此提供值,NEST 会认为查询是无条件的,并且不会将其序列化为发送的查询的一部分。例如,给定var term = "term";var to = DateTime.Now;var from = to.AddDays(-7);您当前的查询序列化为{&nbsp; "query": {&nbsp; &nbsp; "bool": {&nbsp; &nbsp; &nbsp; "must": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "query_string": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "query": "term"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; }&nbsp; }}如果Field添加var result = client.Search<Ticket>(s => s&nbsp; &nbsp; .Query(q => q&nbsp; &nbsp; &nbsp; &nbsp; .Bool(b => b&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Must(ms => ms&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .QueryString(qs => qs&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Query(term)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Filter(f => f&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Bool(bb => bb&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Must(ms => ms&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .DateRange(dr => dr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Field(df => df.CreateDate)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .GreaterThanOrEquals(from)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .LessThanOrEquals(to)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; ));这现在序列化为{&nbsp; "query": {&nbsp; &nbsp; "bool": {&nbsp; &nbsp; &nbsp; "must": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "query_string": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "query": "term"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; "filter": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "bool": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "must": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "range": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "createDate": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "gte": "2018-07-17T12:20:02.8659934+10:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "lte": "2018-07-24T12:20:02.8659934+10:00"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; }&nbsp; }}对查询使用运算符重载,这可以写得更简洁var result = client.Search<Ticket>(s => s&nbsp; &nbsp; .Query(q => q&nbsp; &nbsp; &nbsp; &nbsp; .QueryString(qs => qs&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Query(term)&nbsp; &nbsp; &nbsp; &nbsp; ) && +q&nbsp; &nbsp; &nbsp; &nbsp; .DateRange(dr => dr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Field(df => df.CreateDate)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .GreaterThanOrEquals(from)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .LessThanOrEquals(to)&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; ));序列化为{&nbsp; "query": {&nbsp; &nbsp; "bool": {&nbsp; &nbsp; &nbsp; "must": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "query_string": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "query": "term"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; "filter": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "range": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "createDate": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "gte": "2018-07-17T12:21:50.2175114+10:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "lte": "2018-07-24T12:21:50.2175114+10:00"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; }&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP