1
votes

I'm trying to transform my search results into some-other format using XQuery. I'm using example XQuery transformation as in documentation like:

xquery version "1.0-ml";
module namespace example = "http://marklogic.com/rest-api/transform/add-attr";

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


declare function example:transform(
  $context as map:map,
  $params as map:map,
  $content as document-node()
) as document-node()
{
    let $searchResult := $content
    let $docs := fn:doc($searchResult/search:result/@uri)
    return $docs
};

Installed transform using

curl --anyauth --user user:password -X PUT -d@"./add-attr.xqy" -H "Content-type: application/xquery" 'http://IP-ADD:port/LATEST/config/transforms/add-attr?trans:name=string\?&trans:value=string\?'

Now, when I try to search for any string with transform like:

http://IP-ADD:port/v1/search?q=xyz&transform=add-attr

I get the following error:

<rapi:error 
xmlns:rapi="http://marklogic.com/rest-api">
  <rapi:status-code>500</rapi:status-code>
  <rapi:status>INTERNAL ERROR</rapi:status>
  <rapi:message-code>XDMP-AS</rapi:message-code>
  <rapi:message>XDMP-AS: (err:XPTY0004) $service($context, $service-params, $input) -- Invalid coercion: ("{&amp;quot;root&amp;quot;:{&amp;quot;claim&amp;quot;:[{&amp;quot;type&amp;quot;:&amp;quot;In...", ",") as document-node().  See the MarkLogic server error log for further detail.</rapi:message>
</rapi:error>

I'm trying to convert each document into JSON from search result in transform function (I didn't include that code above). What do I miss? Where I'm wrong? Can you please help me out?

Thanks in advance,
Prithvi

2

2 Answers

4
votes

The transform receives the entire search:response in one call. That means that $searchResult/search:result/@uri XPath gives you one page's worth of URIs (probably ten). You are returning them as a sequence of documents when the return type expects just one. You need to provide a wrapper for them, so that you have just the one document. Something like:

document {
  fn:doc($content/search:result/@uri)
}

Also, it looks like you want to work with the full documents, not just snippets. You can get those by adding this to your REST search options:

<transform-results apply="raw"/>
0
votes

the error is due to the fact that it is actually a text node, not a document node. if u want to fix this, you should probably change the expected parameter type to be an element node ( as element() ).