public IEnumerable <HomeEvent> GetEventsWindow(DateTime endDateTime) { var match = new MatchPhraseQuery { Field = new Field("doc"), Query = "event" }; var rangeQuery = new DateRangeQuery { Field = "@timestamp", LessThanOrEqualTo = endDateTime.ToUniversalTime(), GreaterThanOrEqualTo = endDateTime.ToUniversalTime().AddSeconds(-config.EventsWindowsSeconds), }; var boolQuery = new BoolQuery { Must = new List <QueryContainer> { match, rangeQuery } }; var searchRequest = new SearchRequest(Indices.Parse("history-*")) { Query = boolQuery, Sort = new List <ISort>() { new SortField { Field = new Field("@timestamp"), Order = SortOrder.Descending } } }; var result = elastic .Request <ElasticSearchEvent>(searchRequest) .Select(ConvertToLocalDateTime) .ToList(); result.ForEach(r => log.Debug(r.ToString())); return(result.Select(r => new HomeEvent { DateTime = r.timestamp, Id = r.Id, Sensor = r.sensor.display, Status = r.status, SensorType = r.sensor.type, })); }