CTS 和 xPath 都需要吗?
CTS and xPath both necessary?
我有以下 CTS 搜索查询:
cts:search(/parent,
cts:and-query((
cts:element-attribute-value-query(xs:QName('parent'), xs:QName('attr'), 'value'),
cts:element-attribute-value-query(xs:QName('child'), xs:QName('attr-1'), 'value-2'),
cts:element-attribute-value-query(xs:QName('child'), xs:QName('attr-2'), 'value-3')
))
)/child[@attr-1 eq 'value-2' and @attr-2 eq "value-3"]
(: Returns /parent/child elements matching criteria :)
我在 parent 上有一些限定词,在 children 上也有一些限定词。不过,我想要的最终结果只是 children。为了做到这一点,正如您从上面看到的那样,我必须:
- 搜索符合 parent 条件 + children 条件
的文档
- 拿到那个文件后,按照上面的标准逻辑过滤掉children
这行得通,但我必须在 cts:query 中使用与在 xPath 中为 children 相同的逻辑,这似乎真的很愚蠢。不必要地重复逻辑。
有没有一种方法可以在 cts:query 中完成这一切,而不必像上面的示例那样使用额外的 xPath 表达式?
这与我想要的类似,但它不适用于评论中指定的问题:
cts:search(/parent/child,
cts:and-query((
cts:element-attribute-value-query(xs:QName('parent'), xs:QName('attr'), 'value'), (: The problem is this line... I can't filter by the parent, as it is above the scope of my first parameter (/parent/rule) :)
cts:element-attribute-value-query(xs:QName('child'), xs:QName('attr-1'), 'value-2'),
cts:element-attribute-value-query(xs:QName('child'), xs:QName('attr-2'), 'value-3')
))
)
您仍然可以查询 parent,即使您的搜索跨 child:
cts:search(/parent/child,
cts:and-query((
cts:element-attribute-value-query(xs:QName('parent'), xs:QName('attr'), 'value'),
cts:element-attribute-value-query(xs:QName('child'), xs:QName('attr-1'), 'value-2'),
cts:element-attribute-value-query(xs:QName('child'), xs:QName('attr-2'), 'value-3')
))
)
这将 运行 作为过滤搜索,但由于您无论如何都是手动进行过滤,因此性能应该大致相当。
更新:
我测试了一下,上面的说法是错误的。我认为这是正确的,但显然 cts:search
过滤将过滤与可搜索表达式不完全匹配的结果。 parent 将超出可搜索表达式的范围。
理想情况下,您可以在 child
元素上拆分文档,但您至少可以删除重叠的查询和 XPath,如下所示:
cts:search(/parent/child,
cts:and-query((
cts:element-attribute-value-query(xs:QName('child'), xs:QName('attr-1'), 'value-2'),
cts:element-attribute-value-query(xs:QName('child'), xs:QName('attr-2'), 'value-3')
))
)[parent::parent/@attr = 'value']
wst给了你答案。然而,这一切都源于需要过滤。在 MarkLogic 中,想法是一个文档应该反映一个 'record'。是否可以重构您的文档以避免在第一个地方进行过滤?
我有以下 CTS 搜索查询:
cts:search(/parent,
cts:and-query((
cts:element-attribute-value-query(xs:QName('parent'), xs:QName('attr'), 'value'),
cts:element-attribute-value-query(xs:QName('child'), xs:QName('attr-1'), 'value-2'),
cts:element-attribute-value-query(xs:QName('child'), xs:QName('attr-2'), 'value-3')
))
)/child[@attr-1 eq 'value-2' and @attr-2 eq "value-3"]
(: Returns /parent/child elements matching criteria :)
我在 parent 上有一些限定词,在 children 上也有一些限定词。不过,我想要的最终结果只是 children。为了做到这一点,正如您从上面看到的那样,我必须:
- 搜索符合 parent 条件 + children 条件 的文档
- 拿到那个文件后,按照上面的标准逻辑过滤掉children
这行得通,但我必须在 cts:query 中使用与在 xPath 中为 children 相同的逻辑,这似乎真的很愚蠢。不必要地重复逻辑。
有没有一种方法可以在 cts:query 中完成这一切,而不必像上面的示例那样使用额外的 xPath 表达式?
这与我想要的类似,但它不适用于评论中指定的问题:
cts:search(/parent/child,
cts:and-query((
cts:element-attribute-value-query(xs:QName('parent'), xs:QName('attr'), 'value'), (: The problem is this line... I can't filter by the parent, as it is above the scope of my first parameter (/parent/rule) :)
cts:element-attribute-value-query(xs:QName('child'), xs:QName('attr-1'), 'value-2'),
cts:element-attribute-value-query(xs:QName('child'), xs:QName('attr-2'), 'value-3')
))
)
您仍然可以查询 parent,即使您的搜索跨 child:
cts:search(/parent/child,
cts:and-query((
cts:element-attribute-value-query(xs:QName('parent'), xs:QName('attr'), 'value'),
cts:element-attribute-value-query(xs:QName('child'), xs:QName('attr-1'), 'value-2'),
cts:element-attribute-value-query(xs:QName('child'), xs:QName('attr-2'), 'value-3')
))
)
这将 运行 作为过滤搜索,但由于您无论如何都是手动进行过滤,因此性能应该大致相当。
更新:
我测试了一下,上面的说法是错误的。我认为这是正确的,但显然 cts:search
过滤将过滤与可搜索表达式不完全匹配的结果。 parent 将超出可搜索表达式的范围。
理想情况下,您可以在 child
元素上拆分文档,但您至少可以删除重叠的查询和 XPath,如下所示:
cts:search(/parent/child,
cts:and-query((
cts:element-attribute-value-query(xs:QName('child'), xs:QName('attr-1'), 'value-2'),
cts:element-attribute-value-query(xs:QName('child'), xs:QName('attr-2'), 'value-3')
))
)[parent::parent/@attr = 'value']
wst给了你答案。然而,这一切都源于需要过滤。在 MarkLogic 中,想法是一个文档应该反映一个 'record'。是否可以重构您的文档以避免在第一个地方进行过滤?