<?xml version="1.0" encoding="UTF-8"?><api:function-page xml:base="/apidoc/8.0/cts.nearQuery.xml" generated="2015-10-07T16:36:00.016766-07:00" mode="javascript" xmlns:api="http://marklogic.com/rundmc/api"><api:function-name>cts.nearQuery</api:function-name><api:suggest>cts.nearquery</api:suggest><api:suggest>cts</api:suggest><api:suggest>nearquery</api:suggest><api:function-link mode="xquery" fullname="cts:near-query">/apidoc/8.0/cts:near-query.xml</api:function-link><api:function mode="javascript" name="nearQuery" type="builtin" lib="cts" category="SearchBuiltins" subcategory="cts:query Constructors" hidden="false" bucket="MarkLogic Built-In Functions" prefix="cts" namespace="http://marklogic.com/cts" fullname="cts.nearQuery"><api:summary>
  Returns a query matching all of the specified queries, where
  the matches occur within the specified distance from each other.
</api:summary><api:params><api:param name="queries" type="cts:query*"><api:param-description>
    A sequence of queries to match.
  </api:param-description><api:param-name>queries</api:param-name><api:param-type>cts.query[]</api:param-type></api:param><api:param name="distance" type="xs:double?" optional="true"><api:param-description>
    A distance, in number of words, between any two matching queries.
    The results match if two queries match and the distance between the
    two matches is equal to or less than the specified distance. A
    distance of 0 matches when the text is the exact same text or when
    there is overlapping text (see the third example below). A negative
    distance is treated as 0.  The default value is 10.
  </api:param-description><api:param-name>distance</api:param-name><api:param-type>Number?</api:param-type></api:param><api:param name="options" type="xs:string*" optional="true"><api:param-description>
    Options to this query.  The default value is ().
    <p xmlns="http://www.w3.org/1999/xhtml">
      Options include:</p>
      <blockquote xmlns="http://www.w3.org/1999/xhtml"><dl>
        <dt>"ordered"</dt>
        <dd>Any near-query matches must occur in the order of
            the specified sub-queries.</dd>
        <dt>"unordered"</dt>
        <dd>Any near-query matches will satisfy the query,
        regardless of the order they were specified.  </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="distance-weight" type="xs:double?" optional="true"><api:param-description>
    A weight attributed to the distance for this query.  Higher
    weights add to the importance of distance (as opposed to term matches)
    when the relevance order is calculated.  The default value is 1.0. The
    weight should be between 64 and -16.
    Weights greater than 64 will have the same effect as a
    weight of 64.
    Weights less than the absolute value of 0.0625 (between -0.0625 and
    0.0625) are rounded to 0, which means that they do not contribute to the
    score.  This parameter has no effect if the <code xmlns="http://www.w3.org/1999/xhtml">word positions</code>
    index is not enabled.
  </api:param-description><api:param-name>distance-weight</api:param-name><api:param-type>Number?</api:param-type></api:param></api:params><api:return>cts.nearQuery</api:return><api:usage>
  <p xmlns="http://www.w3.org/1999/xhtml">If the options parameter contains neither "ordered" nor "unordered",
  then the default is "unordered".</p>
  <p xmlns="http://www.w3.org/1999/xhtml">The <code>word positions</code> index will speed the performance of
  queries that use <code>cts:near-query</code>. The <code>element word
  positions</code> index will speed the performance of element-queries
  that use <code>cts:near-query</code>.</p>
  <p xmlns="http://www.w3.org/1999/xhtml">If you use <code>cts:near-query</code> with a field, the distance
  specified is the distance in the whole document, not the distance
  in the field.  For example, if the distance between two words is 20 in
  the document, but the distance is 10 if you look at a view of the document
  that only includes the elements in a field, a <code>cts:near-query</code>
  must have a distance of 20 or more to match; a distance of 10 would not
  match.</p>
  <p xmlns="http://www.w3.org/1999/xhtml">If you use <code>cts:near-query</code> with
  <code>cts:field-word-query</code>, the distance supplied in the near query
  applies to the whole document, not just to the field.  For details, see
  <a href="./cts:field-word-query"><code>cts:field-word-query</code></a>.</p>
  <p xmlns="http://www.w3.org/1999/xhtml">Expressions using the <code>ordered</code> option are more efficient
  than those using the <code>unordered</code> option, especially if they
  specify many queries to match.</p>
</api:usage><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
// The following query searches for paragraphs containing
// both "MarkLogic" and "Server" within 3 words of each
// other, given the following document in a database:

// { "text": "MarkLogic Server is an enterprise-class database."}

cts.search(cts.nearQuery(
    [cts.wordQuery("MarkLogic"),
     cts.wordQuery("Server")],
     3))

=&gt; A ValueIterator containing the document

</pre></api:example><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
var x = xdmp.unquote('&lt;p&gt;Now is the winter of our discontent&lt;/p&gt;');
cts.contains(x, cts.nearQuery(
                    ["discontent", "winter"],
                    3, "ordered"))

=&gt; false because "discontent" comes after "winter"

var x = xdmp.unquote('&lt;p&gt;Now is the winter of our discontent&lt;/p&gt;');
cts.contains(x, cts.nearQuery(
                    ["discontent", "winter"],
                    3, "unordered"))

=&gt; true because the query specifies "unordered",
        and it is still a match even though
        "discontent" comes after "winter"

</pre></api:example><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
var x = xdmp.unquote('&lt;p&gt;Now is the winter of our discontent&lt;/p&gt;');
cts.contains(x, cts.nearQuery(
                    ["is the winter", "winter of"],
                    0))

=&gt; true because the phrases overlap

var x = xdmp.unquote('&lt;p&gt;Now is the winter of our discontent&lt;/p&gt;');
cts.contains(x, cts.nearQuery(
                    ["is the winter", "of our"],
                    0))

=&gt; false because the phrases do not overlap
         (they have 1 word distance, not 0)

</pre></api:example></api:function></api:function-page>