node-client-api icon indicating copy to clipboard operation
node-client-api copied to clipboard

Issues with range query in marklogic node js api

Open MayuriSivanesan opened this issue 2 years ago • 4 comments

We are using marklogic node js api to query JSON documents. The query doesn't work fine when range query is paired with boolean values. For example the below query returns zero documents even though there are records which satisfy all the conditions image

Although, the query works fine when the range parameters are passed alone image Or even when paired with text values image

MayuriSivanesan avatar Jun 13 '23 06:06 MayuriSivanesan

Can you attach a copy of the search options you're using and also an example of a document that you expect to match the "IsActive" criteria?

rjrudin avatar Jun 13 '23 10:06 rjrudin

Below are the query options used and an example document in marklogic image

image

MayuriSivanesan avatar Jun 14 '23 04:06 MayuriSivanesan

@MayuriSivanesan Thanks for the info.! Can you please provide few more details -

  1. MarkLogic server version used
  2. Node Client Version used
  3. Does the user has permissions to read the records in the MarkLogic database?

anu3990 avatar Jun 14 '23 16:06 anu3990

@MayuriSivanesan - I have been able to replicate your issue locally. However, when I used the Admin console to create an Element Range Index, then the query worked exactly as expected.

Have you created an index for the "Area" element? If not, try creating an index.

  1. Navigate to the "Element Range Indexes" page for your database.
  2. Click the "Add" tab.
  3. Choose "Int" for the Scalar Type.
  4. Leave Namespace Uri empty
  5. Enter "Area" in the Localname field
  6. Click "Ok" Give your database time to create the new index and try the query again. Note that if your database is large, reindexing can take some time. Do not do this in production without preparing first.

My setup:

  • Ran the sample on both ML 11.0.2 & ML 10.0-10
  • I inserted a document:
{
    "inputType": "application/json",
    "outputType": "application/json",
    "properties": {
        "GroupId": 458,
        "GroupName": "Retail",
        "IsActive": true,
        "Name": "Land",
        "Type": "Building",
        "Area": 2154,
        "IsDeleted": false,
        "ObjectId": 1000006236
    }
}
  • I created a simple Node script to run your query:
const marklogic = require("marklogic");
const connection = require("./settings").connection;

const db = marklogic.createDatabaseClient(connection);
const qb = marklogic.queryBuilder;

const whereClause = {
    "IsDeleted": false,
    "$and": [
        {
            "GroupId": 458
        }, {
            "IsActive": true
        }, {
            "Area": {
                "$ge": 2
            }
        }
    ],
    "$filtered": true
  };

const query = qb.where(qb.byExample(whereClause))
  .withOptions({ categories: "content", metrics: true});

  db.documents.query(query)
    .result()
    .then(response => console.dir(JSON.stringify(response)))
    .catch(error => console.error(error));

What I found was that I was able to replicate your results as long as I don't have any indexes on the "Area" element.

'[{"snippet-format":"snippet","total":0,"start":1,"page-length":10,"results":[],"metrics":{"query-resolution-time":"PT0.001262S","total-time":"PT0.001434S"}}]'

After creating the index:

'[{"snippet-format":"snippet","total":1,"start":1,"page-length":10,"results":[{"index":1,"uri":"/githubExample.json","path":"fn:doc(\\"/githubExample.json\\")","score":14336,"confidence":0.5296452,"fitness":0.7490314,"href":"/v1/documents?uri=%2FgithubExample.json&database=mlxprs-test-content","mimetype":"application/json","format":"json","matches":[{"path":"fn:doc(\\"/githubExample.json\\")/properties/number-node(\\"GroupId\\")","match-text":[{"highlight":"458"}]},{"path":"fn:doc(\\"/githubExample.json\\")/properties/boolean-node(\\"IsActive\\")","match-text":[{"highlight":"true"}]},{"path":"fn:doc(\\"/githubExample.json\\")/properties/number-node(\\"Area\\")","match-text":[{"highlight":"2154"}]},{"path":"fn:doc(\\"/githubExample.json\\")/properties/boolean-node(\\"IsDeleted\\")","match-text":[{"highlight":"false"}]}]}],"metrics":{"query-resolution-time":"PT0.002302S","snippet-resolution-time":"PT0.000847S","total-time":"PT0.319747S"}},{"uri":"/githubExample.json","category":"content","format":"json","contentType":"application/json","contentLength":"223","content":{"inputType":"application/json","outputType":"application/json","properties":{"GroupId":458,"GroupName":"Retail","IsActive":true,"Name":"Land","Type":"Building","Area":2154,"IsDeleted":false,"ObjectId":1000006236}}}]'

BillFarber avatar Jun 14 '23 20:06 BillFarber

Closing this since it has been a year. Please open another issue if the problem exists/happens again. Thanks.!

anu3990 avatar Jun 17 '24 18:06 anu3990