search.parse

search.parse(
   qtext as String[],
   [options as element(search.options)?],
   [output as String?]
) as Node?

Summary

This function parses query text according to given options and returns the appropriate cts:query XML. If the query string to be parsed does not conform to the query grammar, the behavior differs, depending on the value of $output:

MarkLogic recommends that you use cts:query for parsing text.

Parameters
qtext The query text to parse. This may be a sequence, to accommodate more complex search UI. Multiple query texts will be ANDed together.
options Options to define the search grammar and control the search. See the description for $options for the function search:search.
output The format of the parsed query, one of schema-element(cts:query) (default), cts:query, search:query, or cts:annotated-query (deprecated). See the Usage Notes for details.

Usage Notes

Use the $output parameter to specify one of the following output formats. All the formats are suitable for use with search:resolve, but "cts:query" is most efficient if you do not need to transform or traverse the query. Passing a cts:query object avoids an extra parsing step.

Example

const search = require('/MarkLogic/appservices/search/search');

search.parse("tag:technology AND format:pdf",
   fn.head(xdmp.unquote('<options xmlns="http://marklogic.com/appservices/search">' +
     '<constraint name="tag">' +
       '<collection/>' +
     '</constraint>' + 
     '<constraint name="format">' +
        '<value>' +
          '<element ns="http://purl.org/dc/elements/1.1/" name="tag"/>' +
        '</value>' +
     '</constraint>' +
   '</options>')).root); 

// ==>
// <cts:and-query xmlns:cts="http://marklogic.com/cts">
// <cts:collection-query>
// <cts:uri>technology</cts:uri>
// </cts:collection-query>
// <cts:element-value-query>
// <cts:element xmlns:_1="http://purl.org/dc/elements/1.1/">_1:tag</cts:element>
// <cts:text xml:lang="en">pdf</cts:text>
// </cts:element-value-query>
// </cts:and-query>
   

Example

const search = require('/MarkLogic/appservices/search/search');

search.parse("tag:technology AND format:pdf",
   fn.head(xdmp.unquote('<options xmlns="http://marklogic.com/appservices/search">' +
     '<constraint name="tag">' +
       '<collection/>' +
     '</constraint>' +
     '<constraint name="format">' +
        '<value>' +
          '<element ns="http://purl.org/dc/elements/1.1/" name="tag"/>' +
        '</value>' +
     '</constraint>' +
   '</options>')).root, "search:query");

// ==> 
// <search:query xmlns:search="http://marklogic.com/appservices/search">
//  <search:and-query>
//    <search:term-query>
//      <search:text>hello</search:text>
//    </search:term-query>
//    <search:and-query>
//      <search:collection-constraint-query>
//        <search:constraint-name>tag</search:constraint-name>
//        <search:uri>technology</search:uri>
//      </search:collection-constraint-query>
//      <search:value-constraint-query>
//        <search:constraint-name>format</search:constraint-name>
//        <search:text>pdf</search:text>
//      </search:value-constraint-query>
//    </search:and-query>
//  </search:and-query>
// </search:query>
   
Powered by MarkLogic Server | Terms of Use | Privacy Policy