Warm tip: This article is reproduced from serverfault.com, please click

xquery-使用两个索引的MarkLogic光学查询不返回任何结果

(xquery - MarkLogic optic query using two indexes returns no results)

发布于 2020-12-15 15:30:09

我想使用MarkLogic光学API来联接两个范围索引,但是不知何故它们无法联接。我写错的查询还是无法比较使用的索引?

我定义了两个索引:

  • 元素属性范围索引x / @ refid
  • 范围字段索引“ id”

两者都是字符串类型,并且定义了相同的排序规则。这两个索引都有我可以使用cts:values()函数检索的数据。两者都是巨大的索引,我想使用光学将它们加入,因此我构造了以下查询:

import module namespace op="http://marklogic.com/optic"
at "/MarkLogic/optic.xqy";

let $subfrag := op:fragment-id-col("subfrag") 
let $notfrag := op:fragment-id-col("notfrag") 

let $query :=
cts:and-query((
  cts:collection-query("latest")
))

let $subids := op:from-lexicons(
   map:entry("subid", cts:field-reference("id")), (), $subfrag) => op:where($query)

let $notids := op:from-lexicons(
   map:entry("notid", cts:element-attribute-reference(xs:QName("x"), xs:QName("refid"))),
   (),
       $notfrag)
    
return $subids
   => op:join-cross-product($notids)
   => op:where(op:eq($notfrag, $subfrag))
   => op:result() 

该查询使用join-cross-product,当我删除op:where子句时,我会左右获得所有值。我已验证并且某些条件相等,因此该子句应仅过滤我实际感兴趣的那些行。但是不知何故它不起作用,并且我得到一个空结果。另外,如果我将op:eq中的值之一替换为字符串值,则不会返回结果。

当我在op:eq运算符中使用相同的变量时(例如op:eq($ notfrag,$ notfrag)),我会得到结果,因此该语句可以正常工作。只是不是两个索引之间的比较。

我还使用了带有join-inner和left-outer-join的变体,但是这些变体也没有返回结果。

我是在比较两个无可比拟的索引,还是缺少一些陈述(因为文档/示例有点薄)。

(当然,我可以不使用光学器件来解决问题,但在这种情况下,这将是一个完美的选择)

[更新]

我最终通过更改最终声明使它起作用:

return $subids
=> op:join-cross-product($notids)
=> op:where(op:eq(op:col('subid'), op:col('notid')))
=> op:result() 

因此,你无法以某种方式在条件中使用片段定义。在此之后,我用联接内部结构替换了联接交叉产品,它应该会更有效率。

为了完整起见,我最初使用了MarkLogic文档中的示例(https://docs.marklogic.com/guide/app-dev/OpticAPI#id_87356),特别是最后一个示例,其中他们使用片段列定义来在我的情况下不起作用的join-inner语句中用作参数。

Questioner
Marcel de Kleine
Viewed
0
ehennum 2020-12-19 01:41:17

叉积通常仅对小行集有用。

将这两个引用放在同一个from-lexicons()访问器中会执行隐式连接,这意味着引擎通过构造为每个文档索引的值的本地叉积来形成行。

这样的查询可以表示为:

op:from-lexicons(
   map:entry("subid", cts:field-reference("id"))
   =>map:with("notid", cts:element-attribute-reference(xs:QName("x"),
     xs:QName("refid")))
=>op:where(cts:collection-query("latest"))
=>op:result() 

明确地进行连接可以通过以下方式完成:

let $subids := op:from-lexicons(
   map:entry("subid", cts:field-reference("id")), (), $subfrag)
   => op:where($query)

let $notids := op:from-lexicons(
   map:entry("notid", cts:element-attribute-reference(xs:QName("x"),
       xs:QName("refid"))),
   (),
   $notfrag)

return $subids
   => op:join-inner($notids, op:on($notfrag, $subfrag))
   => op:result() 

希望能有所帮助,