【问题标题】:MarkLogic optic query using two indexes returns no results使用两个索引的 MarkLogic 光学查询不返回任何结果
【发布时间】:2020-12-15 15:30:09
【问题描述】:

我想使用 MarkLogic optic 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() 

此查询使用连接交叉产品,当我删除 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() 

所以不知何故你不能在条件中使用片段定义。在此之后,我将 join-cross-product 替换为 join-inner 结构,这应该更有效。

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

【问题讨论】:

    标签: xquery marklogic


    【解决方案1】:

    交叉产品通常仅对小行集有用。

    将两个引用放在同一个 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() 
    

    希望对您有所帮助,

    【讨论】:

    • 您好,感谢您的回复。不幸的是,这不起作用(我将 =>with 更正为 => map:with)。我得到了它的工作,但最后,请参阅更新原始帖子。不知何故,它仍然需要片段列定义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 2019-04-10
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多