Non Null values in Marklogic - Search JSON documents with attributes in array with not null values in Marklogic

548 Views Asked by At

Structure of JSON documents required to be searched in Marklogic

{  
   "MESSAGEID":"18878285",
   "ORDERNUMBER":["2295796"],
   "CATEGORY":"F3702200000"
}

I wanted to search the URIs of all JSON documents in Marklogic that comprised of not null ORDERNUMBER using Javascript

I am using the following query but it is still showing the URIs for the documents comprising of "ORDERNUMBER":[]

cts.uris("",null,cts.andQuery
    ([
        cts.jsonPropertyValueQuery("ORDERNUMBER", "*", "wildcarded"),
        cts.notQuery(cts.jsonPropertyValueQuery("ORDERNUMBER",""))
    ])
);
1

There are 1 best solutions below

0
grtjn On

You normally have a few options, but the empty array case is particularly hard to distinguish. This is because it is neither an empty string value, nor a null, yet the property is truly present.

cts.jsonPropertyScopeQuery("ORDERNUMBER", cts.trueQuery()) will match any doc that has that property.

cts.jsonPropertyValueQuery("ORDERNUMBER", "") matches ORDERNUMBER: "" and ORDERNUMBER: [""].

cts.jsonPropertyValueQuery("ORDERNUMBER", null) matches ORDERNUMBER: null.

cts.jsonPropertyValueQuery("ORDERNUMBER", "?*", "wildcarded") matches ORDERNUMBER: null (not sure why), ORDERNUMBER: "xx", and ORDERNUMBER: ["xx"], but only if you enable filtering (which requires cts.search), or if you are able to find the appropriate wildcard settings.

To be honest, the simplest solution to my opinion is to just put a range index on ORDERNUMBER, and use a rangeQuery:

cts.rangeQuery(cts.pathReference('ORDERNUMBER'), '>', '')

HTH!