<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<section id="JCR.HiglightResultofFulltextSearch">
  <title>High-lighting Result of Fulltext Search</title>

  <para>It's also called excerption (see Excerpt configuration in <link
  linkend="JCR.SearchConfiguration">Search Configuration</link> and in <link
  linkend="JCR.SearchingRepositoryContent.Highlighting">Searching
  Repository</link> article).</para>

  <para>The goal of this query is to find words "eXo" and "implementation"
  with fulltext search and high-light this words in result value.</para>

  <section id="JCR.HiglightResultofFulltextSearch.BaseInfo">
    <title>Base info</title>

    <para>High-lighting is not default feature so we must set it in
    jcr-config.xml, also excerpt provider must be defined:</para>

    <programlisting language="xml">&lt;query-handler class="org.exoplatform.services.jcr.impl.core.query.lucene.SearchIndex"&gt;
   &lt;properties&gt;
      ...
      &lt;property name="support-highlighting" value="true" /&gt;
      &lt;property name="excerptprovider-class" value="org.exoplatform.services.jcr.impl.core.query.lucene.WeightedHTMLExcerpt"/&gt;
      ...
   &lt;properties&gt;
&lt;/query-handler&gt;</programlisting>

    <para>Also, remember that we can make indexing rules, as in the example
    below:</para>

    <para>Let's write rule for all nodes with primary node type
    'nt:unstructed' where property 'rule' equal to "excerpt" string. For those
    nodes, we will exclude property "title" from high-lighting and set "text"
    property as highlightable. Indexing-configuration.xml must containt the
    next rule:</para>

    <programlisting language="xml">&lt;index-rule nodeType="nt:unstructured" condition="@rule='excerpt'"&gt;
   &lt;property useInExcerpt="false"&gt;title&lt;/property&gt;
   &lt;property&gt;text&lt;/property&gt;
&lt;/index-rule&gt;</programlisting>
  </section>

  <section id="JCR.HiglightResultofFulltextSearch.RepositoryStructure">
    <title>Repository structure:</title>

    <para>We have single node with primary type 'nt:unstructured'</para>

    <itemizedlist>
      <listitem>
        <para>document (nt:unstructured)</para>

        <itemizedlist>
          <listitem>
            <para>rule = "excerpt"</para>
          </listitem>

          <listitem>
            <para>title = "eXoJCR"</para>
          </listitem>

          <listitem>
            <para>text = "eXo is a JCR implementation"</para>
          </listitem>
        </itemizedlist>
      </listitem>
    </itemizedlist>
  </section>

  <section id="JCR.HiglightResultofFulltextSearch.QueryExecution">
    <title>Query execution</title>

    <para><emphasis role="bold">SQL</emphasis></para>

    <programlisting language="java">// make SQL query
QueryManager queryManager = workspace.getQueryManager();
// create query
String sqlStatement = "SELECT rep:excerpt() FROM nt:unstructured WHERE CONTAINS(*, 'eXo implementation')";
Query query = queryManager.createQuery(sqlStatement, Query.SQL);
// execute query and fetch result
QueryResult result = query.execute();</programlisting>

    <para><emphasis role="bold">XPath</emphasis></para>

    <programlisting language="java">// make XPath query
QueryManager queryManager = workspace.getQueryManager();
// create query
String xpathStatement = "//element(*,nt:unstructured)[jcr:contains(., 'eXo implementation')]/rep:excerpt(.)";
Query query = queryManager.createQuery(xpathStatement, Query.XPATH);
// execute query and fetch result
QueryResult result = query.execute();</programlisting>
  </section>

  <section id="JCR.HiglightResultofFulltextSearch.FetchingResult">
    <title>Fetching result</title>

    <para>Now let's see on the result table:</para>

    <programlisting language="java">String[] columnNames = result.getColumnNames();
RowIterator rit = result.getRows();
while (rit.hasNext())
{
   Row row = rit.nextRow();
   // get values of the row
   Value[] values = row.getValues();
}</programlisting>

    <para>Table content is</para>

    <table>
      <title>Table content</title>

      <tgroup cols="3">
        <thead>
          <row>
            <entry>rep:excerpt()</entry>

            <entry>jcr:path</entry>

            <entry>jcr:score</entry>
          </row>
        </thead>

        <tbody>
          <row>
            <entry>&lt;div&gt;&lt;span&gt;&lt;strong&gt;eXo&lt;strong&gt;is JCR&lt;strong&gt;implementation&lt;strong&gt;&lt;span&gt;&lt;div&gt;</entry>

            <entry>/testroot/node1</entry>

            <entry>335</entry>
          </row>
        </tbody>
      </tgroup>
    </table>

    <para>As you see, words "eXo" and "implamentation" is highlighted.</para>

    <para>Also, we can get exactly "rep:excerpt" value:</para>

    <programlisting language="java">RowIterator rows = result.getRows();
Value excerpt = rows.nextRow().getValue("rep:excerpt(.)");
// excerpt will be equal to "&lt;div&gt;&lt;span\&gt;&lt;strong&gt;eXo&lt;/strong&gt; is a JCR &lt;strong&gt;implementation&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;"</programlisting>
  </section>
</section>
