Search code examples
xquerymarklogicfaceted-searchmarklogic-8

Sorting on facet count in search:search


I want to sort results on the basis of facet count. How can I go that in search:search?

As of now, I am using this query -

let $options := 
<options xmlns="http://marklogic.com/appservices/search">
  <return-metrics>false</return-metrics>
  <return-facets>true</return-facets>
  <return-results>false</return-results>
  <additional-query>{cts:query($cts-query)}</additional-query>
  <constraint name="decade">
    <range type="xs:dateTime" facet="true">          
      <bucket lt="2002-01-01T00:00:00Z" ge="2001-01-01T00:00:00Z" name="2001">2001</bucket>
      <bucket lt="2003-01-01T00:00:00Z" ge="2002-01-01T00:00:00Z" name="2002">2002</bucket>
      <bucket lt="2004-01-01T00:00:00Z" ge="2003-01-01T00:00:00Z" name="2003">2003</bucket>
      <bucket ge="2004-01-01T00:00:00Z" name="2004">2004</bucket>
      <facet-option>limit=10</facet-option>
      <element ns="http://iddn.icis.com/ns/core" name="released-on"/>
    </range>
  </constraint>
  <operator>
    <state>
      <sort-order direction="descending" type="xs:integer">
        <score/>
      </sort-order>
    </state>
  </operator>
</options>

let $date-seq := search:search("*", $options)
return $date-seq

I want to change the options so that I can sort the facet results by facet count.


Solution

  • Haven't tried this with buckets, but normally I think you'd add facet-options "frequency-order" and "descending":

    <constraint name="decade">
      <range type="xs:dateTime" facet="true">          
        <bucket lt="2002-01-01T00:00:00Z" ge="2001-01-01T00:00:00Z" name="2001">2001</bucket>
        <bucket lt="2003-01-01T00:00:00Z" ge="2002-01-01T00:00:00Z" name="2002">2002</bucket>
        <bucket lt="2004-01-01T00:00:00Z" ge="2003-01-01T00:00:00Z" name="2003">2003</bucket>
        <bucket ge="2004-01-01T00:00:00Z" name="2004">2004</bucket>
        <facet-option>limit=10</facet-option>
        <facet-option>frequency-order</facet-option>
        <facet-option>descending</facet-option>
        <element ns="http://iddn.icis.com/ns/core" name="released-on"/>
      </range>
    </constraint>