Page1.xml
<pages xmlns="http://marklogic.com/docs">
<page>
<elementNode>data1</elementNode>
<textNode>text1</textNode>
</page>
<page>
<elementNode>data2</elementNode>
<textNode>text2</textNode>
</page>
<page>
<elementNode>data3</elementNode>
<textNode>text3</textNode>
</page>
<page>
<elementNode>data4</elementNode>
<textNode>text4</textNode>
</page>
</pages>
我在elementNode上创建了一个元素范围索引,然后执行了以下XQuery:
xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";
declare namespace ts= "http://marklogic.com/docs";
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">
<searchable-expression xmlns:ex="http://marklogic.com/docs">//ex:page</searchable-expression>
<grammar>
<starter strength="30" apply="grouping" delimiter=")">(</starter>
<starter strength="40" apply="prefix" element="cts:not-query">NOT</starter>
<joiner strength="10" apply="infix" element="cts:or-query" tokenize="word">OR</joiner>
<joiner strength="20" apply="infix" element="cts:and-query" tokenize="word">AND</joiner>
<joiner strength="50" apply="constraint">:</joiner>
</grammar>
<constraint name="elementNode">
<range collation="http://marklogic.com/collation/" type="xs:string">
<facet-option>limit=1000</facet-option>
<element ns="http://marklogic.com/docs" name="elementNode"/>
<searchable-expression xmlns:ex="http://marklogic.com/clover/docs-xml">//ex:elementNode</searchable-expression>
</range>
</constraint>
</options>;
let $searchResult := search:search("elementNode:(*data*)", $options)
return $searchResult
在上面的例子中我没有得到任何结果,因为我在这里使用了外卡星号[*]。有没有办法启用带有元素范围约束的通配符搜索?提前谢谢。
答案 0 :(得分:2)
范围约束使用cts:element-range-query()
,它允许值比较但不允许使用通配符。但是,cts:element-value-match()
旨在使用通配符表达式,因此一种解决方案是使用该API调用构建自定义约束:
declare function parse(
$constraint-qtext as xs:string,
$right as schema-element(cts:query))
as schema-element(cts:query)
{
let $vals := cts:element-value-match(
xs:QName("elementNode"),
string($right//cts:text))
return document {
cts:element-range-query(xs:QName("elementNode"),"=",$vals) }/*
};
然后在您的选项节点中声明:
<options xmlns="http://marklogic.com/appservices/search">
<constraint name="match-elementName">
<custom facet="false">
<parse apply="parse" ns="" at="/custom.xqy"/>
</custom>
</constraint>
</options>
但是,如果您不需要范围索引,则在elementNode
上创建字段然后使用字段约束可能更简单:
<constraint name="elementNode">
<term-option>wildcarded</term-option>
<word>
<field name="field-elementNode"/>
</word>
</constraint>