<?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.RegexpIndexingRule">
  <title>Regular Expression as Property Name in Indexing Rules</title>

  <para>In this example, we want to configure indexind in the next way. All
  properties of nt:unstructured nodes must be excluded from search, except
  properties whoes names ends with 'Text' string. First of all, add rules to
  indexing-configuration.xml:</para>

  <programlisting language="xml">&lt;index-rule nodeType="nt:unstructured""&gt;
   &lt;property isRegexp="true"&gt;.*Text&lt;/property&gt;
&lt;/index-rule&gt;</programlisting>

  <note>
    <para>See <link linkend="JCR.SearchConfiguration">Search
    Configuration</link></para>
  </note>

  <para>Now, let's check this rule with simple query - select all nodes with
  primary type 'nt:unstructured' and containing 'quick' string (fulltext
  search by full node).</para>

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

    <para>Repository contains nt:unstructured nodes, with different
    'text'-like named properties</para>

    <itemizedlist>
      <listitem>
        <para>root</para>

        <itemizedlist>
          <listitem>
            <para>node1 (nt:unstructured) Text="The quick brown fox
            ..."</para>
          </listitem>

          <listitem>
            <para>node2 (nt:unstructured) OtherText="The quick brown fox
            ..."</para>
          </listitem>

          <listitem>
            <para>node3 (nt:unstructured) Textle="The quick brown fox
            ..."</para>
          </listitem>
        </itemizedlist>
      </listitem>
    </itemizedlist>
  </section>

  <section id="JCR.RegexpIndexingRule.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 * FROM nt:unstructured WHERE CONTAINS(*,'quick')";
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(., 'quick')]";
Query query = queryManager.createQuery(xpathStatement, Query.XPATH);
// execute query and fetch result
QueryResult result = query.execute();</programlisting>
  </section>

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

    <para>Let's get nodes:</para>

    <programlisting language="java">NodeIterator it = result.getNodes();

if(it.hasNext())
{
   Node findedNode = it.nextNode();
}</programlisting>

    <para>NodeIterator will return "node1" and "node2". "node3", as you see,
    is not in result set.</para>

    <para>Also, we can get a 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>jcr:primarytype</entry>

            <entry>jcr:path</entry>

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

        <tbody>
          <row>
            <entry>nt:unstructured</entry>

            <entry>/node1</entry>

            <entry>3806</entry>
          </row>

          <row>
            <entry>nt:unstructured</entry>

            <entry>/node2</entry>

            <entry>3806</entry>
          </row>
        </tbody>
      </tgroup>
    </table>
  </section>
</section>
