Ant
Ant

Reputation: 368

Nested Date Range Query Filter ElasticSearch NEST C#

I have a simple Trip object with many Departures and I want to return the parent Trip where it contains any departures greater than a date.

Trip JSON looks like

{
    "title": "Something",
    "code": "something else",
    "departures" : [{ "out" : {date}, "in" : {date} }]
}

I can get it working in PostMan manually by calling _search on type endpoint with post data:

{
    "query" : {
        "nested" : {
            "path" : "departures",
            "query" : {
                "bool" : {
                    "filter" : [
                        { "range" : { "departures.out" : { "gt" : 1483315200000 } } }
                    ]
                }
            }
        }
    }
}

However I am using C# and specifically NEST and have written the query as such:

var searchResponse = client.Search<Trip>(s => s
                .Index("indexName")
                .From(0)
                .Size(10)
                .Query(q => q
                    .Nested(n => n
                        .Path(t => t.Departures)
                        .Query(q2 => q2
                            .Bool(b => b
                                .Must(f => f
                                    .DateRange(dr => dr
                                        .Field(t2 => t2.Departures.First().Out)
                                        .GreaterThanOrEquals("01/02/2017")
                                        .Format("dd/MM/yyyy")
                                    )
                                )
                            )
                        )
                    )
                )
            );

NEST is throwing an error Failed to create query:

query: {
  "nested" : {
    "query" : {
      "bool" : {
        "must" : [
          {
            "match_none" : {
              "boost" : 1.0
            }
          }
        ],
        "disable_coord" : false,
        "adjust_pure_negative" : true,
        "boost" : 1.0
      }
    },
    "path" : "departures",
    "ignore_unmapped" : false,
    "score_mode" : "avg",
    "boost" : 1.0
  }

Why is it not creating the date range query properly? I've tried all sorts of different variations and nothing seems to work!

Upvotes: 0

Views: 2872

Answers (1)

Ant
Ant

Reputation: 368

I had to set up the index manually and define the departures as a nested object. Just pushing the data into the index and having it auto map the properties did not define it as a nested object and therefore it was not creating the query properly.

var index = new CreateIndexDescriptor(INDEX_NAME)
            .Mappings(ms => ms
                    .Map<Trip>(m => m
                            .AutoMap()
                            .Properties(p => p
                                .Nested<Departure>(n => n
                                        .Name(d => d.Departures)
                                )
                            )
                    )
            );

Upvotes: 1

Related Questions