简体   繁体   English

如何使用 Marklogic XQuery cts:search 获取所需的数据

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

So, I faced an interview recently with a well known company on Marklogic.所以,我最近在 Marklogic 上接受了一家知名公司的采访。 He has asked me a question which I couldn't answer.他问了我一个我无法回答的问题。 There is an XML example data as below shown.有一个 XML 示例数据,如下所示。

He asked me how can you get only employee id whose zipcode is 12345 and state is california using search?他问我如何使用搜索仅获取zipcode12345statecalifornia员工 ID like cts:search喜欢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我想到的是像下面这样写 XPath 但因为他用搜索问我我无法回答

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

xml dataset: xml 数据集:

<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>

Using XPath is a natural initial thought for many familiar with XML technologies and starting with MarkLogic.对于熟悉 XML 技术并从 MarkLogic 开始的许多人来说,使用 XPath 是一个自然的初步想法。 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.一些XPath 表达式可以通过数据库进行优化并快速高效地执行,但也有一些不能也可能不会很好地执行。

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 .使用cts:search和内置查询构造可以优化表达式,利用索引,并允许您通过分析xdmp:planxdmp:query-metersxdmp: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: XPath 的等效cts:search表达式,在第一个$path参数中指定/employees/employee的路径,并在第二个$query参数中将cts:element-value-querycts:and-query组合为:

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):您还可以使用更通用的$path来搜索所有文档并使用xdmp:element-query()包围cts:element-value-query条件以将搜索限制为employee元素的后代,然后将 XPath 放入结果文件:

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

xpath I would have tried (not tested): xpath 我会尝试(未测试):

/employees/employee[officeAddress/zipCode = '38023' and officeAddress/state = 'California']/employeeId/string() /employees/employee[officeAddress/zipCode = '38023' and officeAddress/state = 'California']/employeeId/string()

Note that you can use xdmp:plan on xpath too;请注意,您也可以在 xpath 上使用 xdmp:plan; it's interesting to see how it works vs cts:search.看看它与 cts:search 是如何工作的很有趣。

In general you're better off putting as much into cts:search as possible vs xpath (and I like xpath.).一般来说,你最好在 cts:search 与 xpath (我喜欢 xpath)中尽可能多地投入。

The question is a little ambiguous.这个问题有点模棱两可。 Are there many employees in one document?一份文件中有很多员工吗? Or many employees documents?还是很多员工文件? Both?两个都?

Also, don't forget to add the appropriate position indexes, or you won't get much unfiltered help.另外,不要忘记添加适当的 position 索引,否则您将不会获得太多未经过滤的帮助。 Look at the plan before and after adding the indexes.查看添加索引前后的计划。

See also https://help.marklogic.com/Knowledgebase/Article/View/queries-constrained-to-elements另请参阅https://help.marklogic.com/Knowledgebase/Article/View/queries-constrained-to-elements

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM