0

I've been working through a tutorial to build a rest service with marklogic. I've built a simple example through using roxy deploy tool and calling ml ext . My problem is trying to grab a specific element out of a document via rest call. Here is what i have so far:

    declare
%roxy:params("dataType=xs:string","pNumber=xs:number", "sNumber=xs:string", "searchTerm=xs:string")
function jad:get(
  $context as map:map,
  $params  as map:map
) as document-node()*
{
  map:put($context, "output-types", "application/xml"),
  map:put($context, "accept-types", "multipart/mixed"),
  map:put($context, "output-status", (200, "OK")),
  let $doc := doc('testNew.xml')
  let $docs := $doc//stuff/pData/sData/headerData/bData/sitData[contains(., 'word')]
  let $c := json:config("custom"),

   $_ := map:put($c,"whitespace", "ignore"),
   $_ := map:put( $c , "camel-case" , fn:true() )
   let $results := search:search("word",
   <options xmlns="http://marklogic.com/appservices/search">
     <transform-results apply="raw"/>
   </options>)
return document{$doc//stuff/pData/sData/headerData/bData/sitData[contains(., 'word')]}

this has gone through many iterations but ultimately the problem is I can't seem to simply return a document along an xpath within rest. When I call upon this url I get an error like this:

enter image description here

After reading through many stackoverflow links this seems to stem from the Xpath only returning a snippet. The problem is I don't know how to return the full results. I had read somewhere that I could set a property somewhere in my "rest-api" folder to be but this hasn't stopped this problem. So what I would ultimately like is the possibility to search a specific document's internal structure for keywords.

EDIT: would I be better off using RXQ for this purpose?

4

1 回答 1

1

根据您的评论:

如果我想在数据库中搜索特定文档,我不确定如何在不使用 xpath 的情况下完成该操作

如果您知道要返回的文档的 URI,则可以使用fn:doc($uri).

如果我有来自文档的多个 xml 片段,我怎样才能将它们作为单个文档发送

这应该这样做:

let $results := search:search("word",
  <options xmlns="http://marklogic.com/appservices/search">
    <transform-results apply="raw"/>
  </options>)
return document { $results }

search:search返回单个 XML 元素;您需要返回一个文档节点,因此我将搜索结果包装在document { }.

如果要返回多个元素,则需要将它们包含在一个父元素中,因为 XML 文档可能只有一个根:

return document{
  <root>{
    $doc//stuff/pData/sData/headerData/bData/sitData[contains(., 'word')]
  }</root>
}

另外,查看您的 HTTP 调用:

http://localhost:8040/v1/resources/decosta?dataType=thing&policyNumber=1234&searchTerm=thisthingrighthere

REST API 扩展的参数需要有一个“rs:”前缀,以区别于 REST API 本身的参数。因此,您的 URL 应如下所示:

http://localhost:8040/v1/resources/decosta?rs:dataType=thing&rs:policyNumber=1234&rs:searchTerm=thisthingrighthere

在您的扩展中,您可以像这样访问参数:

let $data-type := map:get($params, "dataType")
于 2017-03-16T19:59:46.700 回答