2013-10-03 38 views
2

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

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列表重复索引元素的数量,示例日志

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 

回答

1

的问题是,你的for循环local:create-index()创造CONFIGS序列,每个$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(),你可以得到一个递归函数相同的效果。

+0

我在local:create-index()函数中添加了'xdmp:set()'命令到for循环返回行。我还将代码导出到日志中。 – user2844108