下面是我正在创建方面的XML结构。 XML包含两个单独的文档,它们分别作为example1.xml和example2.xml加载。
<?xml version="1.0" encoding="UTF-8"?>
<Providers>
<Provider>
<id>1</id>
<Name>Nilabh</Name>
<Products>
<Product>
<Pid>1</Pid>
<ProdName>prd1</ProdName>
<Roles>
<Role>
<RoleCode>R1</RoleCode>
<Specialities>
<Speciality>
<SpecName>S1</SpecName>
</Speciality>
<Speciality>
<SpecName>S2</SpecName>
</Speciality>
</Specialities>
</Role>
<Role>
<RoleCode>R2</RoleCode>
<Specialities>
<Speciality>
<SpecName>S1</SpecName>
</Speciality>
</Specialities>
</Role>
</Roles>
</Product>
<Product>
<Pid>2</Pid>
<ProdName>prd2</ProdName>
<Roles>
<Role>
<RoleCode>R3</RoleCode>
<Specialities>
<Speciality>
<SpecName>S2</SpecName>
</Speciality>
<Speciality>
<SpecName>S3</SpecName>
</Speciality>
</Specialities>
</Role>
<Role>
<RoleCode>R1</RoleCode>
<Specialities>
<Speciality>
<SpecName>S4</SpecName>
</Speciality>
</Specialities>
</Role>
</Roles>
</Product>
</Products>
</Provider>
<Provider>
<id>2</id>
<Name>Nil</Name>
<Products>
<Product>
<Pid>1</Pid>
<ProdName>prd1</ProdName>
<Roles>
<Role>
<RoleCode>R1</RoleCode>
<Specialities>
<Speciality>
<SpecName>S1</SpecName>
</Speciality>
<Speciality>
<SpecName>S3</SpecName>
</Speciality>
</Specialities>
</Role>
<Role>
<RoleCode>R2</RoleCode>
<Specialities>
<Speciality>
<SpecName>S3</SpecName>
</Speciality>
</Specialities>
</Role>
</Roles>
</Product>
<Product>
<Pid>3</Pid>
<ProdName>prd3</ProdName>
<Roles>
<Role>
<RoleCode>R3</RoleCode>
<Specialities>
<Speciality>
<SpecName>S5</SpecName>
</Speciality>
<Speciality>
<SpecName>S3</SpecName>
</Speciality>
</Specialities>
</Role>
<Role>
<RoleCode>R2</RoleCode>
<Specialities>
<Speciality>
<SpecName>S1</SpecName>
</Speciality>
</Specialities>
</Role>
</Roles>
</Product>
</Products>
</Provider>
</Providers>
下面是我用于使用facet查询的XQuery。我在ProdName和SpecName上定义了范围索引。
import module namespace search = "http://marklogic.com/appservices/search" at "/MarkLogic/appservices/search/search.xqy";
let $options := <options xmlns="http://marklogic.com/appservices/search">
<additional-query>{cts:element-query(xs:QName("ProdName"),"prd3")}</additional-query>
<constraint name="ProdName">
<range type="xs:string" collation="http://marklogic.com/collation/codepoint" facet="true">
<element ns="" name="ProdName"/>
<facet-option>frequency-order</facet-option>
</range>
</constraint>
<constraint name="SpecName">
<range type="xs:string" collation="http://marklogic.com/collation/codepoint" facet="true">
<element ns="" name="SpecName"/>
<facet-option>frequency-order</facet-option>
</range>
</constraint>
<return-results>true</return-results>
<return-facets>true</return-facets>
<debug>true</debug>
</options>
return search:search(" ", $options)
上面的查询输出如下所示,显示了facet的所有条目。但是根据我的上述查询 我期待“SpecName”Facet应该只包含[s4(1),s3(1),s5(1)]条目,因为这是在prd3产品中 ,但下面的输出包括结果xml中的所有条目。有人可以帮我按预期实现输出。
<search:response snippet-format="snippet" total="2" start="1" page-length="10" xmlns:search="http://marklogic.com/appservices/search">
<search:result index="1" uri="/providers/2.xml" path="fn:doc("/providers/2.xml")" score="2048" confidence="0.1760856" fitness="0.2357023">
<search:snippet>
<search:match path="fn:doc("/providers/2.xml")/Provider/Products/Product[2]">
<search:highlight>prd3</search:highlight>
</search:match>
</search:snippet>
</search:result>
<search:result index="2" uri="/providers/1.xml" path="fn:doc("/providers/1.xml")" score="2048" confidence="0.1760856" fitness="0.2357023">
<search:snippet>
<search:match path="fn:doc("/providers/1.xml")/Provider/Products/Product[3]">
<search:highlight>prd3</search:highlight>
</search:match>
</search:snippet>
</search:result>
<search:facet name="ProdName" type="xs:string">
<search:facet-value name="prd1" count="2">prd1</search:facet-value>
<search:facet-value name="prd3" count="2">prd3</search:facet-value>
<search:facet-value name="prd2" count="1">prd2</search:facet-value>
<search:facet-value name="prd5" count="1">prd5</search:facet-value>
</search:facet>
<search:facet name="SpecName" type="xs:string">
<search:facet-value name="S1" count="2">S1</search:facet-value>
<search:facet-value name="S3" count="2">S3</search:facet-value>
<search:facet-value name="S2" count="1">S2</search:facet-value>
<search:facet-value name="S4" count="1">S4</search:facet-value>
<search:facet-value name="S5" count="1">S5</search:facet-value>
<search:facet-value name="S6" count="1">S6</search:facet-value>
</search:facet>
<search:qtext>
</search:qtext>
<search:report id="SEARCH-FLWOR">(cts:search(fn:collection(), cts:and-query(cts:element-query(fn:QName("","ProdName"), cts:word-query("prd3", ("lang=en"), 1), ()), ()), ("score-logtfidf","faceted",cts:score-order("descending")), 1))[1 to 10]</search:report>
<search:metrics>
<search:query-resolution-time>PT0.596S</search:query-resolution-time>
<search:facet-resolution-time>PT0.1S</search:facet-resolution-time>
<search:snippet-resolution-time>PT0.047S</search:snippet-resolution-time>
<search:total-time>PT1.275S</search:total-time>
</search:metrics>
</search:response>
我的要求就好像有人搜索ProdName:prd1和RoleCode:R1然后Facet SpecName应该显示输出为S1(2),S2(1),S3(1)。你能指导我如何获得这个输出。
此致 Nilabh
答案 0 :(得分:1)
您试图获得每个产品的方面结果,但将所有产品存储在一个片段中。我认为你最好在产品级别拆分大型XML。
HTH!
答案 1 :(得分:0)
有多个步骤可以满足您的要求:
从ML查询控制台运行以下搜索脚本:
搜索:搜索(&#34;((ProdName:prd1)AND(RoleCode:R1))&#34;,$ options)
将为您提供预期的输出:
<search:response snippet-format="snippet" total="1" start="1" page-length="10" xmlns:search="http://marklogic.com/appservices/search">
<search:result></search:result>
<search:facet name="ProdName" type="xs:string">
<search:facet-value name="prd1" count="2">prd1</search:facet-value>
</search:facet>
<search:facet name="RoleCode" type="xs:string">
<search:facet-value name="R1" count="2">R1</search:facet-value>
<search:facet-value name="R2" count="2">R2</search:facet-value>
</search:facet>
<search:facet name="SpecName" type="xs:string">
<search:facet-value name="S1" count="2">S1</search:facet-value>
<search:facet-value name="S2" count="1">S2</search:facet-value>
<search:facet-value name="S3" count="1">S3</search:facet-value>
</search:facet>
<search:qtext>((ProdName:prd1) AND (RoleCode:R1))</search:qtext>
<search:report id="SEARCH-FLWOR">(cts:search(fn:collection(), cts:and-query((cts:element-range-query(fn:QName("","ProdName"), "=", "prd1", ("collation=http://marklogic.com/collation/"), 1), cts:element-range-query(fn:QName("","RoleCode"), "=", "R1", ("collation=http://marklogic.com/collation/"), 1)), ()), ("score-logtfidf","faceted",cts:score-order("descending")), 1))[1 to 10]</search:report>
<search:metrics>
<search:query-resolution-time>PT0.088S</search:query-resolution-time>
<search:facet-resolution-time>PT0.198S</search:facet-resolution-time>
<search:snippet-resolution-time>PT0.016S</search:snippet-resolution-time>
<search:total-time>PT0.389S</search:total-time>
</search:metrics>
</search:response>