How would you design a RESTful query to support OR operand between parameters. Let's say my resource has two fields field1 & field2. How would you design the URL to enable the following query:
"Get myresources where field1=x OR field2=y"
Designing queries in REST is pretty straight forward, but I have only seen queries that supports AND between query fields. e.g. /myresource?field1=x&field2=y
A possible solution can be to provide a single query parameter with free text where part, for example:
GET /myresource?q={field1=x OR field2=y}
But that would make it more complicated for clients to parse and extend or reduce filtered fields.
What do you suggest?
Query params aren't by definition AND related, they're just inert params - how you handle them is up to you. For an OR search I'd suggest:
GET /myresources?field1=x&field2=y&inclusive=true
If you want to default to an AND relationship (reasonable), and any other extension you want is of course possible.
For what its worth, SO uses the following format for finding questions with multiple tags:
http://stackoverflow.com/questions/tagged?tagnames=jquery or css or asp.net or php or web-development or svn
It's perfectly reasonable to separate them with ,
or ;
assuming those aren't valid characters for the tags themselves. Search engines typically use q=keyword1+keyword2
and url-encode any +
in the keywords themselves which is what I would suggest you do if this is for a search URI.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With