<?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.QueryUsecases">
  <title>JCR Query Usecases</title>
    <para>JCR supports two query languages - JCR and XPath. A query, whether
    XPath or SQL, specifies a subset of nodes within a workspace, called the
    result set. The result set constitutes all the nodes in the workspace that
    meet the constraints stated in the query.</para>

  <section id="JCR.QueryUsecases.QueryLifecycle">
    <title>Query Lifecycle</title>

    <section id="JCR.QueryUsecases.QueryLifecycle.QueryCreationAndExecution">
      <title>Query Creation and Execution</title>

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

      <programlisting language="java">// get QueryManager
QueryManager queryManager = workspace.getQueryManager(); 
// make SQL query
Query query = queryManager.createQuery("SELECT * FROM nt:base ", Query.SQL);
// execute query
QueryResult result = query.execute();</programlisting>

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

      <programlisting language="java">// get QueryManager
QueryManager queryManager = workspace.getQueryManager();
// make XPath query
Query query = queryManager.createQuery("//element(*,nt:base)", Query.XPATH);
// execute query
QueryResult result = query.execute();</programlisting>
    </section>

    <section id="JCR.QueryUsecases.QueryLifecycle.QueryResultProcessing">
      <title>Query Result Processing</title>

      <programlisting language="java">// fetch query result
QueryResult result = query.execute();</programlisting>

      <para>Now we can get result in an iterator of nodes:</para>

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

      <para>or we get the result in a table:</para>

      <programlisting language="java">// get column names
