Search code examples
marklogicmarklogic-9

MarkLogic - Query Options - sort on multiple properties


I created a facet using Query Options, as shown below.

"constraint": [
  { 
    "name": "Full Name",
    "range": 
      {
        "type": "xs:string",
        "element": {"name": "Full Name" }
      }
  }
]

I see that the "Full Name" properties are returned in sorted order under "facet" property and also, we can control the sort order (ascending vs descending).

"facets": {
  "Full Name": {
    "type": "xs:string",
    "facetValues": [
      {
        "name": "John H",
        "count": 1,
        "value": "John H"
      },
      {
        "name": "Mary",
        "count": 1,
        "value": "Mary"
      }
    ]
  }
}

But, my requirement is to sort the names based on role first and then names. For example, John is HR and Mary is an accountant, so Mary should appear before John as her role (accountant) precedes John's role (HR) in the sort order. Is there a way I can sort the indexed constraint by additional properties?

One way I heard, is to create a composite property (role+Name) and create a range index on the composite property. Is it suggested approach?


Solution

  • Facets are built from a range index. A range index is the equivalent of a single column or an array of atomic values. A facet can't sort the values in the range index by other values in the source documents.

    As you suggest, one alternative is to concatenate all of the values needed for sorting.

    In MarkLogic 9, another alternative would be to use TDE to create a multi-column index and use the Optic API to sort on one column but return the values of or group on a different column.

    TDE and Optic don't have any integration with the Search API, however, so you would need to collect a page of search results separately from the facet analysis for the entire set of matched documents.

    Hoping that helps,