使用search:search搜索限于元素的查询

时间:2019-05-31 14:10:37

标签: search marklogic marklogic-9

我想要做的事情与下面的链接类似,但我们希望使用search:search

基本上我们有2个xml,如下所示:

XML-1

<rootElement>
   <id>7635940284725382300</id>
   <parentElement>
      <childElement1>ce1-A</childElement1>
      <childElement2>ce2-1</childElement2>
   </parentElement>
   <parentElement>
      <childElement1>ce1-B</childElement1>
      <childElement2>ce2-2</childElement2>
      </parentElement>
   <parentElement>
      <childElement1>ce1-C</childElement1>
      <childElement2>ce2-3</childElement2>
   </parentElement>
</rootElement>

XML-2

<rootElement>
   <id>7635940284725382398</id>
   <parentElement>
      <childElement1>ce1-A</childElement1>
      <childElement2>ce2-2</childElement2>
   </parentElement>
   <parentElement>
      <childElement1>ce1-B</childElement1>
      <childElement2>ce2-3</childElement2>
      </parentElement>
   <parentElement>
      <childElement1>ce1-C</childElement1>
      <childElement2>ce2-4</childElement2>
   </parentElement>
</rootElement>

所以现在我要做的是在同一childElement1中匹配childElement2parentElement

例如:

如果我使用值childElement1 = ce1-BchildElement2 = ce2-3搜索,它应该只给我XML-2,而不返回XML-1

我们已经使用cts:search查询来实现这一点,如链接中所述:

cts:search (/, 
     cts:element-query (
         xs:QName ('parentElement'),
         cts:and-query ((
             cts:element-value-query(xs:QName('childElement1'), 'ce1-B', 'exact'),
             cts:element-value-query(xs:QName('childElement2'), 'ce2-3', 'exact')
         ))
     )
)

问题:

我们在不同的search:search上使用search:constraint吗?我已经读过关于container的用法,但是没有很好的例子说明如何使用它。

这是我们的示例search:search,因此需要对其进行调整,使其具有与上面显示的cts:search查询所显示的功能相同的功能:

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

search:search("childElement1:ce1-B AND childElement2:ce2-3 sort:childElement2ASC"
    , <options xmlns="http://marklogic.com/appservices/search">
        <term>
            <term-option xmlns="http://marklogic.com/appservices/search">unstemmed</term-option>
            <empty apply="all-results" />
            <term-option>case-insensitive</term-option>
            <term-option>wildcarded</term-option>
        </term>

        <transform-results apply="transformed-result" ns="http://isearchgui/search" at="/customResultSet.xqy" />

        <search:constraint name="childElement1">
            <search:range type="xs:string" facet="false" collation="http://marklogic.com/collation/en">
                <search:path-index ns="http://my.search.com/something">//childElement1</search:path-index>
            </search:range>
        </search:constraint>

        <search:constraint name="childElement2">
            <search:range type="xs:string" facet="false" collation="http://marklogic.com/collation/en">
                <search:path-index ns="http://my.search.com/something">//childElement2</search:path-index>
            </search:range>
        </search:constraint>

        <search:operator name="sort">
            <search:state name="childElement1ASC">
                <search:sort-order direction="ascending" type="xs:string">
                <search:path-index ns="http://my.search.com/something">//childElement1</search:path-index>
                </search:sort-order>
            </search:state>
            <search:state name="childElement1DES">
                <search:sort-order direction="descending" type="xs:string">
                <search:path-index ns="http://my.search.com/something">//childElement1</search:path-index>
                </search:sort-order>
            </search:state>
            <search:state name="childElement2ASC">
                <search:sort-order direction="ascending" type="xs:string">
                <search:path-index ns="http://my.search.com/something">//childElement2</search:path-index>
                </search:sort-order>
            </search:state>
            <search:state name="childElement2DES">
                <search:sort-order direction="descending" type="xs:string">
                <search:path-index ns="http://my.search.com/something">//childElement2</search:path-index>
                </search:sort-order>
            </search:state>
        </search:operator>
    </options>
    , 0
    , 15);

希望这有助于回答。

1 个答案:

答案 0 :(得分:1)

您处在正确的轨道上。使用string[] outputArr = outputRaw .Split(new string[] { Environment.NewLine }, StringSplitOptions.None) .Select((stringval, stringindex) => "'" + stringval + " '") .ToArray(); this.Output = String.Join($" + {Environment.NewLine}", outputArr) + ";"; 选项(与其他任何约束选项一样),然后将嵌套查询包装在括号中。例如:

container

=>

search:parse('contain:(test one two)',
  <options xmlns="http://marklogic.com/appservices/search">
    <constraint name="contain">
      <container>
        <element ns="" name="parentElement"/>
      </container>
    </constraint>    
  </options>)