cts.fieldWordQuery( $field-name as String, $text as String, [$options as String], [$weight as Number?] ) as cts.fieldWordQuery
Returns a query matching text content containing a given phrase in the
specified field. If the specified field does not exist,
cts:field-word-query throws an exception. A field
is a named object that specified elements to include and exclude
from a search, and can include score weights for any included elements.
You create fields at the database level using the Admin Interface. For
details on fields, see the chapter on "Fields Database Settings" in the
|$field-name||One or more field names to search over. If multiple field names are supplied, the match can be in any of the specified fields (or-query semantics).|
|$text||The word or phrase to match. If multiple strings are specified, the query matches if any of the words or phrases match (or-query semantics).|
Options to this query. The default is ().
|$weight||A weight for this query. Higher weights move search results up in the relevance order. The default 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.|
If you use
cts:field-word-query, the distance supplied in the near query
applies to the whole document, not just to the field. For example, if
you specify a near query with a distance of 3, it will return matches
when the words or phrases are within 3 words in the whole document,
even if some of those words are not in the specified field. For a code
example illustrating this, see the
Phrases are determined based on words being next to each other (word positions with a distance of 1) and words being in the same instance of the field. Because field word positions are determined based on the fragment, not on the field, field phrases cannot span excluded elements (this is because MarkLogic Server breaks out of the field when it encounters the excluded element and start a new field when it encounters the next included element). Similarly, field phrases will not span included sibling elements. The second code example below illustrates this.
Field phrases will automatically phrase-through all child elements of
an included element, until it encounters an explicitly excluded
below illustrates this.
An example of when this automatic phrase-through behavior might be
convenient is if you create a field that includes only the element
ABSTRACT. Then all child elements of
are included in the field, and phrases would span all of the child
elements (that is, phrases would "phrase-through" all the child elements).
Negative "min-occurs" or "max-occurs" values will be treated as 0 and non-integral values will be rounded down. An error will be raised if the "min-occurs" value is greater than the "max-occurs" value.
cts.search(cts.fieldWordQuery("myField", "my phrase")); => a list of documents that contain the phrase "my phrase" in the field "myField". The field must exist in the database against which this query is evaluated.
// Assume the database has a field named // "buzz" with the element "buzz" // included and the element "baz" excluded. var x = xdmp.unquote( '<hello>word1 word2 word3\n\ <buzz>word4 word5</buzz>\n\ <baz>word6 word7 word8</baz>\n\ <buzz>word9 word10</buzz>\n\ </hello>').next().value; var res = new Array(); res.push(cts.contains(x, cts.nearQuery( [cts.fieldWordQuery("buzz", "word5"), cts.fieldWordQuery("buzz", "word9")], 3))); res.push(cts.contains(x, cts.nearQuery( [cts.fieldWordQuery("buzz", "word5"), cts.fieldWordQuery("buzz", "word9")], 4))); res.push(cts.contains(x, cts.fieldWordQuery("buzz", "word5 word9"))); res; // Returns the array [false, true, false]. // The first part does not match because // the distance between "word5" and "word9" // is 4. This is because the distance is // calculated based on the whole node (if the // document was in a database, based on the // fragment), not based on the field. The // second part specifies a distance of 4, and // therefore matches and returns true. The third // part does not match because the phrase is // based on the entire node, not on the field, // and there are words between "word5" and "word9" // in the node (even though not in the field).
// Assume the database has a field named // "buzz" with the element "buzz" // included and the element "baz" excluded. var x = xdmp.unquote( '<hello>\n\ <buzz>word1 word2\n\ <gads>word3 word4 word5</gads>\n\ <zukes>word6 word7 word8</zukes>\n\ word9 word10\n\ </buzz>\n\ </hello>').next().value; cts.contains(x, cts.fieldWordQuery("buzz", "word2 word3")); // Returns "true" because the children of // "buzz" are not excluded, and are therefore // automatically phrased through.