tags:

views:

1255

answers:

3

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?

+5  A: 

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.

annakata
A: 

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.

aleemb
+1  A: 

it depends

if you want your resource to ALLWAYS be accessed with condition1 OR condition2 your can just treat them that way...

but if you want to have both possibilities (using AND or OR) you would have to implement something like annakata said, a parameter indicating how should conditions be added to que query...

if you want to have s more flexible approach ( cond1 and cond2 or cond3 ) i see no other choice but implementing you own query with free text, like you said...

On the other hand, if you are allways querying the same field (which I think is not the case because you specified field1, field2) you can use allemb's approach, and just use some character ( "," or ";" ) to separate values...

personally, I've developed some kind of micro query language, like

field1 = val1..val2 (field1 between val1 and val2) field1 = >val2 (field1 > val2) field1 = val1;val2 (field1 = val1 or field1 = val2 ) filed1 = val1 ( field1 contains val1 )

field1 = val1..val2&>val3 ( field1 between val1 and val2 and field1 > val3...

well, you get the idea

but then I combine every condition with and, so this is just an expanded example of waht aleemb was saying...

opensas