You need to enable JavaScript to run this app. 如何在MarkLogic中限制Search API仅在指定集合内执行搜索?
优惠活动
大模型
产品
解决方案
定价
更多

如何在MarkLogic中限制Search API仅在指定集合内执行搜索?

你之前的两种配置本质是定义集合搜索约束,而非直接给搜索请求附加固定的集合过滤条件,约束定义完成后需要主动在搜索词中调用才会生效,且第一种配置缺少约束name属性,本身为无效配置。

正确实现方案

方式1:通过<additional-query>添加固定集合过滤(推荐,无需修改搜索词)

该方式会给所有搜索请求自动叠加集合过滤条件,无需调整搜索参数,实现代码如下:

import module namespace search = "http://marklogic.com/appservices/search"
  at "/MarkLogic/appservices/search/search.xqy";

let $options  :=
<options xmlns="http://marklogic.com/appservices/search">
  <additional-query>
    cts:collection-query('/mycollection')
  </additional-query>
</options>
return
  search:search('Text',$options)

方式2:使用约束并在搜索词中主动调用

如果需要通过约束动态控制集合过滤条件,需要先正确定义约束,再在搜索词中调用对应约束:

import module namespace search = "http://marklogic.com/appservices/search"
  at "/MarkLogic/appservices/search/search.xqy";

let $options  :=
<options xmlns="http://marklogic.com/appservices/search">
  <constraint name="collection">
    <collection/>
  </constraint>
</options>
return
  search:search('collection:"/mycollection" Text',$options)

你之前第二种配置的prefix参数适合多子集合过滤场景,配置prefix="/mycollection"后,搜索时传入collection:xxx会自动匹配/mycollection/xxx格式的集合,该配置同样需要主动在搜索词中调用约束才会生效。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.15 23:15:05