Search code examples
node.jsmarklogic

Why is searching arrays using cts different than searching arrays using node api?


I have a question similar to this question: Marklogic (Nodejs API) - Search documents that match 2 (or more) conditions in object array attribute

I have the following document:

{
  "address": [
    { "type": "mailing",
      "street": "1001 Main Street",
      "city": "Springfield",
      "state": "MO"
    },
    { "type": "location",
      "street": "989 First Street",
      "city": "Johnstone",
      "state": "WY"
    }
  ]
}

When I run the following code within query console, it correctly does not return the document:

'use strict';

const queryText = 
  cts.jsonPropertyScopeQuery("address", cts.andQuery([
    cts.jsonPropertyWordQuery("city", "Johnstone"),
    cts.jsonPropertyWordQuery("state", "MO")
  ]));

cts.search(queryText);

When I run this code in Node.js, it does return the document because it appears to combine all of the array nodes when evaluating.

const queryText =
  qb.scope(qb.property("address"), qb.and(
    qb.word("city","Johnstone"),
    qb.word("state","MO")
  ));

const query = qb.where(queryText);

Is there a way I can get cts functionality using the Node API? I would prefer to use the Node API versus using invoke on a server side javascript query.


Solution

  • By default, the SJS searches run filtered, which will remove any false positive results. You can toggle that behavior by adding explicit options to the SJS search:

    cts.search(queryText, "unfiltered");
    

    By default, the Node.js queries are run unfiltered, which means that you may encounter false positive results.

    In order to get your Node.js search running filtered, add the filtered search option to your query:

    const query = qb.where(queryText)
      .withOptions({search:['filtered']});