使用 search:search 限制元素的查询进行搜索

Search with queries constrained to elements using search:search

我想做的与下面的类似 link 但我们希望使用 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>

所以现在我想做的是在同一个 parentElement.

中匹配 childElement1childElement2

例如:

如果我搜索值 childElement1 = ce1-BchildElement2 = ce2-3,它应该只给我 XML-2 而不是 return XML-1.

我们使用 cts:search 查询实现了这一点,如 link:

中所述
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);

希望这有助于回答。

你走在正确的轨道上。使用 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>)

=>

  <cts:element-query xmlns:cts="http://marklogic.com/cts">
    <cts:element>parentElement</cts:element>
    <cts:and-query>
      <cts:word-query>
        <cts:text xml:lang="en">test</cts:text>
      </cts:word-query>
      <cts:word-query>
        <cts:text xml:lang="en">one</cts:text>
      </cts:word-query>
      <cts:word-query>
        <cts:text xml:lang="en">two</cts:text>
      </cts:word-query>
    </cts:and-query>
  </cts:element-query>

所以在你的情况下你会得到 main-search 字符串:

search:search('contain:(childElement1:ce1-B AND childElement2:ce2-3) sort:childElement2ASC', <options ....)