Using XPath is a natural initial thought for many familiar with XML technologies and starting with MarkLogic. It was what I first started to do when I was just starting out.
Some XPath expressions can be optimized by the database and perform fast and efficiently, but there are also others that cannot and may not perform well.
Using cts:search
and the built-in query constructs allows for optimized expressions that will leverage indexes, and allows you to further tune by analyzing xdmp:plan
, xdmp:query-meters
, and xdmp:query-trace
.
An equivalent cts:search
expression for the XPath, specifying the path to /employees/employee
in the first $path
parameter and combining cts:element-value-query
with cts:and-query
in the second $query
parameter would be:
cts:search(/employees/employee,
cts:and-query((
cts:element-value-query(xs:QName("zipCode"), "12345"),
cts:element-value-query(xs:QName("state"), "California") )))/employeeId
You could also use a more generic $path
to search against all documents and use an xdmp:element-query()
to surround the cts:element-value-query
criteria to restrict the search to descendants of the employee
element and then XPath into the resulting document(s):
cts:search(doc(),
cts:element-query(xs:QName("employee"),
cts:and-query((
cts:element-value-query(xs:QName("zipCode"), "12345"),
cts:element-value-query(xs:QName("state"), "California") ))
)
)/employees/employee/employeeId
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…