【问题标题】:MarkLogic - Search via Max/Min FilterMarkLogic - 通过最大/最小过滤器搜索
【发布时间】:2016-09-21 13:32:49
【问题描述】:

我想在 MarkLogic 中搜索文档。

我的文档如下所示:

<product xmlns="myns/products">
  <id>3114</id>
  <materialNo xml:lang="en">1.1160</materialNo>
  <steelName xml:lang="en">SWRCH24K</steelName>
  <name xml:lang="en">wire, wire rod for cold heading</name>
  <chemicalProperties>
    <chemicalProperty>
      <element>c</element>
      <min>0.1900</min>
      <max>0.2500</max>
    </chemicalProperty>
    <chemicalProperty>
      <element>si</element>
      <min>0.1000</min>
      <max>0.3500</max>
    </chemicalProperty>
    <chemicalProperty>
      <element>mn</element>
      <min>1.3500</min>
      <max>1.6500</max>
    </chemicalProperty>
    <chemicalProperty>
      <element>p</element>
      <max>0.0300</max>
    </chemicalProperty>
  </chemicalProperties>
</product>

所以我想通过化学性质的最大值/最小值进行搜索。为此,我使用此 xquery 搜索(简单示例):

cts:search(/, cts:and-query(
  (cts:collection-query("test"),
   cts:element-value-query(
     fn:QName("myns/products", "name"),
     "wire, wire rod for cold heading"),
   cts:element-query(
     fn:QName("myns/products", "chemicalProperty"),
     cts:and-query(
       (cts:element-value-query(
          fn:QName("myns/products", "element"), "c"),
        cts:or-query(
          (cts:element-range-query(
             fn:QName("myns/products", "max"), "<=", 0.2),
           cts:and-not-query(
             cts:element-range-query(
               fn:QName("myns/products", "min"), "<=", 0.2),
             cts:element-value-query(
               fn:QName("myns/products", "max"), "*")))),
        cts:or-query(
          (cts:element-range-query(
             fn:QName("myns/products", "min"), ">=", 0.1),
           cts:and-not-query(
             cts:element-range-query(
               fn:QName("myns/products", "max"), ">=", 0.1),
             cts:element-value-query(
               fn:QName("myns/products", "min"), "*"))))))))))

问题是上面的查询将返回示例文档。 子查询(和 - 不)在那里检查是否存在最大/最小值。在某些情况下,可能只有最小值或最大值。

但是这个文档超出了范围?!

我的数据库确实在最小值和最大值上有元素范围索引。所有其他设置均为默认设置。

有什么问题?任何建议。

更新

好的,感谢您的建议,但没有。启用值位置并不能解决问题。然而,一种解决方法是删除“and-not-query”并将其替换为“and-query”并向文档添加新属性:

<chemicalProperty hasMin="0" hasMax="1">...

索引和查询这些属性有效并返回正确的结果。

【问题讨论】:

  • 如果您有后续问题,请将其作为单独的 SO 问题发布。如果这个问题是相关的,那么您可以在新问题中链接到它。

标签: xquery marklogic marklogic-8


【解决方案1】:

由于您的索引设置,如果 minmax 查询在同一文档中的 any &lt;chemicalProperty&gt; 中匹配,cts:element-query 可能会返回 true,而不限于单个&lt;chemicalProperty&gt;。但是,我只希望在未过滤的搜索中看到此选项,并且在您对 cts:search 的调用中看不到该选项。

首先尝试启用element value positions,这应该允许数据库使用索引排除来自不同元素的匹配项。

另一种解决方案是使用cts:near-query 按位置限制元素查询中的值。

【讨论】:

  • 职位应该可以解决这个问题。 cts:near-query 也需要这些,否则它将有效地表现得像 cts:and-query。至少,在未经过滤的情况下运行..
  • @grtjn 我认为cts:near-query 需要与cts:element-query 不同的位置索引?但是,是的,我对问题中明显的过滤查询有点困惑。鉴于这种行为,我认为目前最安全的假设是 OP 偶然将其排除在示例之外。
  • 你是对的,cts:near-query 需要不同的位置索引。 docs.marklogic.com/cts:near-query 说:The word positions index will speed the performance of queries that use cts:near-query. The element word positions index will speed the performance of element-queries that use cts:near-query.
  • 如何使用路径索引?这会提高元素值位置的性能吗?
  • @HarryBakken 我认为如果为路径索引启用了range value positions,这可能比启用element value positionselement-value-query 更快,因为范围索引将被内存映射。
【解决方案2】:

问题似乎是您试图在 cts:element-value-query 调用中使用通配符,但没有将它们声明为通配符。由于没有任何内容与文字“*”匹配,cts:and-not-query 与您的意图相反。

你想要这样的东西:

cts:element-value-query( fn:QName("myns/products", "max"), "*", "wildcarded")

cts:element-value-query

或者,您可以启用通配符索引之一,机器学习会自动检测通配符查询。

如果“通配符”和“未通配符”都不存在,则数据库配置和 $text 确定通配符。如果数据库启用了任何通配符索引(“三个字符搜索”、“两个字符搜索”、“一个字符搜索”或“尾随通配符搜索”)并且如果 $text 包含任一通配符 '?'或“*”,它指定“通配符”。否则它指定“未通配符”。

【讨论】:

  • 通常不需要声明通配符选项。如果启用了任何通配符索引,则当查询字符串中有通配符时,wildcardedoption 将是隐式的。
  • 我从“所有其他设置都是默认设置”假设没有启用任何通配符索引。
猜你喜欢
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-08
相关资源
最近更新 更多