Search code examples
restodata

OData search on transformed properties


We serve json objects over a OData REST API. The rough structure of an object looks like below

{ "Property1": "Value1", "Property2": { "InterestingProperty": "/text1/part1/text2/part2" } }

The values "part1" and "part2" are the ones I am interested in and from the back end they are indexed together.

I need to design the List API which can allow filtering of the objects which contain both the part1 and part2 in that particular property. Any guidance on how to go about this ?


Solution

  • You could solve this on the client side with the $filter query option and the built-in contains function. For example,

    GET http://host/MyObjects?$filter=contains(Property2/InterestingProperty,'part1') and contains(Property2/InterestingProperty,'part2')
    

    However, this approach does allow for false positives; e.g., if the value of InterestingProperty is something like /text1/part1/text2/part234.

    Perhaps a better approach is to define a server-side OData function to do custom filtering. This would allow you to have fine-grain control over matching against InterestingProperty. A function named CustomFilter in the MyService namespace and bound to the MyObjects entity set, would be invoked as follows:

    GET http://host/MyObjects/MyService.CustomFilter
    

    If you are using the .NET implementation of OData, there is a tutorial to get you started writing OData functions.