Marklogic(Nodejs API) - 在XPath查询上添加facet

时间:2016-08-01 13:34:22

标签: marklogic marklogic-8

我使用nodejs库在我的API上修复了一个查询,但是我在故事中失去了方面。

最初,查询(使用facet)是使用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));
});

此查询在某些情况下返回错误数据,因此我使用XPath查询更改它。

 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));
});

查询效果很好,但是,现在,我需要添加facet以保持兼容性。 我搜索了如何使用nodejs库(文档,示例,tuto,...)在XPath查询中添加facet,但我还没有发现任何问题。

你知道我该怎么做吗?

THX

1 个答案:

答案 0 :(得分:1)

如果您使用构建器对象的内部属性而不是记录的函数,那么您将承担风险,因为内部属性可能随时发生变化,从而破坏您的代码。

例如,如果要指定查询,可以调用函数而不是分配属性:

const query = qb.where(queryElements);

如果要创建构面,则应使用facet()和calculate()函数 - 请参阅:

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

Facet完全由索引构建 - 这是实现大规模性能良好的方面的唯一方法 - 因此只能使用查询而不是XPath进行过滤。

相关问题