Jahia Search in the Enterprise

Nov 09, 2011
Oshyn Labs

Jahia has an embedded search engine that can be used by a Tag Library. This library offers components to build forms, field elements, components to display search results, and components to implement the pagination. I want to share a way of how to use the Jahia Search Tag Library to do full text searches.


Configuring Search Results Page Template

I am assuming that you have some experience with template development. If not, you can find a quick review of template development here. When you create a template set, you need to configure the file templates.xml to declare the templates that are going to be part of the template, but also you can use this file to configure which page is going to handle search results. You need to add the following configuration to the file templates.xml.


<common-pages>
<search-results>SearchResult.jsp</search-results>
</common-pages>


SearchResults.jsp is the page that is going to display the search results executed by the Jahia Search Taglib.


Search Form

To build search forms, first you need to include the Jahia Search Taglib in the .jsp file that is going to contain the form


<%@ taglib uri="http://www.jahia.org/tags/search" prefix="s" %>


Then you need to use the tags and to build the form. Following is an example form:


<s:form name="fulltextsearch">
<table>
<tr>
<td class="title" style="width: 150px;">Search</td>
<td style="width: 350px;"><s:term class="text1"/></td>
<td><input type="submit" value="search" class="button"/></td>
</tr>
</table>
</s:form>


Search Results Page

The search results page is the page configured in the file template.xml to display the results. This page is called every time that a submit is performed in the s:form. You need to use the libraries:

<%@ taglib uri="http://www.jahia.org/tags/search" prefix="s" %>
<%@ taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg" %>


And the tags <s:results>, <pg:pager>, <s:resultIterator>,  <pg:item> to display the results.

The tag <s:results> is in charge to execute the search  and expose the results to be displayed, and <s:resultIterator> is in charge to iterate over the results.  The tag <s:results>  has the important method getCount(), to obtain the number of results, and can be accessed by the EL (Expression Language) ${count}.

The tag <pg:pager> is in charge of implement the pagination of large data sets and <pg:item> represents each page returned by the search.


Following is an example of how to use the above elements in action:

<s:results>
<table>
<pg:pager maxPageItems="10" url="${jahia.page.url}" export="currentPageNumber=pageNumber">
<s:resultIterator>
<pg:item>
<tr>
<td><a href="${hit.link}">${fn:escapeXml(hit.title)}</a></td>
<td>${hit.summary}</td>
</tr>
</pg:item>
</s:resultIterator>
</pg:pager>
</table>
</s:results>


Notice that the element <pg:pager> is used to define the number of results displayed per page. The <s:results> tag returns a java.util.List<org.jahia.engines.search.Hit> List, so for that reason you can use the EL ${hit.link}, ${hit.title} and ${hit.summary}.

Also you can include more pagination elements to handle the navigation. You can use the tags <pg:prev> and <pg:next> inside the tag <pg:index> like the following example:


<pg:index export="itemCount">
<pg:prev ifnull="true">
<c:if test="${not empty pageUrl}">
<a href="${pageUrl}">previous</a>
</c:if>
<c:if test="${empty pageUrl}">
previous
</c:if>
</pg:prev>        |
<pg:next ifnull="true">
<c:if test="${not empty pageUrl}">
<a> <a href="${pageUrl}">next</a></c:if>
<c:if test="${empty pageUrl}">
next
</c:if>
</pg:next>
</pg:index>


The following screenshot shows a search result page with pagination. Each page has the size of one, and notice that at the bottom of the page are the next and previous arrows to navigate through pages.





Related blog posts: