marklogic_coder
marklogic_coder

Reputation: 1510

Clear collections is not working in Java API

We are using MarkLogic v10 and Java API v4.1.0. Our requirement is to clear collections from the specific document,

we are using below code snippet to clear all the collections,

XMLDocumentManager xmlDocumentManager = client.newXMLDocumentManager();

DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle();
xmlDocumentManager.readMetadata("/test/inventory", metadataHandle);

DOMHandle domHandle = new DOMHandle();
xmlDocumentManager.read("/test/inventory", domHandle);

metadataHandle.getCollections().clear();
xmlDocumentManager.write("/test/inventory", metadataHandle, domHandle);

After running the above snippet, it should remove all the collections from the document but it is not happening.

Please help me to make this work.

Upvotes: 1

Views: 77

Answers (1)

ehennum
ehennum

Reputation: 7335

Copying the resolution from the bug in case it's useful for others.

Clear collections only clears locally for the in-memory DocumentMetadataHandle in the Java API client of the REST API.

The behavior of the REST API invoked by the Java API depends on the configuration of the update policy of the REST API server:

  • MERGE_METADATA preserves the current metadata of an existing document for any metadata category that's not sent by the client.

  • OVERWRITE_METADATA ignores the current metadata of an existing document

Changing the update policy to OVERWRITE_METADATA should yield the expected behavior.

Here's the Java API code for making that change:

    ServerConfigurationManager configMgr = client.newServerConfigManager();
    configMgr.readConfiguration();
    configMgr.setUpdatePolicy(ServerConfigurationManager.UpdatePolicy.OVERWRITE_METADATA);
    configMgr.writeConfiguration();

Note that

  • The server configuration will remain in effect until changed. Typically, the configuration is never changed after setting.

  • In recent releases of the MarkLogic server, the default update policy changed from MERGE_METADATA to OVERWRITE_METADATA

Thanks to Shivling for digging into the issue.

Upvotes: 1

Related Questions