# DateRange search using Elastic.Clients.Elasticsearch nuget package

**URL:** https://discuss.elastic.co/t/daterange-search-using-elastic-clients-elasticsearch-nuget-package/365986
**Category:** Elastic Search
**Created:** [September 3, 2024, 9:41pm UTC](https://discuss.elastic.co/t/daterange-search-using-elastic-clients-elasticsearch-nuget-package/365986 "2024-09-03T21:41:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![cteaguelogitix](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@cteaguelogitix](https://discuss.elastic.co/u/cteaguelogitix)
#### Post date: [September 3, 2024, 9:41pm UTC](https://discuss.elastic.co/t/daterange-search-using-elastic-clients-elasticsearch-nuget-package/365986/1 "2024-09-03T21:41:07Z")

</div>

I would like to convert the following into c# using the Elastic.Clients.Elasticsearch nuget package (version 8.15.4), but I cannot find an example. I was able to do a search for an exact match by following the documentation on ([CRUD usage examples | Elasticsearch .NET Client [8.9] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/net-api/8.9/examples.html))

```auto
GET /IndexName/_search
{
  "query": {
    "range": {
      "@timestamp": {
        "gte": "2024-09-03T15:12:52.2301336+00:00",
        "lte": "2024-09-03T15:12:54.2301336+00:00"
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![cteaguelogitix](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@cteaguelogitix](https://discuss.elastic.co/u/cteaguelogitix)
#### Post date: [September 3, 2024, 10:13pm UTC](https://discuss.elastic.co/t/daterange-search-using-elastic-clients-elasticsearch-nuget-package/365986/2 "2024-09-03T22:13:22Z")

</div>

I figured it out

```auto
public class Searcher
    {
        private static readonly string ElasticSearchUri
            = "https://Redacted.azure.elastic-cloud.com";
        private ElasticsearchClient client = new ElasticsearchClient(
            "Redacted"
            , new ApiKey("Redacted"));
        private const string logIndexId = "Redacted";
        private const string indexName = "Redacted";

        public async Task SearchTimeInterval()
        {

            // Define the timestamp and calculate the range
            var timestamp = new DateTime(2024, 9, 3, 15, 12, 53, 230, DateTimeKind.Utc);
            var startTime = timestamp.AddSeconds(-1);
            var endTime = timestamp.AddSeconds(1);

            // Create the range query
            var rangeQuery = new DateRangeQuery (new Field("@timestamp"))
            {
                Gt = startTime,
                Lt = endTime
            };

            var request = new SearchRequest(indexName)
            {
                From = 0,
                Size = 10,
                Query = rangeQuery
            };

            var response = await client.SearchAsync<object>(request);

            if (response.IsValidResponse)
            {
                foreach (var doc in response.Documents)
                {
                    Console.WriteLine(doc);
                    Console.WriteLine();
                }
            }
        }
    }

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [October 1, 2024, 10:13pm UTC](https://discuss.elastic.co/t/daterange-search-using-elastic-clients-elasticsearch-nuget-package/365986/3 "2024-10-01T22:13:36Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
