Loading TOC...

MarkLogic 9 Product Documentation
search:parse

search:parse(
   $qtext as xs:string+,
   [$options as element(search:options)?],
   [$output as xs:string?]
) as element()?

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

xquery version "1.0-ml";
import module namespace search = "http://marklogic.com/appservices/search"
     at "/MarkLogic/appservices/search/search.xqy";

search:parse("tag:technology AND format:pdf",
   <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>
) 

==>

<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

xquery version "1.0-ml";
import module namespace search = "http://marklogic.com/appservices/search"
     at "/MarkLogic/appservices/search/search.xqy";

search:parse("hello tag:technology AND format:pdf",
   <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>, "search:query")

=> the following structured 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>

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.