3
votes

Trying to get count of cars based on different filters and within one collection. Even though we see multiple cars that meet the requirement, the query is returning count 0.

fn:count(cts:search(fn:collection("com.cars"), cts:and-query((
    cts:element-word-query(xs:QName("exteriorColor"),  "red", "wildcarded" ),
    cts:element-word-query(xs:QName("interiorColor"),  "gray", "wildcarded" )
    cts:element-word-query(xs:QName("powerSteering"),  json:null(), "wildcarded" )
    ))))

Test Data:

{
      "id":1
      "carName":"Toyoto",
      "exteriorColor": "red",
      "interiorColor": "gray",
      "powerStreering": null
    }
    {
      "id":2
      "carName":"Toyoto",
      "exteriorColor": "blue",
      "interiorColor": "gray",
      "powerStreering": null
    }
    {
      "id":3
      "carName":"Toyoto",
      "exteriorColor": "red",
      "interiorColor": "gray",
      "powerStreering": "yes"
    }
    {
      "id":4
      "carName":"Toyoto",
      "exteriorColor": "white",
      "interiorColor": "gray",
      "powerStreering": null
    }
    {
      "id":5
      "carName":"Toyoto",
      "exteriorColor": "red",
      "interiorColor": "gray",
      "powerStreering": null
    } 
1
What do the documents look like? Are you sure they are in the com.cars collection? json:null() evaluates to the empty sequence when passed this way - is that what you intended? - wst
Documents are JSON type (edited question with the data). They are all in com.cars collection. I'm trying to get count of cars that have exteriorColor red and interiorColor gray and powerStreering is null. - Mohammed Khan

1 Answers

6
votes

You're using cts:element-word-query when you need to be using cts:json-property-word-query instead: https://docs.marklogic.com/cts:json-property-word-query

To test for null, I believe you should be using:

cts:json-property-value-query("propertyName", json:null())