How to add collation in an existing element range index in marklogic

218 views Asked by At

I have already created an element range index in MarkLogic 7 with scalar type gYear. I wrote the following script, ran it and successfully created the index,

xquery version "1.0-ml";
import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";
let $newRangeIndexes := (
    <range-element-index xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://marklogic.com/xdmp/database">
      <scalar-type>gYear</scalar-type>
      <namespace-uri>http://www.dummy.com/namespaces/dummy</namespace-uri>
      <localname>dummyValue</localname>
      <range-value-positions>false</range-value-positions>
    </range-element-index>
)
  let $config := admin:get-configuration()
  let $dbid := xdmp:database()
  let $newConfig :=  admin:database-add-range-element-index($config, $dbid, $newRangeIndexes)
  return
     admin:save-configuration($newConfig)*

It does not include a collation element as this is not mandatory for the gYear scalar type. After creating this index the default value of the element invalid-values is reject.

Now I have a requirement where I have to update this existing index (I have to modify the value of invalid-values to ignore). When I am trying to do this I am getting an error due to the absence of the collation element. I have found out only one solution which is to delete the index and recreate which is not acceptable in my case.

So I would like to add an empty collation element in this existing index first and then I will apply my changes for invalid-values. So is there any other way out (except deleting the existing one) to update an existing element range index configuration to add an empty collation element?

1

There are 1 answers

5
grtjn On

I would recommend not creating the XML definition for indexes manually, but using the admin functions to do so. In your case that would have been admin:database-range-element-index.

I would not worry too much about deleting and recreating indexes. If you combine a delete and a add, and save both at the same time, MarkLogic will only apply changes as needed.

To replace an existing definition for a particular element with a different one, you could do:

xquery version "1.0-ml";

import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";

declare namespace db = "http://marklogic.com/xdmp/database";

let $config := admin:get-configuration()
let $database-id := xdmp:database()

let $existing-index := admin:database-get-range-element-indexes($config, $database-id)[
  db:namespace-uri = "http://www.dummy.com/namespaces/dummy"
    and db:localname = "dummyValue"
]
let $config := admin:database-delete-range-element-index($config, $database-id, $existing-index)

let $config :=  admin:database-add-range-element-index($config, $database-id, admin:database-range-element-index(
  "gYear",
  "http://www.dummy.com/namespaces/dummy",
  "dummyValue",
  (), 
  fn:false(),
  "reject"
))

return
  admin:save-configuration($config)

HTH!