Written by The Jahia Team
 
Developers
   Estimated reading time:

Once you have Augmented Search installed and running, you can start writing your first queries. To make it easier we recommend getting started with GraphiQL available in Jahia tools.

Please note that for security reasons, the GraphQL API is closed even if nodes have public read permissions. For information on opening the API and making it usable for your application, see Setting up authorization.

Writing search queries

This section shows you how how to create a basic query, and specify sort, basic and advanced filters, and facets options. 

Writing a basic query

The section shows you how to create a basic query. The examples use the Digitall demo website that is bundled with Jahia.

A search query is composed of several parts: 

  • searches wrapper
    The searches wrapper specifies siteKey, language and workspace properties and allows you to have multiple search queries which execute at the same time. Here’s an example of what the start of your query might look like.
    {
      jcr {
        searches(siteKey: "digitall", language: "en", workspace: LIVE) {
          ...
        }
      }
    }
  • search object
    You can use a search object to specify the parameters of your search. The search object must have q which stands for query and is the only mandatory parameter. Here’s an example of using the q parameter.
    {
      jcr {
        searches(siteKey: "digitall", language: "en", workspace: LIVE) {
          search(q:"news") {
            took
            totalHits
            hasMore
            hitsOnPage
            hits {
              node {
                name
              }
              lastModified
              lastPublished
              lastModifiedBy
              lastPublishedBy
              displayableName
            }
          }
        }
      }
    }
  • searchIn parameter
    You can also specify whether you want to search files or content or both by specifying a value for the searchIn parameter as shown below. By default (if you don’t provide a searchIn parameter), the search is performed in both files and content indices.
    searchIn:[CONTENT]
    {
      jcr {
        searches(siteKey: "digitall", language: "en", workspace: LIVE) {
          search(q:"news", searchIn:CONTENT) {
            took
            totalHits
            hasMore
            hitsOnPage
            hits {
              node {
                name
              }
              lastModified
              lastPublished
              lastModifiedBy
              lastPublishedBy
              displayableName
            }
          }
        }
      }
    }
  • search properties
    Search gives you access to the following properties:
    • took
      The time it took to search
    • totalHits
      The total number of available search results
    • hasMore
      Whether there are more results on next page
    • hitsOnPage
      The number of hits returned on the current page
    • hits
      Individual results

      The hits object accepts the following properties:
      • node
        Wrapper for JCR node, which gives you access to any property of the node, children, permissions, and more
      • displayName
        Display name of the returned node
      • lastModified
        Date when content was last modified (includes node and indexed subnodes)
      • lastPublished
        Date when content was last published (includes node and indexed subnodes)
      • lastModifiedBy
        User who made the latest change
      • lastPublishedBy
        User who last published the node
  • Limit and offset parameters
    You can specify limit and offset parameters to further restrict your search. The total offset is calculated using limits. For example, a limit of 5 and an offset of 2 returns results from the 10th entry to the 15th, provided that they exist. In other words, offset is a page number for a page of size limit.
    {
      jcr {
        searches(siteKey: "digitall", language: "en", workspace: LIVE) {
          search(
            q:"news" 
            searchIn:[CONTENT]
            limit:2 
            offset:0) {
            took
            totalHits
            hitsOnPage
            ... 
          }
        }
      }
    }

Now that you know how to write a basic text search query and use constraints, let’s look at some additional functionality which will help you refine your search further.

Sorting

Sort just requires two ingredients: a property to sort on and a sorting direction. Here’s an example of adding sort on jcr:title to a query that sorts in descending order.

{
  jcr {
    searches(siteKey: "digitall", language: "en", workspace: LIVE) {
      search(... 
          sortBy:{property:"jcr:title", orderType:DESC}) {
       ...
      }
    }
  }
}