Querying structures

Single value

The example below shows you how to search the News Item structure based on an Id which is retrieved from the querystring.

  1. #if($request.getParameter('item'))  
  2.   
  3.     #set ($query_Id = $request.getParameter('item'))  
  4.     #set ($query = "+structureName:YOURSTRUCTURENAME +YOURSTRUCTURENAME.Id:$!{query_Id}")  
  5.   
  6. #end  

Multiple values

There will be times when you need to build a query that searches the CMS for more than one value i.e. News Item region (Africa, Europe etc)

This example gets a list of values from a multiple select field on a form either via the methods get (querystring) or post.

  1. #if($request.getParameterValues('regions'))  
  2.   
  3.     #foreach ($regions in $request.getParameterValues('regions'))  
  4.         #if ($query_regions)  
  5.             #set ($query_regions = "$!{query_regions} YOURSTRUCTURENAME.regions:*${regions}*")  
  6.         #else  
  7.             #set ($query_regions = "YOURSTRUCTURENAME.regions:*${regions}*")  
  8.         #end  
  9.     #end  
  10.   
  11.     #if ($query_regions)  
  12.         #set ($query_regions = "+(${query_regions})")  
  13.     #end  
  14.   
  15.     #set($query = "+structureName:YOURSTRUCTURENAME $!{query_regions}")  
  16.   
  17. #end  

Exclude values

The example below gets all the items from the YOURSTRUCTURENAME structure but removes all the items which are type:article (-YOURSTRUCTURENAME.type1:Article) notice the minus symbol. This will now display all the blogs, photos and videos.

  1. #set($query = "+structureName:YOURSTRUCTURENAME -YOURSTRUCTURENAME.type1:Article")  
  2.   
  3. #end  

Ordering results by one filter / sort

To order a query using just one filter use the following code. (Notice the PressRelease.pubDate desc this is the sorting part):

  1. #set ($cont = $dotcontent.pull("+structureName:PressRelease",0,"PressRelease.pubDate desc"))  

Ordering results by two sorting filters

To order by two values i.e type and then date you need to use a comma to separate the values. See below:

Sort Parameters you can use include: desc, asc and modDate

  1. #set ($cont = $dotcontent.pull("+structureName:PressRelease",0,"PressRelease.type desc, PressRelease.pubDate desc"))  

Searching one structure with values from another structure

If you need to search one structure with the values from another structure then you can use the code below. It is not as straightforward as in SQL

  1.   
  2. #if ($UtilMethods.isSet($request.getParameter('courseTaken')))  
  3.   
  4.       #set($userList = "+UserProfile.userId:(")  
  5.       #foreach($course in $dotcontent.pull("+structureName:Courses $!{courseId} ","0","modDate desc"))  
  6.             #set($userList = "$userList $course.userUid or")  
  7.       #end  
  8.       #set($userList = "$userList )")  
  9.       #set($query = "+structureName:UserProfile +UserProfile.userType:*Alumni* $!{userList} $!{location}")  
  10.   
  11.       $query  
  12.   
  13. #end  

Other query examples

If you need to search for several categories then you need to use the examples shown below:

Query to pull all Blogs with category: Investing OR category: Conferences selected

  1. +structureName:Blog +(categories:investing categories:conferences)  

Query to pull all Blogs with category: Investing AND category: Conferences selected

  1. +structureName:Blog +categories:investing +categories:conferences  

Query to pull all Blogs with category: Investing AND NOT category: Conferences selected

  1. +structureName:Blog +categories:investing -categories:conferences  

Query to pull all Blogs with category: Investing AND (category: Conferences OR category: releaseDates) selected

  1. +structureName:Blog +categories:investing +(categories:conferences categories:categories:releaseDates)  

Query to pull all Blogs with (category: Conferences AND category:Banking) OR (category: Investing AND category: releaseDates) selected

  1. +structureName:Blog +( (+categories:conferences +categories:banking) (+categories:investing +categories:releaseDates) )  
comments powered by Disqus