[英]How to add collections in transformations when writing(creating) a Document in MarkLogic
I wrote a transformation in xquery which unquotes an XML-String and inserts a element with its content. 我在xquery中编写了一个转换,该转换取消了XML-String的引用,并插入了包含其内容的元素。 This works fine.
这很好。 I need to create a collection dependant on the root element of this element as well.
我还需要创建一个依赖于该元素的根元素的集合。 I can't do this on new documents as xdmp:document-add-collections() is not working.
我无法在新文档上执行此操作,因为xdmp:document-add-collections()无法正常工作。 How do I add the collection to new Documents in transformations?
如何在转换中将集合添加到新文档?
Here my ServerSide xQuery Code: 这是我的ServerSide xQuery代码:
xquery version "1.0-ml";
module namespace transform = "http://marklogic.com/rest-api/transform/smtextdocuments";
import module namespace mem = "http://xqdev.com/in-mem-update" at '/MarkLogic/appservices/utils/in-mem-update.xqy';
declare function transform(
$context as map:map,
$params as map:map,
$content as document-node()
) as document-node()
{
let $uri := base-uri($content)
let $doccont := $content/smtextdocuments/documentcontent
let $newcont := xdmp:unquote($doccont)
let $contname := node-name($newcont/*)
let $result := if ( exists($content/smtextdocuments/content))
then mem:node-replace($content/smtextdocuments/content, <content>11{$newcont}</content>)
else mem:node-insert-after($doccont, <content>{$newcont}</content>)
let $log := xdmp:log($content)
return (
$result,
xdmp:document-add-collections($uri, fn:string($contname)),
xdmp:document-remove-collections($uri, "raw")
)
};
The script ist running with the java api (4.0.4) create methode via parameter ServerTransform transform. 与Java api(4.0.4)一起运行的脚本ist通过参数ServerTransform转换创建方法。 As per documentation the transformation script is running before the document is stored in the Database.
根据文档,转换脚本在文档存储在数据库中之前正在运行。
Its a new document; 它是一个新文件; I need to transform the content and then create the collection.
我需要转换内容,然后创建集合。
I can see the document after the create, the content is available. 创建后我可以看到文档,内容可用。 Just the collection is missing.
只是集合丢失了。 I can try xdmp:document-insert method but is it correct writing the document while create is running?.
我可以尝试使用xdmp:document-insert方法,但是在create运行时编写文档是否正确?
The transform mechanism of the Java API / REST API takes responsibility for the document write. Java API / REST API的转换机制负责文档的编写。 At present, there's no way for the transform to supply collections to the writer.
目前,转换无法将集合提供给作者。 That would be a reasonable request for enhancement.
这将是一个合理的增强要求。
The transform shouldn't attempt to write the document, because the writer would also attempt to write the same document. 转换不应尝试编写文档,因为编写者还将尝试编写同一文档。
One alternative would be to transform the document in Java before writing it and specify the collection as part of the write request. 一种替代方法是在编写文档之前先用Java对其进行转换,然后将集合指定为写请求的一部分。
Another alternative would be to rewrite the transform as a resource service extension, implement the write within the resource service extension, and modify the Java client to send the document to the resource service extension. 另一种选择是将转换重写为资源服务扩展,在资源服务扩展内实现写入,并修改Java客户端以将文档发送到资源服务扩展。
Depending on the model, a final alternative might be to use a range index on an element within the document to collect documents into sets instead of using a collection on the document. 根据模型,最终的替代方法可能是在文档中的元素上使用范围索引以将文档收集到集合中,而不是在文档上使用集合。
Hoping that helps, 希望能有所帮助,
What do you mean by "new documents"? “新文件”是什么意思? Is the document already inserted into the MarkLogic database at the time you are adjusting the collections of it?
调整文档集合时,文档是否已插入MarkLogic数据库? If not, you may want to modify your return to
($result, xdmp:document-insert($uri, $result, xdmp:default-permissions(), fn:string($contname)) )
for that case. 否则,您可能需要修改该情况的返回值
($result, xdmp:document-insert($uri, $result, xdmp:default-permissions(), fn:string($contname)) )
。
Otherwise, can you edit your question to share the error or problem more specifically you are facing? 否则,您可以编辑问题以更具体地分享您面临的错误或问题吗?
It is a pity that REST transforms do not allow this, like MLCP transforms do. 遗憾的是,REST转换不允许这样做,就像MLCP转换一样。 Until changed you have the options drawn by ehennum, or you can consider delaying adding of collections to a pre- or post-commit trigger.
在更改之前,您可以使用ehennum绘制的选项,或者可以考虑延迟将集合添加到提交前或提交后的触发器。 It takes some overhead, but it sometimes makes perfect sense to do something like that in a trigger, since it makes sure it is always enforced, and a good place to do content validation, audit logging, and things like that as well.
这会花费一些开销,但是在触发器中执行类似的操作有时很有意义,因为它确保始终执行,并且是进行内容验证,审核日志记录以及类似操作的好地方。
HTH! HTH!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.