'Custom Search Results in REST MarkLogic

So new to MarkLogic am stuck and not finding the documentation of use. I know what i need to do, just do not know how to do it.

I have a keyvalue? search on my REST server which returns ML's standard search results and XML snippet. I want to create my own custom search result which will output a title element for my XML files.

I am aware that i need to create an XSLT transformation document and upload that to the server but do not know how to target ML's search function or how to write this out.

I have basic knowledge of XSLT, if i just created something that targets each files title using xPath will this work, or does ML require use of their custom functions?

I know its a bit broad, but hopefully someone can point steer me.



Solution 1:[1]

You can use extract-metadata in your search options with search:search or the /v1/search/ REST API endpoint to include the title element in a metadata element or JSON property in your results:

import module namespace search = "http://marklogic.com/appservices/search"
  at "/MarkLogic/appservices/search/search.xqy";

search:search(
  "my query string",
  <options xmlns="http://marklogic.com/appservices/search">
    <extract-metadata>
      <qname elem-ns="" elem-name="title"/>
    </extract-metadata>
  </options>)

If you need more flexibility, you specify a custom snippet implementation or a results decorator function in your search options.

Solution 2:[2]

Is this key-value or full text? For key-value you could use XPath. Any XPath that starts with / or // or fn:collection() or fn:doc() will search the entire database. You can search specific document(s) or collection(s) too.

For full text you'd probably want to use https://docs.marklogic.com/search:search - or possibly https://docs.marklogic.com/cts:search for really low-level control.

There's some example code using search:search from XSL at https://github.com/marklogic/RunDMC which might help. It doesn't use the REST API: it's a traditional form-submit web page. But the view/search.xsl code might give you some idea how to call the search API from XSLT.

That RunDMC code might also help you if you need to call XSL from XQuery: take a look at controller/transform.xqy.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 joemfb
Solution 2 mblakele