0

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

  • I think your best bet is to address why your first query returned wrong data. Such [queries run unfiltered](http://docs.marklogic.com/guide/performance/unfiltered#chapter) by default, so you could either make it run filtered or (better) configure your indexes differently. Can you say more about the incorrect results? – Dave Cassel Aug 02 '16 at 14:22
  • I have an object array in my document and I need to find documents where I have at least one of the object whose match n conditions. If I use queryBuilder, I find documents where conditions are matched somewhere in all the objects, not necessarily in the same object. Currently, we can't make this kind of query on JSON documents in marklogic with the queryBuilder. I need to use XPath query to apply conditions on each object – Jonathan S. Aug 03 '16 at 13:50

1 Answers1

1

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.

ehennum
  • 7,295
  • 13
  • 9
  • I join the project recently and I discover the existing code. I just try to fix some bugs without refactory to much code. – Jonathan S. Aug 04 '16 at 08:04