Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advanced search REST API

My requirement is to implement advanced search Rest API for searching the phones. The URI for the search API is http://myservice/api/v1/phones/search?q=${query_expression}

Where q is the complex query expression. Have the following questions

1) Since advanced search involves a lengthy query expression, the URI will not fit in a GET call. Is it alright to implement the search API via POST request and still maintain the RESTfulness?

2) I have come across the following implementations for the advanced search:

  • 1st approach - Send the complete infix expression for the query expression. eg.

PHONENAME STARTSWITH 'AR' AND ( PHONETYPE = '4G' OR PHONECOLOR = 'RED')

  • 2nd approach - Constructing entire query expression in the form of a json. eg.

    {"criteria":[ {"index":1,"field":"PHONENAME","value":"AR","comparator":"STARTSWITH"}, {"index":2,"field":"PHONETYPE","value":"4G","comparator":"EQUALS"}, {"index":3,"field":"PHONECOLOR","value":"RED","comparator":"EQUALS"} ],"criteria":"( 1 AND (2 OR 3) )"}

  • 3rd approach - Alternative way to implement the query expression as a json. eg.

    {"and":[ {"field":"PHONENAME","value":"AR","comparator":"STARTSWITH"}, "or":[ {"field":"PHONETYPE","value":"4G","comparator":"EQUALS"}, {"field":"PHONECOLOR","value":"RED","comparator":"EQUALS"}] ]}

Which approach would be considered more RESTful out of the three? Suggestions for any other approaches are welcome :)

like image 778
Akshai Avatar asked Sep 25 '22 14:09

Akshai


1 Answers

You could follow the approach taken by ElasticSearch, which out of the examples you had given is the third one.

See https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html

The third approach is also easier to understand and easier to maintain. For example if in the future you would need to add "fuzzy" query operator and it would have a completely different model, that would be an easy thing to do.

like image 115
aclowkay Avatar answered Sep 28 '22 04:09

aclowkay