<?xml version="1.0" encoding="UTF-8"?><api:function-page xml:base="/apidoc/8.0/cts.search.xml" generated="2015-10-07T16:36:00.016766-07:00" mode="javascript" xmlns:api="http://marklogic.com/rundmc/api"><api:function-name>cts.search</api:function-name><api:suggest>cts.search</api:suggest><api:suggest>cts</api:suggest><api:suggest>search</api:suggest><api:function-link mode="xquery" fullname="cts:search">/apidoc/8.0/cts:search.xml</api:function-link><api:function mode="javascript" name="search" type="builtin" lib="cts" category="SearchBuiltins" subcategory="Search" hidden="false" bucket="MarkLogic Built-In Functions" prefix="cts" namespace="http://marklogic.com/cts" fullname="cts.search"><api:summary>
  Returns a relevance-ordered sequence of nodes specified by a given query.
</api:summary><api:params><api:param name="query" type="cts:query?"><api:param-description>
    A <code xmlns="http://www.w3.org/1999/xhtml">cts:query</code> specifying the search to perform.  If a string
   is entered, the string is treated as a <code xmlns="http://www.w3.org/1999/xhtml">cts:word-query</code> of the
   specified string.
  </api:param-description><api:param-name>query</api:param-name><api:param-type>cts.query?</api:param-type></api:param><api:param name="options" type="(cts:order|xs:string)*" optional="true"><api:param-description>
    Options to this search.  The default is ().
    <a id="unfiltered" xmlns="http://www.w3.org/1999/xhtml"></a><p xmlns="http://www.w3.org/1999/xhtml">
      Options include:</p>
      <blockquote xmlns="http://www.w3.org/1999/xhtml"><dl>
        <dt><p>"filtered"</p></dt>
        <dd><p>A filtered search (the default). Filtered searches
        eliminate any false-positive matches and properly resolve cases where
        there are multiple candidate matches within the same fragment.
        Filtered search results fully satisfy the specified
        <code>cts:query</code>.</p>
        </dd>
        <dt><p>"unfiltered"</p></dt>
        <dd><p>
        An unfiltered search.  An unfiltered search
        selects fragments from the indexes that are candidates to satisfy
        the specified <code>cts:query</code>, and then it returns
        a single node from within each fragment that satisfies the specified
        searchable path expression. Unfiltered searches are useful because
        of the performance they afford when jumping deep into the
        result set (for example, when paginating a long result set and
        jumping to the 1,000,000th result). However, depending on the
        searchable path expression, the
        <code>cts:query</code> specified, the structure of the documents in
        the database, and the configuration of the database, unfiltered
        searches may yield false-positive results being included in the
        search results.  Unfiltered searches may also result in missed
        matches or in incorrect matches, especially when there are
        multiple candidate matches within a single fragment.
        To avoid these problems, you should only use unfiltered searches
        on top-level XPath expressions (for example, document nodes,
        collections, directories) or on fragment roots. Using unfiltered
        searches on complex XPath expressions or on XPath expressions that
        traverse below a fragment root can result in unexpected results.</p>
        </dd>
        <dt><p>"score-logtfidf"</p></dt>
        <dd><p>Compute scores using the logtfidf method (the default scoring
        method). This uses the formula: <br/><br/>
          <code>log(term frequency) * (inverse document frequency)
        </code></p></dd>
        <dt><p>"score-logtf"</p></dt>
        <dd><p>Compute scores using the logtf method. This does not take into
        account how many documents have the term and uses the formula: <br/>
        <br/>
          <code>log(term frequency)</code></p></dd>
        <dt><p>"score-simple"</p></dt>
        <dd><p>Compute scores using the simple method. The score-simple
        method gives a score of 8*weight for each matching term in the
        <code>cts:query</code> expression, and then scales the score up by
	multiplying by 256. It does not matter how
        many times a given term matches (that is, the term
        frequency does not matter); each match contributes 8*weight
        to the score.  For example, the following query  (assume the
        default weight of 1) would give a score of 8*256=2048 for
        any fragment with one or more matches for "hello", a score of
	16*256=4096
        for any fragment that also has one or more matches for "goodbye",
        or a score of zero for fragments that have no matches for
        either term:<br/><br/>
          <code>cts:or-query(("hello", "goodbye"))</code></p></dd>
        <dt><p>"score-random"</p></dt>
        <dd><p>Compute scores using the random method. The score-random
        method gives a random value to the score.  You can use this
        to randomly choose fragments matching a query.</p></dd>
        <dt><p>"score-zero"</p></dt>
        <dd><p>Compute all scores as zero.
        When combined with a quality weight of zero,
        this is the fastest consistent scoring method.</p></dd>
        <dt>"checked"</dt>
        <dd><p>Word positions are checked (the default) when resolving
        the query. Checked searches eliminate false-positive matches for
        phrases during the index resolution phase of search processing.</p></dd>
        <dt>"unchecked"</dt>
        <dd><p>Word positions are not checked when resolving the
        query.  Unchecked searches do not take into account word positions
        and can lead to false-positive matches during the index resolution
        phase of search processing.  This setting is useful
        for debugging, but not recommended for normal use.</p></dd>
        <dt>"too-many-positions-error"</dt>
        <dd>If too much memory is needed to perform positions calculations
        to check whether a document matches a query, 
        return an XDMP-TOOMANYPOSITIONS error, 
        instead of accepting the document as a match.</dd>
        <dt>"faceted"</dt>
        <dd><p>Do a little more work to save faceting information about
        fragments matching this search so that calculating facets
        will be faster.</p></dd>
        <dt>"unfaceted"</dt>
        <dd><p>Do not save faceting information about fragments matching
        this search.</p></dd>
        <dt>"relevance-trace"</dt>
        <dd><p>Collect relevance score computation details with which you
        can generate a trace report using <code>cts:relevance-info</code>.
        Collecting this information is costly and will significantly
        slow down your search, so you should only use it when using
        <code>cts:relevance-info</code> to tune a query.</p></dd>
        <dt>"format-<em>FORMAT</em>"</dt>
        <dd><p>Limit the search to documents in document format specified
        by <em>FORMAT</em> (binary, json, text, or xml)</p></dd>
	<dt class="javascript">"any"</dt>
        <dd class="javascript">Search from any fragment.</dd>
        <dt class="javascript">"document"</dt>
        <dd class="javascript">Search from document fragments.</dd>
        <dt class="javascript">"properties"</dt>
        <dd class="javascript">Search only from properties fragments.</dd>
        <dt class="javascript">"locks"</dt>
        <dd class="javascript">search only from locks fragments.</dd>
        <dt><em>cts:order Specification</em></dt>
        <dd><p>A sequence of <code>cts:order</code> specifications. The order
	is evaluated in the order each appears in the sequence.  The sequence
	typically consists of one or more of:
	<code><a href="./cts:index-order">cts:index-order</a></code>,
	<code><a href="./cts:score-order">cts:score-order</a></code>,
	<code><a href="./cts:confidence-order">cts:confidence-order</a></code>,
	<code><a href="./cts:fitness-order">cts:fitness-order</a></code>,
	<code><a href="./cts:quality-order">cts:quality-order</a></code>,
	<code><a href="./cts:document-order">cts:document-order</a></code>,
	<code><a href="./cts:unordered">cts:unordered</a></code>.  When using
	<code>cts:index-order</code>, there must be a range index defined
	on the index(es) specified by the <code>cts:referennce</code>
        specification (for example,
     <code><a href="./cts:element-reference">cts:element-reference</a></code>.)
	</p></dd>
     </dl></blockquote>
  </api:param-description><api:param-name>options</api:param-name><api:param-type>String[]</api:param-type></api:param><api:param name="quality-weight" type="xs:double?" optional="true"><api:param-description>
    A document quality weight to use when computing scores.
    The default is 1.0.
  </api:param-description><api:param-name>quality-weight</api:param-name><api:param-type>Number?</api:param-type></api:param><api:param name="forest-ids" type="xs:unsignedLong*" optional="true"><api:param-description>
    A sequence of IDs of forests to which the search will be constrained.
    An empty sequence means to search all forests in the database.
    The default is (). In the XQuery version, you can use
    <code xmlns="http://www.w3.org/1999/xhtml">cts:search</code> with this
    parameter and an empty <code xmlns="http://www.w3.org/1999/xhtml">cts:and-query</code> to specify a
    forest-specific XPath statement (see the <a href="#searchEx3" xmlns="http://www.w3.org/1999/xhtml">third
    example</a> below). If you
    use this to constrain an XPath to one or more forests, you should set
    the <code xmlns="http://www.w3.org/1999/xhtml">quality-weight</code> to zero to keep the XPath document
    order.
  </api:param-description><api:param-name>forest-ids</api:param-name><api:param-type>String[]</api:param-type></api:param></api:params><api:return>ValueIterator</api:return><api:usage>

