0
votes

I use marklogic 9 with Node.js Client Api. when I try to use queryBuilder to search my doc, I find some problem.

this is my doc data

company: {
  uuid : uuid,
  name : comapnyName,
  parentCompany: {
    uuid: uuid,
    name: parentCompanyName,
  }
}

I want to find all the company below the parent company but not include parent company. I use

db.documents.query(
  this.qb.where(
    this.qb.directory('/company/'),
    this.qb.collection('company'),
    this.qb.word('name',parentCompanyName),
  )
).result();

this query find all the company include parent company. I think it find company.name and company.parentCompany.name.

How can I use BuilderQuery to find doc with company.parentCompany.name?

2

2 Answers

0
votes

You could add a path range index on name and query with pathRangeQuery to get your documents. Your path range index would look similar to this:

  • scalar type: string
  • path expression: company/parentCompany/name

You can add a path range index in your admin interface under databases/your-db/Path Range Indexes.

After adding the index, this xquery should get your documents:

cts:search(fn:doc(), 
  cts:and-query((
    cts:path-range-query("company/parentCompany/name", "=", "Company name")
  ))
)

Sorry for not having a javscript code example, but i did not yet use the nodejs api.

0
votes

One approach is to specify a scope for the query:

db.documents.query(
  qb.where(
    qb.directory('/company/'),
    qb.collection('company'),
    qb.scope('parentCompany', qb.word('name',parentCompanyName))
  )
).result();

This query matches the name property anywhere under the parentCompany property. Such queries are equivalent to the cts.jsonPropertyScopeQuery() function in server-side JavaScript. For more information about the scope query builder function, see:

http://docs.marklogic.com/jsdoc/queryBuilder.html#scope

The path range index alternative specifies an explicit path instead of a scope relationship. If the scope relationship is not enough to eliminate false positives, that's the right way to go. As Wagner notes, the prerequisite is an additional index. The query would resemble the following:

db.documents.query(
  qb.where(
    qb.directory('/company/'),
    qb.collection('company'),
    qb.word(qb.pathIndex('company/parentCompany/name'),parentCompanyName)
  )
).result();

For more information about the pathIndex query builder function, see:

http://docs.marklogic.com/jsdoc/queryBuilder.html#pathIndex

Hoping that helps,