English 中文(简体)
多种制约因素,名称相同,名称不同。
原标题:Multiple constraints with same constraint name, different namespaces
  • 时间:2011-11-10 13:44:55
  •  标签:
  • marklogic

According to the Search Developers Guide: Each constraint is named, and the name must be unique across all operators and constraints in your options node.

我们正在使用一个能够产生类似产出的内容浓缩包:

`<TM360:Measurements Measurements="Distance">
    <Measurements:Distance Amount="3" Unit="inches"/>
</TM360:Measurements>
<TM360:Measurements Measurements="Volume">
    <Measurements:Volume Amount="5.0" Unit="liters"/>
</TM360:Measurements>`

Looking at "Amount": The attribute localName is not unique, but the element that contains it is unique.

是否有办法绕过限制词独一无二之处,以建立限制的搜索,即“数量:5.0”,包括上面两个条目上的指数?

处理这种情况的最佳方式是什么?

最佳回答

你们可以为实现这一目标制造一种习俗限制。 我成功地使用了以下三篇文字:

  • 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.

问题回答

为了取得最佳成果,你必须重新证明或丰富《禁止杀伤人员地雷公约》。 搜索标本旨在最佳利用基于QNames的标志性记录仪指数特征。 你今天拥有的一个要素是QName:TM360和几个属性QNames,其中没有一个是强烈的选择性的。

你们可以使用SLT,或进行重新改革,以改革XML。 我建议你把这一目标作为目标:

<dist:inches xmlns:dist="ns://fubar.distance">3</dist:inches>
<vol:liters xmlns:vol="ns://fubar.volume">5.0</vol:liters>

As a side effect, this lets you write more concise XPath queries, and allows much more specific schema types for your nodes.

这一使用案例可能具有争议性,但你可能还想考虑将所有这些测量结果纳入SI单位的标准分类,例如,以便比较容易。

它相对简单,具有帐篷功能。 你可以做这样的事情:

declare namespace Measurements = "http://example.com/Measurements";

cts:search(doc(), cts:element-attribute-value-query(
  (xs:QName("Measures:Distance"), xs:QName("Measures:Volume")),
  xs:QName("Amount"),
  $myamount
))

简言之,你还可以表示,作为Marklogic 8.0-5的一种单一搜索制约因素,使用search:或(nowadays)在REST api:

xquery version "1.0-ml";

declare namespace TM360        = "http://example.com/TM360";
declare namespace Measurements = "http://example.com/Measurements";

xdmp:document-insert(
  "/amount3.xml",
  <TM360:Measurements Measurements="Distance">
    <Measurements:Distance Amount="3" Unit="inches"/>
  </TM360:Measurements>
),
xdmp:document-insert(
  "/amount5.xml",
  <TM360:Measurements Measurements="Volume">
    <Measurements:Volume Amount="5.0" Unit="liters"/>
  </TM360:Measurements>
)

;

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">
      <value>
        <element ns="http://example.com/Measurements" name="Distance"/>
        <element ns="http://example.com/Measurements" name="Volume"/>
        <attribute ns="" name="Amount"/>
      </value>
    </constraint>
  </options>;

(: matches /amount3.xml :)
search:search("Amount:3",$options),

(: matches /amount5.xml :)
search:search("Amount:5.0",$options)

如果你具备所有元素组合的幅度指数,你也可以使用埃文所建议的一系列制约因素。

HTH!





相关问题
Using JSON with XQuery page

So I am using a jQuery plugin (jsonp) to make a cross-domain call to an api and getting JSON data back. I need to somehow get this data into my XQuery page. I m using Marklogic server to store all ...

MarkLogic XQuery xdmp:save/xdmp:quote indent-untyped option

Has anyone used this option before? I do not believe that this feature is working how it should be as I am failing to compile my code once I have inserted this as a option. The problem I am trying to ...

tail() function in XQuery

Is there a way in XQuery to do something like a tail() function? What I m trying to accomplish is to get the contents of a file (using "xdmp:filesystem-file($path)") and then display only the last ...

How to get node without children in xQuery?

So I have two nodes of elements that I m essentially trying to join. I want the top level node to stay the same but the child nodes to be replaced by those cross referenced. Given: <stuff> &...

How to unencode escaped XML with xQuery

I have a variable in xQuery of type xs:string with the value of an encoded HTML snippet (the content of a twitter tweet). It looks like this: Headlines-Today &#8226; AP sources: &lt;b&...