Search code examples
odataolingo

OData Filter for child and return parent


I am developing a search function which will be powered by a OData service. It will return one or a list of Header Objects as results. Many fields that we need to search on are not in the Header Object. They are only in child Objects(Navigational Properties). What is the right approach to be able to execute an OData search against a child field and still return the parent object list.

This is similar to what I would expect to be able to do in standard SQL as an 'exists' query.

I am using Java - Apache Olingo for my project but I believe that this is a general OData question.


Solution

  • Yes this is perfectly possible, just include the child path in the $filter

    e.g. say we have a header Aircraft and child Airline

    /Aircraft would list all aircraft for all Airlines Aircraft?$filter=Airline/Code eq 'BA' would list all Aircraft for BA only

    For you 2nd query, to return the child and filter on the parent... Not sure about returning JUST the child - you can do this if you resolve to a single parent by its key, e.g. Aircraft(123)/Airline - would find Aircraft with key 123 and return JUST the airline child navigation property info

    to filter and include multiple headers, then I think your only option is to use $expand to include the child info with the header info. e.g.

    Aircraft?$filter=BodyType eq 'NB'&$expand=Airline - so this filters the header aircraft on type NB (narrow body), and includes the child Airline info along with it.

    Hope this helps.