user1660340
user1660340

Reputation: 131

MarkLogic : Wrong total Count

I have the two following XML files:

Page1.xml

<pages xmlns="http://marklogic.com/docs">
  <page>
    <elementNode>data1</elementNode>
    <textNode>text1</textNode>
  </page>
  <page>
    <elementNode>data1</elementNode>
    <textNode>text1</textNode>
  </page>
  <page>
    <elementNode>data3</elementNode>
    <textNode>text3</textNode>
  </page>
  <page>
    <elementNode>data4</elementNode>
    <textNode>text4</textNode>
  </page>
<pages>

Page2.xml

<pages xmlns="http://marklogic.com/docs">
  <page>
    <elementNode>data1</elementNode>
    <textNode>text1</textNode>
  </page>
  <page>
    <elementNode>data2</elementNode>
    <textNode>text3</textNode>
  </page>
  <page>
    <elementNode>data3</elementNode>
    <textNode>text5</textNode>
  </page>
  <page>
    <elementNode>data4</elementNode>
    <textNode>text6</textNode>
  </page>
<pages>

I have created an element range index on "elementNode" and also defined the "page" element as a fragment root. I executed the following xquery with searchText "text1"

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">
    <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"/>
      </range>
    </constraint>
  </options>;
let $searchResult := search:search("text1", $options)
return $searchResult

After executing the query I got the following response:

<?xml version="1.0" encoding="UTF-8"?>
<search:response total="3" start="1" page-length="10" xmlns="" xmlns:search="http://marklogic.com/appservices/search">
  <search:result index="1" uri="/content/C/Documents and Settings/vimleshm/Desktop/abc.xml" path="fn:doc(&quot;/content/C/Documents and Settings/vimleshm/Desktop/abc.xml&quot;)" score="22784" confidence="0.451657" fitness="0.663945">
    <search:snippet>
      <search:match path="fn:doc(&quot;/content/C/Documents and Settings/vimleshm/Desktop/abc.xml&quot;)/*:pages/*:page[1]">
        <search:highlight>text1</search:highlight>
      </search:match>
      <search:match path="fn:doc(&quot;/content/C/Documents and Settings/vimleshm/Desktop/abc.xml&quot;)/*:pages/*:page[2]">
        <search:highlight>text1</search:highlight>
      </search:match>
    </search:snippet>
  </search:result>
  <search:result index="2" uri="/content/C/Documents and Settings/vimleshm/Desktop/abc1.xml" path="fn:doc(&quot;/content/C/Documents and Settings/vimleshm/Desktop/abc1.xml&quot;)" score="22784" confidence="0.451657" fitness="0.663945">
    <search:snippet>
      <search:match path="fn:doc(&quot;/content/C/Documents and Settings/vimleshm/Desktop/abc1.xml&quot;)/*:pages/*:page[1]">
        <search:highlight>text1</search:highlight>
      </search:match>
    </search:snippet>
  </search:result>
  <search:facet name="elementNode">
    <search:facet-value name="data1" count="3">data1</search:facet-value>
  </search:facet>
  <search:qtext>text1</search:qtext>
  <search:metrics>
    <search:query-resolution-time>PT0S</search:query-resolution-time>
    <search:facet-resolution-time>PT0.015S</search:facet-resolution-time>
    <search:snippet-resolution-time>PT0S</search:snippet-resolution-time>
    <search:total-time>PT0.015S</search:total-time>
  </search:metrics>
</search:response>

Now as you can see here the "total" attribute of search:response is "3" here because I have defined "page" as a fragment root [Marklogic Wrong count and Facet result Xquery. Is there any way to get the correct total count, which would be "2" here since "text1" exists only in Page1.xml and Page2.xml? Also, I got facet "data2" with count "3", which should be "2" since it exists in two XML files as in the previous case. Please help me on this.

Upvotes: 0

Views: 518

Answers (1)

wst
wst

Reputation: 11771

You can set the fragment root to <pages> (or to nothing, which will default to fragmenting on the root of each XML document), and then there will be 2 hits: one with 2 matches in Page1.xml and one with 1 match in Page2.xml. However, if you set the fragment root to <page> as you have done, search treats each of those elements as its own document, and it will count <page> instead of <pages>.

Simply, result counts will reflect the number of fragment hits. So the question is really what do you want to count? Documents or <page>s?

Upvotes: 3

Related Questions