Search code examples
sortingmarklogicmarklogic-8

How to sort on element having multiple occurrences in XML using cts:index-order in MarkLogic?


Let's assume I have bunch of XMLS with structure as defined below.

<root>
    <child1>
        <name>Dixit</name>
        <age>25</age>
    </child1>
    <child2>
        <name>Singla</name>
        <age>45</age>
    </child2>
</root>

CTS search query I have written returns me the entire document

I want to sort the documents by <name> element of <child2>.

I have defined the cts:index-order for <name> element but the documents are getting sorted by element <name> of <child1>.

How can I sort the documents by <name> element of <child2>?


Solution

  • Define a path range index on child2/name, and use that for cts:index-order.

    Here are some details on how to create a path range index using the Admin UI: http://docs.marklogic.com/guide/admin/range_index#id_54948

    cts:index-order takes a cts:reference as a parameter. To use a path range index, you need a cts:path-reference.

    cts:index-order( cts:path-reference("/root/child2/name", ()) )
    

    HTH!