3

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
    } 
Mohammed Khan
  • 405
  • 3
  • 13
  • 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 Jun 13 '18 at 18:41
  • 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 Jun 13 '18 at 19:00

1 Answers1

6

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())
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
Rob S.
  • 3,599
  • 6
  • 30
  • 39