Dynamic AQL filtering based on URL's parameters

Let say we want to build a page which will call an helper which will have some extra parameters and we want to filter data based on them.

/en/my/search/term/test

In this URL based on this schema (/:lang/:all/:page/:splat*) the splat will be { "term": "test" }

You can have as many params you want (the limit is the URL size)

Here a basic AQL

FOR doc IN datasets
  FILTER doc.type == "posts"
  RETURN doc

It will return all the documents found for type "posts". Now let filter it via the URL

FOR doc IN datasets
  FILTER doc.type == "posts"
  FILTER doc.slug == @term
  RETURN doc

On this request we will then only return documents with slug == "test" (based on the URL parameter). Great ... but It could be better ... What about if want to reach /en/my/search without any parameter. The request will fail because of the missing @term parameter ! But there is a way to handle it.

FOR doc IN datasets
  FILTER doc.type == "posts"
  __IF term__
  FILTER doc.slug == @term
  __END term__
  RETURN doc

Cool ! There is an easy way to build filters depending existing parameters ! Notice that there is a keyword also for checking the absence of a parameter.

FOR doc IN datasets
  FILTER doc.type == "posts"
  __IF_NOT term__
  FILTER doc.online == true
  __END_NOT term__
  RETURN doc

In this case, it will return all online == true documents if term parameter is not provided.

That's all for today ! I hope it will help you to build blazing fast CMS !