简体   繁体   English

在 Marklogic 中使用 XQuery 无法理解 CTS 查询

[英]Trouble understanding CTS queries using XQuery in Marklogic

I am trying to understand difference between cts:element-query , cts:element-value-query and cts:element-word-query using cts:search() .我正在尝试使用cts:search()了解cts:element-querycts:element-value-querycts:element-word-query之间的区别。

When someone can achieve the same thing using all three why did they created these many?当有人可以使用所有三个实现相同的事情时,他们为什么要创建这么多?

I am sure I am missing something here to understand.我确定我在这里遗漏了一些需要理解的东西。 I have following data:我有以下数据:

<CATALOG>
  <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
  </CD>
  <CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>CBS Records</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1988</YEAR>
  </CD>
  <CD>
    <TITLE>Greatest Hits</TITLE>
    <ARTIST>Dolly Parton</ARTIST>
    <COUNTRY>EU</COUNTRY>
    <COMPANY>RCA</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1982</YEAR>
  </CD>
</CATALOG>

I want to filter the data for country say "EU".我想过滤国家的数据说“欧盟”。 I can achieve the same thing with any query listed below.我可以使用下面列出的任何查询来实现相同的目标。

  •  cts:search(//CD,cts:element-query(xs:QName("COUNTRY"),"EU"))
  •  cts:search(//CD,cts:element-value-query(xs:QName("COUNTRY"),"EU"))
  •  cts:search(//CD,cts:element-word-query(xs:QName("COUNTRY"),"EU"))

So what is the difference?那么区别是什么呢? When to use what?什么时候用什么? Can someone help me understand?有人可以帮我理解吗?

My understand was to use cts:search with cts:element-query .我的理解是使用cts:searchcts:element-query I was researching with the other queries if I can get the same thing using other queries too.如果我也可以使用其他查询获得相同的结果,我正在研究其他查询。 (I have gone thru the documentation I still don't understand). (我已经浏览了我仍然不明白的文档)。 Can someone please give me a simple explanation?有人可以给我一个简单的解释吗?

Those three cts:element-* query functions have some overlapping functionality, and it is possible to get the same results, but there are some key differences that affect what is possible and how efficient the query may be for your system.这三个 cts:element-* 查询函数具有一些重叠的功能,并且可以获得相同的结果,但有一些关键差异会影响查询的可能性以及查询对您的系统的效率。

  • cts:element-query() is a container query. cts:element-query()是一个容器查询。 It matches the element specified in the first parameter.它匹配第一个参数中指定的元素。 The query from second parameter is applied to the matched element and all of its descendants .第二个参数的查询应用于匹配的元素及其所有后代 So the cts:word-query would match the text of COUNTRY or any descendant elements, if there were a more complex structure.因此,如果有更复杂的结构, cts:word-query将匹配 COUNTRY 或任何后代元素的文本。

    Using xdmp:plan() to see the query plan,使用xdmp:plan()查看查询计划,

     xdmp:plan(cts:search(//CD,cts:element-query(xs:QName("COUNTRY"),"EU")))

    you can see the plan has criteria with an unconstrained word-query being applied:您可以看到该计划具有应用不受约束的单词查询的条件:

     <qry:term-query weight="1"> <qry:key>17785254954065741518</qry:key> <qry:annotation>word("EU")</qry:annotation> </qry:term-query>
  • cts:element-value-query() only matches against simple elements (that is, elements that contain only text and have no element children) with text content matching the phrase from the second parameter. cts:element-value-query()仅匹配文本内容与第二个参数中的短语匹配的简单元素(即仅包含文本且没有子元素的元素)。

    The xdmp:plan() for that query:该查询的xdmp:plan()

     xdmp:plan( cts:search(//CD,cts:element-value-query(xs:QName("COUNTRY"),"EU")) )

    reveals that there is a value being applied specifically to the COUNTRY element:表明有一个值专门应用于COUNTRY元素:

     <qry:term-query weight="1"> <qry:key>9358511946618902997</qry:key> <qry:annotation>element(COUNTRY,value("EU"))</qry:annotation> </qry:term-query>
  • cts:element-word-query() is similar to a cts:element-value-query except that it searches only through immediate text node children of the specified element as well as any text node children of child elements defined in the Admin Interface as element-word-query-throughs or phrase-throughs . cts:element-word-query()类似于 cts:element-value-query,除了它只搜索指定元素的直接文本节点子节点以及管理界面中定义的子元素的任何文本节点子节点element-word-query-throughs 或 phrase-throughs It does not search through any other children of the specified element.它不会搜索指定元素的任何其他子元素。

    The xdmp:plan() for that query:该查询的xdmp:plan()

     xdmp:plan( cts:search(//CD,cts:element-word-query(xs:QName("COUNTRY"),"EU")) )

    shows that there is a word query applied specifically to the COUNTRY element:表明有一个词查询专门应用于COUNTRY元素:

     <qry:term-query weight="1"> <qry:key>6958980695756965065</qry:key> <qry:annotation>element(COUNTRY,word("EU"))</qry:annotation> </qry:term-query>

    cts:element-word-query is most helpful if you had mixed content and a known vocabulary of specific elements that you want to be able to "see through" when searching.如果您有混合内容和特定元素的已知词汇表,并且希望在搜索时能够“看穿”,那么cts:element-word-query最有帮助。 One example is MS Word or XHTML markup in which there are elements wrapping text that are used for applying styling and formatting, such as <b> , <i> , and <u> inside of a <p> and you wanted to search for a word in a given paragraph and search through the b , i , and u child elements.一个示例是 MS Word 或 XHTML 标记,其中包含用于应用样式和格式的元素包装文本,例如<p>内的<b><i><u> > 并且您想要搜索给定段落中的单词并搜索biu子元素。

For this specific instance, looking to search for a value in a specific element, you should use:对于这个特定实例,要在特定元素中搜索值,您应该使用:

cts:search(//CD,cts:element-value-query(xs:QName("COUNTRY"),"EU")) 

It is the most specific and efficient means of telling MarkLogic that you want to search for the value "EU" in the COUNTRY element (and not any of it's children or descendants).这是告诉 MarkLogic 您要在COUNTRY元素(而不是它的任何子元素或后代)中搜索值“EU”的最具体和最有效的方法。

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

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