0

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!

3
  • the json you've posted for the serialized query is not the same query as defined with NEST's fluent API. I just tried your example and get the expected serialized query. What version of Elasticsearch are you targeting, and what version of NEST are you using? Commented Jan 19, 2017 at 0:23
  • In the fluent api I changed the filter to a must inside the bool but I get the same result if I use filter. Can you post the query that was generated by NEST? I'm using the latest. Just installed nest via nuget recently Commented Jan 19, 2017 at 7:50
  • what version of Elasticsearch are you running against, and what version of NEST are you using? Commented Jan 20, 2017 at 1:26

1 Answer 1

1

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)
                                )
                            )
                    )
            );
Sign up to request clarification or add additional context in comments.

1 Comment

It was creating the query properly, your mapping was incorrect :) By default, NEST automapping will infer POCO properties as object mappings, so you need to either attribute the property with [Nested], or use the .Properties() override to set it to a nested mapping

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.