我正在尝试创建一个为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