Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

How to get the data required using Marklogic XQuery cts:search

So, I faced an interview recently with a well known company on Marklogic. He has asked me a question which I couldn't answer. There is an XML example data as below shown.

He asked me how can you get only employee id whose zipcode is 12345 and state is california using search? like cts:search

The thing which came into my mind is write XPath like below but since he asked me using search I couldn't answer

let $x :=//employee/officeAddress[zipCode="38023"]/../employeeId/string()
return $x

xml dataset:

<employees>
  <employee>
    <employeeId>30004</employeeId>
    <firstName>crazy</firstName>
    <lastName>carol</lastName>
    <designation>Director</designation>
    <homeAddress>
      <address>900 clean ln</address>
      <street>quarky st</street>
      <city>San Jose</city>
      <state>California</state>
      <zipCode>22222</zipCode>
    </homeAddress>
    <officeAddress>
      <address>000 washington ave</address>
      <street>bonaza st</street>
      <city>San Francisco</city>
      <state>California</state>
      <zipCode>12345</zipCode>
    </officeAddress>
  <employee>
</employees>
question from:https://stackoverflow.com/questions/65643286/how-to-get-the-data-required-using-marklogic-xquery-ctssearch

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...