MarkLogic temporal:document-insert执行失败求助
问题:MarkLogic temporal:document-insert 报错 XDMP-NOMATCH
执行以下插入代码时失败:
let $root := <tempdoc> <content>v1-content here</content> </tempdoc> return temporal:document-insert("entity:person", "koolorder.xml", $root)
报错信息:
[1.0-ml] XDMP-NOMATCH: xdmp:eval("xquery version "1.0-ml"; import module namesp...", (), <options xmlns="xdmp:eval"><database>7821828681277378166</database>...</options>) -- No matches for cts:field-reference("systemStart",("type=dateTime")) in <tempdoc><content>v1-content here</content></tempdoc>
问题原因
报错核心是:cts:field-reference("systemStart")指向的是文档内容中的字段,但你期望MarkLogic自动生成的systemStart/systemEnd属于文档元数据(存储在xdmp:metadata中,而非文档内容里),因此普通字段引用无法匹配到元数据字段,导致插入失败。
另外,你手动创建的是普通数据库字段(admin:database-field)和普通范围索引,而非元数据字段对应的索引,这也会导致系统无法识别元数据中的时间字段。
解决方案
1. 删除错误创建的普通字段
先清理之前创建的systemStart/systemEnd普通字段,避免冲突:
xquery version "1.0-ml"; import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy"; let $config := admin:get-configuration() let $dbid := xdmp:database() let $config := admin:database-remove-field($config, $dbid, "systemStart") let $config := admin:database-remove-field($config, $dbid, "systemEnd") return admin:save-configuration($config)
2. 创建元数据字段的dateTime范围索引
针对元数据中的systemStart/systemEnd创建专用范围索引:
xquery version "1.0-ml"; import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy"; let $config := admin:get-configuration() let $dbid := xdmp:database() let $systemStart := admin:database-metadata-range-field-index("dateTime", "", "systemStart", false()) let $systemEnd := admin:database-metadata-range-field-index("dateTime", "", "systemEnd", false()) let $config := admin:database-add-range-field-index($config, $dbid, $systemStart) let $config := admin:database-add-range-field-index($config, $dbid, $systemEnd) return admin:save-configuration($config)
3. 重新创建temporal轴(使用元数据字段引用)
创建轴时,用cts:metadata-field-reference指向元数据字段,而非普通字段引用:
xquery version "1.0-ml"; import module namespace temporal = "http://marklogic.com/xdmp/temporal" at "/MarkLogic/temporal.xqy"; let $systemResult := temporal:axis-create( "systemAxes", cts:metadata-field-reference("systemStart"), cts:metadata-field-reference("systemEnd") ) return ()
4. 重新创建temporal集合并插入文档
xquery version "1.0-ml"; import module namespace temporal = "http://marklogic.com/xdmp/temporal" at "/MarkLogic/temporal.xqy"; let $collectionResult := temporal:collection-create("entity:person", "systemAxes") return $collectionResult
再次执行你原来的插入代码即可成功,此时MarkLogic会自动将systemStart/systemEnd写入文档元数据,无需手动在文档内容中添加。
内容的提问来源于stack exchange,提问作者Marc Messéant
相关产品推荐
相关产品推荐

