I am an ES noob. Started exploring ES recently.
I have an index called Products. Each product has createdAT dateTime field. I want to write a query that will return results based on 2 conditions.
- Consider products that were created between 1st September 2023 and 10th September 2023.
- Return only the products that were created between 6 AM to 10 AM or between 4 PM to 10 PM on all the days mentioned in condition 1.
To realize the 2nd condition, I was trying to use the painless script.
var searchResponse = await _client.SearchAsync<ProductModel>(s => s.Query(q => q.Bool(b => b.Must(m =>
m.DateRange(r => r.Field(f => f.CreatedDateTime)
.GreaterThanOrEquals(startDateTz) //1st Sep 2023
.LessThanOrEquals(endDateTz) //10th Sep 2023
))
.Filter(f => f.Script(sn =>
sn.Name("hour_filter")
.Script(sc => sc
.Source(
"if(doc.createdDateTime.value.getHour() >= params.s1 && doc.createdDateTime.value.getHour() <= params.e1) return true;if (params.containsKey(s2) && params.containsKey(e2)) { if(doc.createdDateTime.value.getHour() >= params.s2 && doc.createdDateTime.value.getHour() <= params.e2) return true;else return false;")
.Params(AddParams(adjustedStartAndEndTimes)))))))
//adjustedStartAndEndTimes has the time spans s1 - 6 AM, e1 - 10 AM, s2 - 4 PM, e2 - 10 PM.
This is not working. Any pointers in realizing this would be really helpful.