Written by The Jahia Team
 
Developers
   Estimated reading time:

Augmented Search exposes a new OSGI service to execute search queries.

To use this service in your Jahia modules, you need to have a dependency on Augmented Search in your pom.xml file.


<dependency>
  <groupId>org.jahia.modules</groupId>
  <artifactId>augmented-search</artifactId>
  <version>2.1.2</version>
  <scope>provided</scope>
</dependency>

Then, to use Augmented Search you will first need to get the service.


ServiceReference searchReference = bundleContext.getServiceReference(AugmentedSearchService.class);
if (searchReference != null) {
   AugmentedSearchService searchService = bundleContext.getService(searchReference);

You can then use the searchService to execute searches. For example, the snippet below searches for terms in the query variable across all content nodes (as per the filter on jnt:content defined in GqlFilterNodeType).


GqlSearchResults response = searchService.search(query, 
  AugmentedSearchService.SearchIn.CONTENT, 20, 0, null,    
  new GqlFilter(null, null, null, null,      
  new GqlFilterNodeType("jnt:content", false), null, null),                                               
  getCurrentResource().getNode().getResolveSite().getSiteKey(),                                               
  getCurrentResource().getLocale().getLanguage(), getCurrentResource().getWorkspace(),                                                
  getCurrentResource().getNode().getSession());

You can also perform simple searches or fetch facets along with your search. The response object contains the result from all the facets and from the main search query. To access those results, grab the SearchResponse object by calling getMainQueryResponse.

The following snippet returns a summary of the search results.


String message = String.format("Search took %s and found %d results when searching for %s",
                              response.getTook(),
                              response.getTotalHits(),
                              query);

This can also be used in a taglib for example.

<as:search q="ceo"/>
<ol>
   <c:forEach items="${asResults.hits}" var="hit">
       <li>${hit.fields["jgql:displayableName"].value}</li>
   </c:forEach>
</ol>

Which results in the following rendering.

jahia-augmented-search-taglib.png

You can find the full codebase for the taglib example in this repository: https://github.com/Jahia/augmented-search-taglib