swat
swat

Reputation: 81

How to identify empty value in MarkLogic

I want to find json documents which have json property productid with an empty array as value in MarkLogic.

Sample JSON

{ "productid":[], "sequenceid": "4022" }

I tried below query, but I am getting no results:

cts.uris("",null, 
  cts.andQuery([
    cts.collectionQuery("collection"),
    cts.jsonPropertyValueQuery("productid","")
  ])
)

Upvotes: 1

Views: 57

Answers (1)

rjrudin
rjrudin

Reputation: 2236

One option - first, run the following in qconsole against your Documents database:

declareUpdate();
xdmp.documentInsert("/a.json", { "productid":[]});
xdmp.documentInsert("/b.json", { "productid":["b"]});
xdmp.documentInsert("/c.json", { "productid":[1]});

Then enable "one character searches" in the Admin app for the Documents database.

Then try this in another qconsole buffer:

cts.search(cts.jsonPropertyScopeQuery("productid", "*"))

You'll get back "/b.json", as the wildcard is matching on the text in the array in that one document.

However, enabling "one character searches" can have a significant impact on indexing time and space. I would instead consider transforming your data - specifically, I would remove productid from the document if its value is an empty array. That is assuming that the meaning of an empty array is the same as if the field weren't present. You can then query like this:

cts.search(cts.jsonPropertyScopeQuery("productid"), cts.falseQuery());

Upvotes: 0

Related Questions