您可以创建一个自定义约束来实现这一点。我使用以下三个脚本成功地做到了这一点:
- 设置-db.xqy
- 搜索.xqy
- 自定义约束.xqy
这是设置脚本,它创建了两个“金额”范围索引并添加了几个示例文档(test1.xml 和 test2.xml):
xquery version "1.0-ml";
import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy" ;
declare namespace TM360 = "http://example.com/TM360";
declare namespace Measurements = "http://example.com/Measurements";
declare function local:make-amount-index($parent-name) {
admin:database-range-element-attribute-index(
(: data type :) "decimal",
(: parent name :) "http://example.com/Measurements",$parent-name,
(: attribute name :) "", "Amount",
(: collation :) "",
(: value positions :) false()
)
};
(: Set up the indexes (or you can add these via the Admin UI) :)
let $dbid := xdmp:database(),
$rangespec1 := local:make-amount-index("Distance"),
$rangespec2 := local:make-amount-index("Volume"),
$config := admin:get-configuration(),
$config := admin:database-add-range-element-attribute-index($config, $dbid, $rangespec1),
$config := admin:database-add-range-element-attribute-index($config, $dbid, $rangespec2)
return
admin:save-configuration($config)
,
(: Add some sample docs :)
xdmp:document-insert("/test1.xml",
<TM360:Measurements Measurements="Distance">
<Measurements:Distance Amount="3" Unit="inches"/>
</TM360:Measurements>),
xdmp:document-insert("/test2.xml",
<TM360:Measurements Measurements="Volume">
<Measurements:Volume Amount="5.0" Unit="liters"/>
</TM360:Measurements>)
下面是 search.xqy,它进行了两次搜索:
search:search("Amount:3",$options)
search:search("Amount:5",$options)
特别注意 $options 节点,它定义了自定义约束:
xquery version "1.0-ml";
import module namespace search="http://marklogic.com/appservices/search"
at "/MarkLogic/appservices/search/search.xqy";
declare variable $options :=
<options xmlns="http://marklogic.com/appservices/search">
<constraint name="Amount">
<custom facet="false">
<parse apply="parse-amount"
ns="http://example.com/custom-constraint"
at="/custom-constraint/custom-constraint.xqy">
</parse>
</custom>
</constraint>
</options>;
(: matches test1.xml :)
search:search("Amount:3",$options),
(: matches test2.xml :)
search:search("Amount:5",$options)
最后,这是 custom-constraint.xqy 代码,它将约束文本转换为跨两个 Amount 索引的 cts OR 查询:
xquery version "1.0-ml";
module namespace my = "http://example.com/custom-constraint";
declare namespace Measurements = "http://example.com/Measurements";
declare default function namespace "http://www.w3.org/2005/xpath-functions";
(: Convert the constraint text into an OR query against "Distance" and "Volume" :)
declare function my:parse-amount($constraint-qtext as xs:string,
$right as schema-element(cts:query))
as schema-element(cts:query)
{
let $value := xs:decimal($right//cts:text)
return
<cts:or-query>{
my:make-amount-query("Distance",$value),
my:make-amount-query("Volume" ,$value)
}</cts:or-query>
};
declare function my:make-amount-query($parent-name, $value) {
cts:element-attribute-range-query(
(: parent name :) QName("http://example.com/Measurements", $parent-name),
(: attribute name :) xs:QName("Amount"),
(: operator :) "=",
(: value :) $value
)
};
如果您希望您的约束也作为一个方面,那么您还需要实现 start-facet 和 finish-facet 函数(并相应地增加您的选项节点中的定义。搜索开发人员指南包括一个示例,说明如何做这个。