<p xmlns="http://www.w3.org/1999/xhtml">Each node that
<code class="javascript">cts.search</code> returns has a score with which
it is associated. To access the score, use the
<code class="javascript">cts.score</code>
function. The nodes are returned in relevance order (most relevant to least
relevant), where more relevant nodes have a higher score.</p>
<p xmlns="http://www.w3.org/1999/xhtml">Only one of the "filtered" or "unfiltered" options may be specified
in the options parameter. If neither "filtered" nor "unfiltered", is
specified then the default is "filtered".</p>
<p xmlns="http://www.w3.org/1999/xhtml">Only one of the "score-logtfidf", "score-logtf", "score-simple",
"score-random", or "score-zero" options may be specified in the
options parameter.
If none of "score-logtfidf", "score-logtf", "score-simple", "score-random",
or "score-zero" are specified, then the default is "score-logtfidf".</p>
<p xmlns="http://www.w3.org/1999/xhtml">Only one of the "checked" or "unchecked" options may be specified
in the options parameter.  If the neither "checked" nor "unchecked" are
specified, then the default is "checked".</p>
<p xmlns="http://www.w3.org/1999/xhtml">Only one of the "faceted" or "unfaceted" options may be specified
in the options parameter.  If the neither "faceted" nor "unfaceted" are
specified, then the default is "unfaceted".</p>

