You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MarkLogic中使用带通配符的directory-query结合term-query搜索?

在MarkLogic中实现带通配符的目录查询(配合term-query)

MarkLogic的<directory-query>确实不支持在URI中直接使用通配符,要匹配/example/*/folder/这类模式的目录并结合term-query搜索,可采用以下几种方案:

方案1:使用<collection-query>(性能最优,推荐)

如果目标目录下的文档已归属到特定集合,或可批量为其添加集合标记,直接用集合查询是最高效的方式:

步骤1:批量添加集合(若未添加)

用XQuery给所有符合目录模式的文档添加集合example-folder-collection:

for $doc in fn:collection()
where fn:starts-with(fn:document-uri($doc), "/example/") 
  and fn:contains(fn:document-uri($doc), "/folder/")
return xdmp:document-add-collections(fn:document-uri($doc), "example-folder-collection")

步骤2:修改搜索查询

<search xmlns="http://marklogic.com/appservices/search">
  <query>
    <and-query>
      <collection-query>
        <uri>example-folder-collection</uri>
      </collection-query>
      <term-query>
        <text>some text</text>
      </term-query>
    </and-query>
  </query>
</search>

方案2:使用<value-query>匹配文档URI

直接针对文档URI进行模式匹配,需要确保URI范围索引已开启(数据库→Indexes→URI Range Index添加字符串类型索引):

方式A:结合<wildcard-query>

<search xmlns="http://marklogic.com/appservices/search">
  <query>
    <and-query>
      <value-query>
        <element ns="" name="uri"/>
        <wildcard-query>
          <text>/example/*/folder/*</text>
        </wildcard-query>
      </value-query>
      <term-query>
        <text>some text</text>
      </term-query>
    </and-query>
  </query>
</search>

方式B:结合<range-query>与正则匹配

<search xmlns="http://marklogic.com/appservices/search">
  <query>
    <and-query>
      <range-query>
        <element ns="" name="uri"/>
        <operator>matches</operator>
        <value>/example/.+/folder/.*</value>
      </range-query>
      <term-query>
        <text>some text</text>
      </term-query>
    </and-query>
  </query>
</search>

方案3:XQuery自定义查询逻辑

直接用XQuery编写查询,结合cts:search和URI正则筛选:

cts:search(
  fn:collection(),
  cts:and-query((
    cts:term-query("some text"),
    cts:document-query(
      fn:filter(
        fn:collection(),
        fn:matches(fn:document-uri(.), "^/example/.+/folder/.*$")
      ) ! fn:document-uri(.)
    )
  ))
)

内容的提问来源于stack exchange,提问作者Aspir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 21:17:48