用于创建索引的XQuery脚本

时间:2013-10-03 22:28:22

标签: xquery marklogic

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

2 个答案:

答案 0 :(得分:1)

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

当您执行admin:save-configuration()时,您实际上已经传递了一系列配置,并且函数映射导致每个配置调用一次save-configuration()

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

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

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

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

答案 1 :(得分:0)

Roxy deployer将为您处理此问题。