I am trying to implement a search that will filter by both the given keyword, as well as a given date range using the Nest client.
var searchResponse = client.Search<mdl.Event>(s => s
.From(0)
.Query(q => q
.Match(m => m
.Query(search.Text))));
This returns as expected, but I haven't been able to figure out a way to filter by date with it. Is there a simple addition like .Date(d => d) or another way I can make this happen?
CodePudding user response:
You can do that with the DateRange
function (standing for the range
DSL query). You need to combine both constraints using bool queries
var searchResponse = client.Search<mdl.Event>(s => s
.From(0)
.Query(q =>
q.Match(m => m.Query(search.Text)) &&
q.DateRange(r => r
.Field(f => f.StartedOn)
.GreaterThanOrEquals(new DateTime(2017, 01, 01))
.LessThan(new DateTime(2018, 01, 01))
)
)