Sunday, 21 December 2014

AEM/CQ & Solr integration

Adobe AEM/CQ & Solr integration

Solr is used as one of the major open source search engine in many applications.
Solr can also be used with Adobe AEM/CQ to index page data. This is more helpful to avoid burdening the query browser.

Some sites also use Solr to get specific functionality. In such cases Solr search for he application will be restricted to a set of defined functionality like fetching a set of contents, some of the related items etc.

If we implement the Solr in an efficient way, the search will be extremely fast.

Steps while indexing data to Solr.
When Solr is used as part of AEM website system, all the page data will not be indexed to Solr. So to index required data, we can create custom transport handlers in CQ to insert data.

In transport handlers we should define what are all the page properties are pushed to index. Only this items are going to Solr. While doing search in Solr through query, the results appears faster.

When the data gets indexed?

When ever the page content gets modified by Author, the new data should be transferred to Solr too. We need to have a custom transport handler created which triggers replication when a page is published.So the transport handler ensure when there is a publish, the respective page is gone through custom agent and its properties are collected and pushed to System. The properties are collected and create a Solr document out of the properties using 'solrj' bundle. This document can be committed to Solr with help of solrj.

How the result is retrieved.
Solr provides search results in many formats like text, xml,JSON. Majority of the application now uses JSON as retrieval format, but it is totally on the convenience of the coder. When the system invokes the solr with query, it returns the results as the format we specified during Solr configuration. These data can be interpreted to display the results in a AEM(CQ) page.

How to delete the data from publish.
The above said custom handler will ensure whenever there is an un-publish, the data will be deleted from Solr too, thus the results are removed from the search.

We are coming up with detailed explanation of above each tasks.
Subscribe our blog for more updates about AEM CQ - Solr integrations.

JSON tips for SOLR

 Below given set of configuration tips while working with Solr output as JSON

1 How to set the Solr output to JSON?

set media-type as below.
 <xsl:output method="text" indent="no" media-type="application/json"/>

 Below line to be added in solrconfig.xml (if multi core configuration, add below line in solrconfig.xml of each core)
<queryResponseWriter name="xslt" class="org.apache.solr.response.XSLTResponseWriter"/>

Given below a sample JSON response writer. Save it in your /core/conf/xslt folder.

The query should have ‘&wt=xslt&tr=json.xsl’ appended to invoke the required JSON format.


2 How to disable displaying an attribute if its value is zero.

    <xsl:if test="articleDate != ''">
    <xsl:text>","articleDate":"</xsl:text><xsl:apply-templates select="str[@name='articleDate']"/>
    </xsl:if>

3 How to get total number of results of search returned

If the xml/json returned having nodes, <result>, below expression gives the count

<xsl:variable name="count" select="result/@numFound"/>

  Now print the value as

<xsl:value-of select="$count"/></xsl:text>


4 How to add comma to JSON element except last element

Check for the position then insert comma using below logic

        <xsl:if test="position()!=last()">
       <xsl:text>,</xsl:text>
      </xsl:if>

5) Check for a value greater than
     
      <xsl:if test="$count &gt; 8">
        <xsl:text><xsl:value-of select="$count"/> is greater than 8</xsl:text>
      </xsl:if>
     
         
6) check if  a value exists,
     
      <xsl:if test="str[@name='id']">
                    <xsl:text> id exists</xsl:text>               
              
    </xsl:if>