Search code examples
marklogic

Marklogic fail to create element range index


Fail to create element range index. Error Invalid coercion range-element-index xmlns="http://marklogic.com/xdmp/database">...... as element(configuration)

I have over 100 million documents in database need to index "tr:ModifiedDate".

 <?xml  version="1.0" encoding="UTF-8"?>
 <mdra:Record Type="TR" xmlns:mdra="http://dvtech.com/mdra/record">
 <tr:Raw xmlns:tr="http://dvtech.com/mdra/record/tr">
        <tr:History>
           <tr:ModifiedDate>2009-03-18T09:07:23.000-04:00</tr:ModifiedDate>
           <tr:DrolsNdxDate>2002-07-05T18:33:26.000-04:00</tr:DrolsNdxDate>
        </tr:History>            

        <tr:TrlType>
           <tr:Code>0</tr:Code>
       </tr:TrlType>
</tr:Raw>  
</mdr:Record>

I create element-range-index on tr:ModifiedDate but fail to create it. An error shows Invalid coercion. I couldn't figure out what cause it.

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("IAD")

let $rangespec := admin:database-range-element-index("dateTime",                            
          "http://dvtech.com/mdra/record/tr",
          "ModifiedDate",       
          "http://marklogic.com/collation/",
          fn:false() )

 return
 admin:save-configuration($rangespec)

Solution

  • The admin library revolves around $config. You need to update that config, and then save the config, not just one index definition. You would use admin:database-add-range-element-index to do this. The correct way of doing it is like this:

    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("IAD")
    
    let $rangespec := admin:database-range-element-index(
      "dateTime",                            
      "http://dvtech.com/mdra/record/tr",
      "ModifiedDate",       
      "http://marklogic.com/collation/",
      fn:false()
    )
    
    let $config := admin:database-add-range-element-index($config, $dbid, $rangespec)
    return
      admin:save-configuration($config)
    

    You can add multiple range indexes with one call to that function.

    I'd also like to mention that there are deployment tools that can help with deploying range indexes into MarkLogic. A good example is ml-gradle.

    HTH!