
cts:near-query( $queries as cts:query*, [$distance as xs:double?], [$options as xs:string*], [$distance-weight as xs:double?] ) as cts:near-query
Returns a query matching all of the specified queries, where the matches occur within the specified distance from each other.
If the options parameter contains neither "ordered" nor "unordered", then the default is "unordered".
The word positions index will speed the performance of
  queries that use cts:near-query. The element word
  positions index will speed the performance of element-queries
  that use cts:near-query.
If you use cts:near-query 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 cts:near-query
  must have a distance of 20 or more to match; a distance of 10 would not
  match. The same applies to minimum distance as well.
If you use cts:near-query with
  cts:field-word-query, the distance supplied in the near query
  applies to the whole document, not just to the field. This too applies to the
  minimum distance as well. For details, see
  cts:field-word-query.
Expressions using the ordered option are more efficient
  than those using the unordered option, especially if they
  specify many queries to match.
Minimum-distance and distances apply to each near-query match. Therefore, if minimum-distance is greater than distance there can be no matches.
 The following query searches for paragraphs containing
 both "MarkLogic" and "Server" within 3 words of each
 other, given the following paragraphs in a database:
  <p>MarkLogic Server is an enterprise-class
  database specifically built for content.</p>
  <p>MarkLogic is an excellent XML Content Server.</p>
  cts:search(//p,
    cts:near-query(
      (cts:word-query("MarkLogic"),
      cts:word-query("Server")),
      3))
  =>
  <p>MarkLogic Server is an enterprise-class
  database specifically built for content.</p>
let $x := <p>Now is the winter of our discontent</p>
return
cts:contains($x, cts:near-query(
                    ("discontent", "winter"),
                    3, "ordered"))
=> false because "discontent" comes after "winter"
let $x := <p>Now is the winter of our discontent</p>
return
cts:contains($x, cts:near-query(
                    ("discontent", "winter"),
                    3, "unordered"))
=> true because the query specifies "unordered",
        and it is still a match even though
        "discontent" comes after "winter"
let $x := <p>Now is the winter of our discontent</p>
return
cts:contains($x, cts:near-query(
                    ("is the winter", "winter of"),
                    0))
=> true because the phrases overlap
let $x := <p>Now is the winter of our discontent</p>
return
cts:contains($x, cts:near-query(
                    ("is the winter", "of our"),
                    0))
=> false because the phrases do not overlap
         (they have 1 word distance, not 0)
let $x := <p>Now is the winter of our discontent</p>
return
cts:contains($x, cts:near-query(
                    ("winter", "discontent"),
                    5, ("ordered", "minimum-distance=4")))
=> false because the distance between the queries is greater than the minimum
distance
let $x := <p>Now is the winter of our discontent</p>
return
cts:contains($x, cts:near-query(
                    ("winter", "discontent"),
                    5, ("ordered", "minimum-distance=3")))
=> true because the distance between the queries is less than or equal to the
minimum distance
Stack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.