0

Please help me out with writing NEST query for below elastic DSL query

{
  "query": {
    "range": {
      "date_of_birth": {
        "format": "yyyy",
        "gte": "1990",
        "lte": "2020"
      }
    }
  }
}

This is what I have written as of now but I am not able to correct it.

var format = "yyyy";
var lowerBound = DateTime.Now.AddYears(-1);

var searchResponse = elasticClient.Search<PresentationDetails>(s => s
    .Index(indexAlias)
    .Type("presentation")
    .Size(1000)
    .Source(source => source
        .Includes(i => i
            .Fields(
                f => f.presentation_name
                )
            )
        )
    .Query(q => q
            .Range( r => r.Field("date_of_birth").GreaterThanOrEquals(lowerBound)
            )
        )
    .Scroll("20s")
    );
Richi Sharma
  • 175
  • 1
  • 13
  • Witch version of es do you use? On the last versions If date_of_birth is a date (and lowerBound too) you have to DateRange instead. Doest es return an error? Check searchResponse.debug – LeBigCat May 25 '22 at 15:15
  • i am using 5.6 version of es – Richi Sharma Jun 02 '22 at 14:49
  • 1
    Well as i asked, if you set date_of_birth as a date in your mappings, you need to use DateRange and not a Range (https://www.elastic.co/guide/en/elasticsearch/client/net-api/5.x/date-range-query-usage.html) – LeBigCat Jun 03 '22 at 09:51

1 Answers1

0

Using DateRange function helped.

Richi Sharma
  • 175
  • 1
  • 13