2

我正在尝试创建一个为MarkLogic数据库创建索引的脚本。请注意,显示的索引只是要创建的脚本的一小部分。

xquery version "1.0-ml";
import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";
declare namespace xdmpdb = "http://marklogic.com/xdmp/database";

declare variable $databaseName as xs:string := "army-itam";
declare variable $os as xs:string := "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_OperatingSystem";
declare variable $pe as xs:string := "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_PhysicalElement";

declare variable $os_xml as node() :=
<range-element-index>
  <index>  
    <type>string</type>
    <name>Name</name>
  </index>
  <index>  
    <type>string</type>
    <name>Version</name>
  </index>  
</range-element-index>;

declare variable $pe_xml as node() :=
<range-element-index>
  <index>  
    <type>string</type>
    <name>Model</name>
  </index>
  <index>  
    <type>string</type>
    <name>Manufacturer</name>
  </index>
  <index>  
    <type>dateTime</type>
    <name>ModelTest</name>
  </index>
</range-element-index>;

declare function local:add-range-element-index($config as element(configuration), $dbname as xs:string, $namespace-uri as xs:string, $type as xs:string, $localname as xs:string)
{
try {
     let $dbid := admin:database-get-id($config, $dbname)
     let $range-index := admin:database-range-element-index($type, $namespace-uri, $localname, "http://marklogic.com/collation/", fn:false() )
     let $ExistingREindexes := fn:data(admin:database-get-range-element-indexes($config, $dbid)/xdmpdb:localname)
     let $config :=
        if ($localname = $ExistingREindexes) 
        then $config
        else admin:database-add-range-element-index($config, $dbid, $range-index)
     let $log := xdmp:log(fn:concat("ERI (", $localname, ") added"), "info")
     return $config
} catch($e) {
     (fn:concat("Error adding ERI: ", fn:string-join($localname,",")),xdmp:log(xdmp:quote($e)))
   }
};

declare function local:create-index($config as element(configuration), $namespace-uri as xs:string, $server as node())
{
try {
    let $log := xdmp:log(fn:concat("Creating indexs for (", $namespace-uri, ")"), "info")
    let $config :=
      for $results in $server//index
        let $type := xs:string($results//type/text())
        let $name := xs:string($results//name/text())
        return local:add-range-element-index($config, $databaseName, $namespace-uri, $type, $name) 
    return $config
} catch($e) {
     xdmp:log(xdmp:quote($e))
   }
};

declare function local:index-create($config as element(configuration))
{
try {
     let $config := local:create-index($config, $os, $os_xml)
     let $config := local:create-index($config, $pe, $pe_xml)
     return $config
} catch($e) {
     xdmp:log(xdmp:quote($e))
   }
};

let $config := admin:get-configuration()
let $config := local:index-create($config)
return admin:save-configuration($config)

当我运行这个脚本时,它只从每个定义的 xml 列表中创建一个索引,无论是第一个还是最后一个。当我检查日志时,该local:add-range-element-index函数声明它处理了所有元素。

此外,日志说第二个 xml 列表按索引元素的数量重复,例如 log

2013-10-03 15:20:52.291 Info: App-Services: Creating indexs for(http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_OperatingSystem)
2013-10-03 15:20:52.634 Info: App-Services: ERI (Name) added
2013-10-03 15:20:52.712 Info: App-Services: ERI (Version) added
2013-10-03 15:20:52.712 Info: App-Services: Creating indexs for (http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_PhysicalElement)
2013-10-03 15:20:52.751 Info: App-Services: ERI (Model) added
2013-10-03 15:20:52.789 Info: App-Services: ERI (Manufacturer) added
2013-10-03 15:20:52.856 Info: App-Services: ERI (ModelTest) added
2013-10-03 15:20:52.856 Info: App-Services: Creating indexs for (http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_PhysicalElement)
2013-10-03 15:20:52.895 Info: App-Services: ERI (Model) added
2013-10-03 15:20:52.932 Info: App-Services: ERI (Manufacturer) added
2013-10-03 15:20:52.970 Info: App-Services: ERI (ModelTest) added
4

2 回答 2

1

问题是您的 for 循环local:create-index()正在创建一系列配置,每个$server//index. 其中每一个都是原始配置加上您在循环的特定迭代中添加的内容。

当您执行 时admin:save-configuration(),您实际上是在向它传递一系列配置,并且函数映射会导致save-configuration()为每个配置调用一次。

我会声明local:create-index()明确返回element(configuration)以确保它只返回一个。

我还会在循环的每次迭代中使用xdmp:set()覆盖$config

xdmp:set($config, local:add-range-element-index($config, ...))

或者,如果您不想使用,则xdmp:set()可以使用递归函数获得相同的效果。

于 2013-10-03T22:55:17.970 回答
0

Roxy 部署程序将为您处理这个以及更多的事情。

于 2013-10-04T12:42:59.197 回答