<p class="javascript" xmlns="http://www.w3.org/1999/xhtml">If the <code>cts:query</code> specified is the
empty string (equivalent to <code>cts.wordQuery("")</code>), then the
search returns an empty Iterator.</p>
<p class="javascript" xmlns="http://www.w3.org/1999/xhtml">Only one of "any", "document", "properties", or "locks"
  may be specified in the options parameter.
  If none of "any", "document", "properties", or "locks" are specified
  and there is a $query parameter, then the default is "document".
  If there is no $query parameter then the default is "any".</p>
</api:usage><!-- XQuery examples --><!-- Javascript examples --><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
cts.search(cts.wordQuery("with flowers"));

 =&gt; ... an Iterator of any node containing the phrase 'with flowers'.
</pre></api:example><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
fn.subsequence(cts.search(cts.wordQuery("with flowers")), 1, 10);

 =&gt; ... an Iterator of the first 10 nodes containing the phrase 'with flowers'.
</pre></api:example><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
fn.subsequence(
    cts.search("hello", ["unfiltered",
     cts.indexOrder(cts.elementReference(fn.QName("", "Title")))
    ] ), 1, 10);
=&gt; Returns the first 10 documents with the word "hello", unfiltered,
   ordered by the range index on the element "Title".  An element
   range index on Title is required for this search, otherwise it
   throws an exception.
</pre></api:example></api:function></api:function-page>