Jonathan S.
Jonathan S.

Reputation: 21

Marklogic (Nodejs API) - Add facet on XPath query

I fix a query on my API using the nodejs library, but I lost the facet in the story.

Initially, the query (with facet) was done with the queryBuilder.

var config = require('./config');
var db marklogic.createDatabaseClient(config.marklogic);
var qb = marklogic.queryBuilder;

var queryElements = [
    qb.directory('/product/'),
    qb.word('brand', 'myBrand')
    /* we add optional conditions here */
];

// facets
var facetQuery = qb.where();
facetQuery.optionsName = 'search_option';
facetQuery.view = 'facets';
facetQuery.search = {
  query: qb.and.apply(qb, queryElements)
};

return db.documents.query(facetQuery).result(function(documents) {
  console.log(JSON.stringify(documents, null, 4));
});

this query return wrong data in some case, so I change it with a XPath query.

 var config = require('./config');
 var db marklogic.createDatabaseClient(config.marklogic);

 var query = 'xdmp:directory("/product")[ attr1 eq "" /* and some optional conditions */]/languages[ code eq "es_ES" and content/category eq "something" /* and some optional conditions */] ! root(.)';

 return db.xqueryEval(query, {})
 .result(function(results) {
   console.log(JSON.stringify(results, null, 2));
});

The query works well, but, now, I need to add facets to keep the compatibility. I searched how to add facet on XPath query with nodejs library (documentation, example, tuto, ...) but I haven't found anyhing.

Do you have any idea how I can do ?

Thx

Upvotes: 0

Views: 87

Answers (1)

ehennum
ehennum

Reputation: 7335

If you use the internal properties of the builder objects instead of the documented functions, you're taking a risk because the internal properties could change at any time, breaking your code.

For instance, if you want to specify a query, you can call the function instead of assigning properties:

const query = qb.where(queryElements);

If you want to create facets, you should use the facet() and calculate() functions -- see:

http://docs.marklogic.com/guide/node-dev/search#id_74030

Facets are built entirely out of the indexes -- that's the only way to implement facets with good performance at scale -- and thus can only be filtered with queries instead of XPaths.

Upvotes: 1

Related Questions