cts.nearQuery( queries as cts.query[], [distance as Number?], [options as String[]], [distance-weight as Number?] ) as cts.nearQuery
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 document in a database: // { "text": "MarkLogic Server is an enterprise-class database."} cts.search(cts.nearQuery( [cts.wordQuery("MarkLogic"), cts.wordQuery("Server")], 3)) => A Sequence containing the document
const x = xdmp.unquote('<p>Now is the winter of our discontent</p>'); cts.contains(x, cts.nearQuery( ["discontent", "winter"], 3, "ordered")) => false because "discontent" comes after "winter" const x = xdmp.unquote('<p>Now is the winter of our discontent</p>'); cts.contains(x, cts.nearQuery( ["discontent", "winter"], 3, "unordered")) => true because the query specifies "unordered", and it is still a match even though "discontent" comes after "winter"
const x = xdmp.unquote('<p>Now is the winter of our discontent</p>'); cts.contains(x, cts.nearQuery( ["is the winter", "winter of"], 0)) => true because the phrases overlap const x = xdmp.unquote('<p>Now is the winter of our discontent</p>'); cts.contains(x, cts.nearQuery( ["is the winter", "of our"], 0)) => false because the phrases do not overlap (they have 1 word distance, not 0)
const x = xdmp.unquote('<p>Now is the winter of our discontent</p>'); cts.contains(x, cts.nearQuery( ["winter", "discontent"], 5, ("ordered", "minimum-distance=4"))) => false because the distance between the queries is greater than the minimum distance const x = xdmp.unquote('<p>Now is the winter of our discontent</p>'); cts.contains(x, cts.nearQuery( ["winter", "discontent"], 5, ("ordered", "minimum-distance=3"))) => true because the distance between the queries is less than or equal to the minimum distance