REST API返回文档内容

时间:2014-04-02 10:44:47

标签: marklogic

我尝试通过cts:uri-match返回所有文档。不仅是uri,还有整个文档。 所以我在/ app / customer /中放了一些文档,现在我创建了这个REST API扩展端点。但是当我无法通过这些文件返回实际文件时。

看起来我的查询正在运行("产生6个结果")但输出格式错误。

这是我的错误:

<rapi:error 
    xmlns:rapi="http://marklogic.com/rest-api">
    <rapi:status-code>400</rapi:status-code>
    <rapi:status>Bad Request</rapi:status>
    <rapi:message-code>RESTAPI-INVALIDRESULT</rapi:message-code>
    <rapi:message>RESTAPI-INVALIDRESULT: (err:FOER0000) Invalid result:  reason: GET     extension produced 6 results and 1 mime types: customers</rapi:message>
</rapi:error>

这是我的扩展程序:

xquery version "1.0-ml";

module namespace foo = "http://marklogic.com/rest-api/resource/customers";
declare namespace roxy = "http://marklogic.com/roxy";
declare namespace pub = "http://acme.com/foo/publisher/1.0";

declare 
%roxy:params("id=xs:string")

function foo:get(
    $context as map:map,
    $params  as map:map
) as document-node()*
{
    map:put($context, "output-types", "application/xml"),
    xdmp:set-response-code(200, "OK"),
    foo:getCustomersByXPath($params)
};

declare function foo:getCustomersByXPath(
    $params  as map:map
) as document-node()* {
    let $set := cts:uri-match("/app/customer/*")
    let $id := map:get($params,"id")
    for $x in doc($set)
    return doc($x)            
};

检查目录中的数据:

http://myserver:myport/v1/search?q=&directory=/app/customer/

结果:

<search:response snippet-format="snippet" total="7" start="1" page-length="10" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns=""  xmlns:search="http://marklogic.com/appservices/search">
  <search:result index="1" uri="/app/customer/10848614934359542547.xml" path="fn:doc(&quot;/app/customer/10848614934359542547.xml&quot;)" score="0" confidence="0" fitness="0" href="/v1/documents?uri=%2Fapp%2Fcustomer%2F10848614934359542547.xml" mimetype="text/xml" format="xml">
    <search:snippet/>
  </search:result>
  <search:result index="2" uri="/app/customer/7883534461919564626.json" path="fn:doc(&quot;/app/customer/7883534461919564626.json&quot;)" score="0" confidence="0" fitness="0" href="/v1/documents?uri=%2Fapp%2Fcustomer%2F7883534461919564626.json" mimetype="application/json" format="json">
    <search:snippet/>
  </search:result>
  <search:result index="3" uri="/app/customer/10893316875648096464.json" path="fn:doc(&quot;/app/customer/10893316875648096464.json&quot;)" score="0" confidence="0" fitness="0" href="/v1/documents?uri=%2Fapp%2Fcustomer%2F10893316875648096464.json" mimetype="application/json" format="json">
    <search:snippet/>
  </search:result>
  <search:result index="4" uri="/app/customer/11529967549112309613.json" path="fn:doc(&quot;/app/customer/11529967549112309613.json&quot;)" score="0" confidence="0" fitness="0" href="/v1/documents?uri=%2Fapp%2Fcustomer%2F11529967549112309613.json" mimetype="application/json" format="json">
    <search:snippet/>
  </search:result>
  <search:result index="5" uri="/app/customer/12616183128326713409.xml" path="fn:doc(&quot;/app/customer/12616183128326713409.xml&quot;)" score="0" confidence="0" fitness="0" href="/v1/documents?uri=%2Fapp%2Fcustomer%2F12616183128326713409.xml" mimetype="text/xml" format="xml">
    <search:snippet/>
  </search:result>
  <search:result index="6" uri="/app/customer/2938594927859036749.json" path="fn:doc(&quot;/app/customer/2938594927859036749.json&quot;)" score="0" confidence="0" fitness="0" href="/v1/documents?uri=%2Fapp%2Fcustomer%2F2938594927859036749.json" mimetype="application/json" format="json">
    <search:snippet/>
  </search:result>
  <search:result index="7" uri="/app/customer/1602860626261524046.json" path="fn:doc(&quot;/app/customer/1602860626261524046.json&quot;)" score="0" confidence="0" fitness="0" href="/v1/documents?uri=%2Fapp%2Fcustomer%2F1602860626261524046.json" mimetype="application/json" format="json">
    <search:snippet/>
  </search:result>
  <search:qtext/>
  <search:metrics>
    <search:query-resolution-time>PT0.005074S</search:query-resolution-time>
    <search:facet-resolution-time>PT0.000077S</search:facet-resolution-time>
    <search:snippet-resolution-time>PT0.000938S</search:snippet-resolution-time>
    <search:total-time>PT0.182643S</search:total-time>
  </search:metrics>
</search:response>

1 个答案:

答案 0 :(得分:3)

get()扩展函数可以返回多个文档,这些文档可以有不同的mime类型。

尝试将output-types键设置为每个返回文档具有一个mime类型字符串的序列。在您的情况下,可能是每个字符串都是&#34; application / xml&#34;

希望有帮助,

Erik Hennum