你们可以为实现这一目标制造一种习俗限制。 我成功地使用了以下三篇文字:
- setup-db.xqy
- search.xqy
- custom-constraint.xqy
这里是设计书,它建立了两个“Amount”范围指数,并增加了几个样本文件(测试1.xml和测试2.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:search("Amount:3",$options)
search:search("Amount:5",$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)
最后,这里的《习俗-制约法典》,即把制约案文转化为两个数额指数的专栏:
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
)
};
If you want your constraint to function as a facet also, then you d additionally need to implement the start-facet and finish-facet functions (and augment the definition in your options node accordingly. The Search Developer s Guide includes an example of how to do this.