Java service

November 14, 2023

Augmented Search exposes a 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

Fetch mappedNodeTypes

Starting with Augmented Search 1.5.1 (for Jahia 7x) and 3.1.1 (for Jahia 8x), you are able to fetch any properties stored in the Elasticsearch directly from the OSGI service.

In the example below (also available in this repository), we are passing Arrays.asList("jgql:createdBy", "jgql:lastModifiedBy") to fetch the values of the corresponding properties from the index.

public int doEndTag() throws JspException {
        BundleContext bundleContext = FrameworkUtil.getBundle(SearchTag.class).getBundleContext();
        if (bundleContext != null) {
            ServiceReference<AugmentedSearchService> searchReference = bundleContext.getServiceReference(AugmentedSearchService.class);
            if (searchReference != null) {
                AugmentedSearchService searchService = bundleContext.getService(searchReference);
                try {
                    GqlFilter filter = new GqlFilter(null, null, null, null,
                                                     new GqlFilterNodeType("jnt:content", true),
                                                     null, null);
                    JCRNodeWrapper node = getCurrentResource().getNode();
                    GqlSearchResultsV2 response = searchService.search(query, AugmentedSearchService.SearchIn.CONTENT, 20, 0,
                                                                       null,
                                                                       filter,
                                                                       node.getResolveSite().getSiteKey(),
                                                                       getCurrentResource().getLocale().getLanguage(),
                                                                       getCurrentResource().getWorkspace(),
                                                                       node.getSession(),
                                                                       Arrays.asList("jgql:createdBy", "jgql:lastModifiedBy"));
                    String message = String.format("Search took %s and found %d results when searching for %s",
                                                   response.getTook(),
                                                   response.getTotalHits(),
                                                   query);
                    pageContext.getOut().print(message);
                    pageContext.setAttribute("asResults", response.getHits());
                } catch (EmptySearchException | RepositoryException | IOException e) {
                    throw new JspException(e);
                }
            }
            return EVAL_BODY_INCLUDE;
        }
        return SKIP_BODY;
    }