Search code examples
elasticsearchnestelastic-stackelasticsearch-dslelasticsearch-query

Date Range search using NEST doesn't return data


I want to use Date Range search in Elasticsearch using NEST API.

Simply it works when I search like this:

                        .DateRange(c => c
                            .Name("myquery")
                            .Boost(1.1)
                            .Field(p => p.CreatedOnUtc)
                            .GreaterThanOrEquals(DateMath.Now.Subtract("3d")) //this works fine
                            .LessThanOrEquals(DateMath.Now)
                            .Format("dd/MM/yyyy||yyyy")
                            .TimeZone("+01:00")
                            )

Bu when I want to use my custom fromDate Datetime value instead of DateMath.Now.Subtract("3d") it doesn't return anything!

I tried many different lines of code as below but none of them worked!

.GreaterThan(DateMath.Anchored(fromDate))

or

.GreaterThanOrEquals(DateMath.Anchored(fromDate).RoundTo(DateMathTimeUnit.Day))

or

.GreaterThan(new DateTime(2012, 01, 01, 11, 0, 0))

Returned sample json value for my datetime filed is:

"createdOnUtc": "2020-04-08T15:53:36.4800870Z"

I found related documentation/samplequery on Elasticsearch website as located here but it doesn't helped me, here I copying Object Initializer syntax example from documentation:

new DateRangeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = "description",
    GreaterThan = FixedDate,
    GreaterThanOrEqualTo = DateMath.Anchored(FixedDate).RoundTo(DateMathTimeUnit.Month),
    LessThan = "01/01/2012",
    LessThanOrEqualTo = DateMath.Now,
    TimeZone = "+01:00",
    Format = "dd/MM/yyyy||yyyy"
}

Is the problem related to Format/TimeZone or what I'm missing here?


Solution

  • When Format = "dd/MM/yyyy||yyyy" is specified on the request, Elasticsearch expects to receive a DateTime string in one of these formats, and will use the supplied format(s) to attempt to parse a DateTime string into a valid instance of a DateTime within Elasticsearch.

    With

    DateMath.Anchored(fromDate)
    

    or

    new DateTime(2012, 01, 01, 11, 0, 0)
    

    the client will serialize these to an ISO8601 DateTime string representations, so the input for these values don't match the supplied format(s) in the request.

    Looking at your first example above, it looks like you don't need to specify a value for Format. When there's no format specified, Elasticsearch will use the format specified in the date field mapping for CreatedOnUtc, and if there isn't one, will use the default "strict_date_optional_time||epoch_millis" format for date types, which will parse ISO8601 date strings produced by the client.