node-client-api
node-client-api copied to clipboard
Issues with range query in marklogic node js api
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
Although, the query works fine when the range parameters are passed alone
Or even when paired with text values
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?
Below are the query options used and an example document in marklogic
@MayuriSivanesan Thanks for the info.! Can you please provide few more details -
- MarkLogic server version used
- Node Client Version used
- Does the user has permissions to read the records in the MarkLogic database?
@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.
- Navigate to the "Element Range Indexes" page for your database.
- Click the "Add" tab.
- Choose "Int" for the Scalar Type.
- Leave Namespace Uri empty
- Enter "Area" in the Localname field
- 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}}}]'
Closing this since it has been a year. Please open another issue if the problem exists/happens again. Thanks.!