String[] columnNames = result.getColumnNames();
// get column rows
RowIterator rowIterator = result.getRows();
while(rowIterator.hasNext()){
   // get next row
   Row row = rowIterator.nextRow();
   // get all values of row
   Value[] values = row.getValues();
}</programlisting>
    </section>

    <section id="JCR.QueryUsecases.QueryLifecycle.Scoring">
      <title>Scoring</title>

      <para>The result returns a score for each row in the result set. The
      score contains a value that indicates a rating of how well the result
      node matches the query. A high value means a better matching than a low
      value. This score can be used for ordering the result.</para>

      <para>eXo JCR Scoring is a mapping of Lucene scoring. For a more
      in-depth understanding, please study <ulink
      url="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_4/scoring.html">Lucene
      documentation</ulink>.</para>

      <para>jcr:score counted in next way - (lucene score)*1000f.</para>

      <para>Score may be increased for specified nodes, see <link
      linkend="JCR.IndexBoostRule">Index Boost Value</link></para>

      <para>Also, see an example <link linkend="JCR.OrderByScore">Order by
      Score</link></para>
    </section>
  </section>

  <section id="JCR.QueryUsecases.QueryResultSettings">
    <title>Query result settings</title>

    <itemizedlist>
      <listitem>
        <para><link linkend="JCR.SetOffsetandSetLimit">Set Offset And
        Limit</link></para>
      </listitem>
    </itemizedlist>
  </section>

  <section id="JCR.QueryUsecases.TypeConstraints">
    <title>Type Constraints</title>

    <itemizedlist>
      <listitem>
        <para><link linkend="JCR.FindAllNodes">Find All Nodes</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.FindNodesByPrimaryType">Find Nodes by Primary
        Type</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.FindNodesByMixinType">Find Nodes by Mixin
        Type</link></para>
      </listitem>
    </itemizedlist>
  </section>

  <section id="JCR.QueryUsecases.PropertyConstraints">
    <title>Property Constraints</title>

    <itemizedlist>
      <listitem>
        <para><link linkend="JCR.PropertyComparison">Property
        Comparison</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.LIKEConstraint">LIKE Constraint</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.EscapinginLIKEStatements">Escaping in LIKE
        Statements</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.NOTConstraint">NOT Constraint</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.ANDConstraint">AND Constraint</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.ORConstraint">OR Constraint</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.PropertyExistenceConstraint">Property
        Existence Constraint</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.FindNodesCaseInsensitive">Upper and Lower
        Case Constraints</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.DatePropertyComparison">Date Property
        Comparison</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.NodeNameConstraint">Node Name
        Constraint</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.MultivaluePropertyComparison">Multivalue
        Property Comparison</link></para>
      </listitem>
    </itemizedlist>
  </section>

  <section id="JCR.QueryUsecases.PathConstraint">
    <title>Path Constraint</title>

    <itemizedlist>
      <listitem>
        <para><link linkend="JCR.ExactPathConstraint">Exact Path
        Constraint</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.ChildNodeConstraint">Child Node
        Constraint</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.FindAllDescendantNodes">Find All Descendant
        Nodes</link></para>
      </listitem>
    </itemizedlist>
  </section>

  <section id="JCR.QueryUsecases.OrderingSpecifying">
    <title>Ordering specifying</title>

    <itemizedlist>
      <listitem>
        <para><link linkend="JCR.OrderByProperty">Order by
        Property</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.OrderByDescendant">Order by Descendant Node
        Property</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.OrderByScore">Order by Score</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.OrderByPathOrName">Order by Path or
        Name</link></para>
      </listitem>
    </itemizedlist>
  </section>

  <section id="JCR.QueryUsecases.FulltextSearch">
    <title><link linkend="JCR.FulltextSearchAndSettings">Fulltext
    Search</link></title>

    <itemizedlist>
      <listitem>
        <para><link linkend="JCR.FulltextSearchByProperty">Fulltext Search by
        Property</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.FulltextSearchByAllProperties">Fulltext
        Search by All Properties</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.AggregationRule">Find nt:file document by
        content of child jcr:content node</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.IgnoreAccentSymbols">How to set new Analyzer.
        Accent symblos ignoring</link></para>
      </listitem>
    </itemizedlist>
  </section>

  <section id="JCR.QueryUsecases.IndexingRulesAndAdditionalFeatures">
    <title>Indexing rules and additional features</title>

    <itemizedlist>
      <listitem>
        <para><link linkend="JCR.AggregationRule">Aggregation
        rule</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.HiglightResultofFulltextSearch">Search Result
        Highlighting</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.IndexBoostRule">Index Boost
        Value</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.NodeScopeIndex">Exclusion from the Node Scope
        Index</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.RegexpIndexingRule">Regular expressions as
        property name in indexing rule</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.SynonimProvider">Synonim
        Provider</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.SpellChecker">Spell Checking</link></para>
      </listitem>

      <listitem>
        <para><link linkend="JCR.FindSimilarNodes">Find Similar
        Nodes</link></para>
      </listitem>
    </itemizedlist>
  </section>

  <section id="JCR.QueryUsecases.QueryExamples">
    <title>Query Examples</title>

    <xi:include href="offset-and-limit.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="find-all-nodes.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="find-nodes-by-primary-type.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="find-nodes-by-mixin-type.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="property-comparison.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="like-constraint.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="escaping-like-statements.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="not-constraint.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="and-constraint.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="or-constraint.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="property-existance-constraint.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="find-nodes-case-insensitive.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="date-property-comparison.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="node-name-constraint.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="multivalue-property-comparison.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="exact-path-constraint.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="child-node-constraint.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="find-all-descendant-nodes.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="order-by-property.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="order-by-descendant.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="order-by-score.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="order-by-path-or-name.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="fulltext-search-by-property.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="fulltext-search-by-all-properties.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="ignore-accent-symbols.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="aggregation-rule.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="index-boost-value.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="node-scope-index.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="regexp-indexing-rule.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="higlight.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="synonim-provider.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="spell-checker.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

    <xi:include href="find-similar-nodes.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />
  </section>

  <section id="JCR.QueryUsecases.TipsAndTricks">
    <title>Tips and tricks</title>

    <xi:include href="tip-nodename-with-number.xml"
                xmlns:xi="http://www.w3.org/2001/XInclude" />

  </section>
</section>
