cts.reverseQuery

cts.reverseQuery(
   nodes as Node[],
   [weight as Number?]
) as cts.reverseQuery

Summary

Construct a query that matches serialized cts queries, based on a set of model input nodes. A serialized query matches if it would match the model nodes. Use with a cts.search or cts.contains over serialized cts.query nodes.

Parameters
nodes Model nodes that must be matchable by queries matched by this reverse query. See the Usage Notes for more details.
weight A weight for this query. This parameter has no effect because a reverse query does not contribute to score. That is, the score is always 0.

Usage Notes

A reverse query matches serialized cts:query nodes. Construct a reverse query from nodes that model what that serialized query should match, rather than passing in the target query. For example, to match queries for the word "hello", specify a node containing the word "hello" as the nodes parameter. See the example, below. Reverse queries are useful for creating alerting applications.

When evaluating a cts.reverseQuery on a set of nodes, the cts.similarQuery or cts.registeredQuery components of any stored query will match all nodes.

You can serialize a cts.query to an in-memory JSON string using xdmp.toJsonString. You can persist a serialized cts.query as JSON document by converting the query to a JavaScript object and passing the object to xdmp.documentInsert. For example:

    // Convert to a JSON string
    xdmp.toJsonString(cts.wordQuery('hello'))

    // Convert to JavaScript object and save in the database. The JS object
    // is implicitly converted into a JSON object node by the call.
    xdmp.documentInsert('/my/query.json', cts.wordQuery('hello').toObject());
   

A reverse query can match both the XML and JSON representations of a serialized query.

See Also

Example

const query = cts.wordQuery('hello');
const modelNode = xdmp.toJSON({bar:"hello"});
cts.contains(
  xdmp.toJSON(query.toObject()).root,
  cts.reverseQuery(modelNode)
);

// Returns true because the content contains a cts:word-query
// for "hello", which would match the model node. Here, the content is
// an in-memory node, but it could also be a document (or node) in the
// database.

Powered by MarkLogic Server | Terms of Use | Privacy Policy