Loading TOC...

MarkLogic 9 Product Documentation
search.snippet

search.snippet(
   result as Node,
   cts-query as schemaElement(cts.query),
   [options as element(search.transformResults)?]
) as element(search.snippet)

Summary

This function extracts matching text from the result node based on options, and returns the matches wrapped in a containing node, with highlights tagged.

Parameters
result A node from which to pull matching snippets from.
cts-query A serialized and annotated cts:query, typically the result of a call to search:parse.
options Options to define the search grammar and control the search. See description for $options for the function search:search. Note that you cannot specify the apply attribute on the transform-results option with search:snippet; to use a different snippetting function, use search:search or search:resolve instead.

See Also

Example

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

search.snippet(
 fn.head(xdmp.unquote('<html xmlns="http://www.w3.org/1999/xhtml">' +
     '<head>' +
        '<title>Page Title</title>' +
     '</head>' +
     '<body>' +
       '<div>Query terms in this div will be ignored for snippeting.</div>' +
       '<p>Text surrounding query terms is highlighted and truncated' +
               'according to configuration.</p>' +
     '</body>' +
     '</html>')).root,
      search.parse("terms"),
      fn.head(xdmp.unquote('<transform-results apply="snippet" xmlns="http://marklogic.com/appservices/search">' +
          '<per-match-tokens>30</per-match-tokens>' +
          '<max-matches>4</max-matches>' +
          '<max-snippet-chars>200</max-snippet-chars>' +
          '<preferred-matches>' +
              '<element name="p" ns="http://www.w3.org/1999/xhtml"/>' +
          '</preferred-matches>' +
      '</transform-results>')).root);

// ==>
// <search:snippet xmlns:search="http://marklogic.com/appservices/search">
//  <search:match path="/*:html">
//     Text surrounding query terms is highlighted and 
//     truncatedaccording to configuration.
//  </search:match>
// </search:snippet>
   

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