# Problem in DateRange query in C#

**URL:** <https://discuss.elastic.co/t/problem-in-daterange-query-in-c/135239>\
**Category:** Elasticsearch\
**Created:** [June 9, 2018, 8:12pm UTC](https://discuss.elastic.co/t/problem-in-daterange-query-in-c/135239 "2018-06-09T20:12:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![r1go](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/r1go/32/30491_2.png) [@r1go](https://discuss.elastic.co/u/r1go)\
**Post date:** [June 9, 2018, 8:12pm UTC](https://discuss.elastic.co/t/problem-in-daterange-query-in-c/135239/1 "2018-06-09T20:12:21Z")

</div>

Hi there! im very new to Elastic Search and just need some help.

Im trying to performe an complex query in c# with my own list of "Indexes", "Dateranges" and "Terms" as well but with out success..

```
result = client.Search<Event>(e => e.Index(customIndex)
                                                .Query(q => 
                                                    q.DateRange(dr => dr
                                                                .Boost(1.1)
                                                                .Field("DateTime")
                                                                .GreaterThanOrEquals(dateFrom)
                                                                .LessThanOrEquals(dateTo)
                                                                .Format("dd/MM/yyyy")
                                                            ) &&
                                                    q.Terms(mt => mt.
                                                    Field("RuleID")
                                                    .Terms(legacyIDs)
                                                    )
                                                )
                                                .From(pageSize * page)
                                                .Size(pageSize)
                                                .Sort(ss => ss.Descending("Date")
                                                .Field(fie => fie.UnmappedType(FieldType.Date)))
                                                );

```

Do i Missed something?  
The NEST Client it says everythings went well, but the result its different.

Its my approach correct? i think the problem it is in the daterange ☹

thanks in advance

---

<div class="post-metadata">

**Author:** ![r1go](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/r1go/32/30491_2.png) [@r1go](https://discuss.elastic.co/u/r1go)\
**Post date:** [June 9, 2018, 8:15pm UTC](https://discuss.elastic.co/t/problem-in-daterange-query-in-c/135239/2 "2018-06-09T20:15:40Z")

</div>

pageSize && page are passed correctly, so i thinks theres no problem at all 🙂  
Im just trying to focus on the dateRange if its something wrong

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 9, 2018, 8:39pm UTC](https://discuss.elastic.co/t/problem-in-daterange-query-in-c/135239/3 "2018-06-09T20:39:33Z")

</div>

Not sure how it works in Nest but I think you should put your queries within a bool query with filter or must clauses.

---

<div class="post-metadata">

**Author:** ![r1go](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/r1go/32/30491_2.png) [@r1go](https://discuss.elastic.co/u/r1go)\
**Post date:** [June 9, 2018, 8:52pm UTC](https://discuss.elastic.co/t/problem-in-daterange-query-in-c/135239/4 "2018-06-09T20:52:24Z")

</div>

Thanks for your help. now im working with something like that...

```
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "DateTime": {
              "gte": "2018-06-09T00:00:00",
              "format": "yyyymmdd"
            }
          }
        }
      ]
    }
  }
}

```

but im not sure at all...

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 10, 2018, 3:04am UTC](https://discuss.elastic.co/t/problem-in-daterange-query-in-c/135239/5 "2018-06-10T03:04:35Z")

</div>

`gte` and `format` don't match here. Which looks strange to me.

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

**Author:** ![r1go](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/r1go/32/30491_2.png) [@r1go](https://discuss.elastic.co/u/r1go)\
**Post date:** [June 10, 2018, 4:14am UTC](https://discuss.elastic.co/t/problem-in-daterange-query-in-c/135239/6 "2018-06-10T04:14:10Z")

</div>

Thanks again for your reply.  
What im trying to performed its very simple, just querying from my "Event" Documents that match my needs which are:

```
.Index('custom_Index_array_of_strings_to_look_for')
.Query(
           Events.DateField ->GreaterThan ("DateToPrivided")
           Events.Datefield -> LowerThanOrEqueals ("DateFromProvided")
&&
            Events.Rule that match with my custom "LegacysIdsProvided" inside a Terms Query
)

```

and pretty much thats it.

now im digging with this link: [https://www.elastic.co/guide/en/elasticsearch/reference/6.x/query-filter-context.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.x/query-filter-context.html) and i think thats the buttom key of the all problem, what do you think?

---

<div class="post-metadata">

**Author:** ![r1go](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/r1go/32/30491_2.png) [@r1go](https://discuss.elastic.co/u/r1go)\
**Post date:** [June 10, 2018, 4:19pm UTC](https://discuss.elastic.co/t/problem-in-daterange-query-in-c/135239/7 "2018-06-10T16:19:27Z")

</div>

I'been reading the all "n00bies" section in order to help myself to write properly my query to everybody knows what i am trying to do.

```
GET index/_search
result = client.Search<Event>(s => s
                            .Query(q => q
                                .Bool(b => b
                                    .Filter(f => f
                                        .Terms(t => t
                                            .Field("RuleID")
                                            .Terms(legacyIDs)
                                            )
                                        &&
                                        f.DateRange(dr => dr
                                            .Field("DateTime")
                                            .GreaterThanOrEquals(new DateTime().AddDays(-1))
                                        )
                                    )
                                )
                            )
                        );

```

Reading the differences between ["Context Querys" and "Filter Querys"](https://www.elastic.co/guide/en/elasticsearch/reference/6.x/query-filter-context.html) allow me to understand what i was doing wrong, but unfortunately theres somethings missing up there with my query.  
Now i think its quite clear, but if you dont understand it please let me know 🙂

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [June 14, 2018, 12:34am UTC](https://discuss.elastic.co/t/problem-in-daterange-query-in-c/135239/8 "2018-06-14T00:34:48Z")

</div>

If I understand correctly, you're looking to have both the `terms` query and `range` query executed in the context of filter clauses of a `bool` query? If that's the case, your query is _almost_ correct, but

1. `.Filter(...)` takes a collection of lambda expressions to express the queries
2. `new DateTime().AddDays(-1)` is an unrepresentable date; it is one day less than the smallest representable date in .NET (`1/01/0001 12:00:00 AM`). I think you want `DateTime.Now.AddDays(-1)` here

```auto
var legacyIDs = new [] { "1", "2" };

var result = client.Search<Event>(s => s
    .Query(q => q
        .Bool(b => b
            .Filter(f => f
                .Terms(t => t
                    .Field("RuleID")
                    .Terms(legacyIDs)
                ), f => f               
                .DateRange(dr => dr
                    .Field("DateTime")
                    .GreaterThanOrEquals(DateTime.Now.AddDays(-1))
                )
            )
        )
    )
);

```

this produces the query

```json
{
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "RuleID": [
              "1",
              "2"
            ]
          }
        },
        {
          "range": {
            "DateTime": {
              "gte": "2018-06-13T10:29:29.6995906+10:00"
            }
          }
        }
      ]
    }
  }
}

```

With [operator overloading](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/bool-queries.html), you can create bool queries even more succinctly. Here's the same query but with more concise syntax

```auto
var result = client.Search<Event>(s => s
    .Query(q => +q
        .Terms(t => t
            .Field("RuleID")
            .Terms(legacyIDs)
        ) && +q          
        .DateRange(dr => dr
            .Field("DateTime")
            .GreaterThanOrEquals(DateTime.Now.AddDays(-1))
        )
    )
);

```

---

<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:** [July 12, 2018, 12:34am UTC](https://discuss.elastic.co/t/problem-in-daterange-query-in-c/135239/9 "2018-07-12T00:34:57Z")

</div>

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