gpt4 book ai didi

javascript - 细化 MarkLogic 查询

转载 作者:行者123 更新时间:2023-11-28 17:38:21 26 4
gpt4 key购买 nike

在 MarkLogic 中如何找到元素所在的文档 URI

<sample-ref type="dated">

匹配的值为1742

这里是示例 XML 文档:

<samples>
<sample>
<sample-ref type="dated">1742</sample-ref>
<sample-ref type="undated">1742</sample-ref>
<sample-xref type="sub">
<sample-ref type="dated">TT 1742</sample-ref>
</sample-xref>
<sample-xref type="din">
<sample-ref type="dated">
</sample-ref>
</sample-xref>
<sample-xref type="sup">
<sample-ref type="dated">XX 1742</sample-ref>
</sample-xref>
</sample>
</samples>

我只想查询这个值匹配1742的元素

<sample-ref type="dated">1742</sample-ref>

我尝试过这个查询,但它返回多个 uri:

cts.uris("", null, cts.elementValueQuery(xs.QName("sample-ref"), "1742", "exact"))

如何优化此查询

感谢您的帮助

最佳答案

您正在尝试查询包含 sample-ref 元素(文本内容为“1742”)和值为“dated”的 type 属性的文档。也许您已经发现以下方法不起作用:

cts.elementQuery(xs.QName('sample-ref'), cts.andQuery([
cts.elementAttributeValueQuery(xs.QName('sample-ref'), xs.QName('type'), 'dated'),
cts.elementValueQuery(xs.QName('sample-ref'), '1742')
]))

因为事实证明,除了元素属性查询之外,元素查询中的元素查询仅匹配后代,而不是后代或自身。

David Cassel 在他的博客文章中记录了此问题的一种解决方案:http://blog.davidcassel.net/2012/08/a-trick-with-ctsnear-query/

通过使用距离为 0 的 cts.nearQueries,它会强制 ML 查找子查询与相同元素匹配的位置。

cts.uris("", null, cts.nearQuery([
cts.elementAttributeValueQuery(xs.QName('sample-ref'), xs.QName('type'), 'dated'),
cts.elementValueQuery(xs.QName('sample-ref'), '1742')
], 0))

博客文章提到,要在索引上运行此操作,您需要在数据库上打开两个索引:元素值位置属性值位置。除了性能提升之外,如果您在 cts.uris 调用中使用近查询,则这不是可选的 - cts.uris 不会“过滤”传递的查询的结果,因此如果没有这些索引,您仍然会得到误报。

关于javascript - 细化 MarkLogic 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48577639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com