Loading TOC...

MarkLogic Server 11.0 Product Documentation
DocumentsSearch.orderBy

DocumentsSearch.orderBy(
   sortKeys as cts.order
) as DocumentsSearch

Summary

Specifies the sort order for matched documents.

Parameters
sortKeys One or an array of ordering specifiers that can be a string, a cts.reference object, or a cts.order object. See the Usage Notes for details. Default: [cts.scoreOrder('descending'),cts.documentOrder('ascending')]

Usage Notes

Each sorting specifier can be either a string, a cts.reference object, or a cts.order object. You pass a single specifier, or an array of specifiers. You can mix different types of specifiers.

A simple string implicitly specifies ascending order by index value, where the index is a string-typed cts.jsonPropertyReference. For example, 'name' implies the same ordering as the following: cts.indexOrder(cts.jsonPropertyReference(name, ['type=string'])). To specify more configuration details about the index type, such as a collation or a non-string type, use a cts.reference instead of a simple name. To specify ordering options such as descending order, use a cts.order constructor such as cts.indexOrder.

A cts.reference object creates an index based cts.order object, as if you used cts.indexOrder. To create a cts.reference, use constructors such as cts.jsonPropertyReference, cts.elementReference, cts.pathReference, or cts.geospatialJsonPropertyReference. To specify additional ordering options, such as changing the order from ascending to descending, use a cts.order constructor such as cts.indexOrder.

To use another ordering scheme or to control whether to return results in ascending or descending order, use a cts.order constructor such as cts.documentOrder, cts.confidenceOrder, cts.indexOrder, or cts.unordered. You must use this form to specify ordering options, such as changing the order from ascending to descending.

See Also

Example


// Example: Using a simple JSON property name as input
// Find documents where the "author" JSON property contains the word "twain",
// and order the results by the value of the "title" JSON property, ascending.
const jsearch = require('/MarkLogic/jsearch.sjs');
jsearch.documents()
  .where(jsearch.byExample({'author': { '$word': 'twain' }}))
  .orderBy('title')
  .result();
   

Example


// Example: Using a JSON property with sort order option.
// Find documents where the "author" JSON property contains the word "twain",
// and order the results by the value of the "title" JSON property, descending.
const jsearch = require('/MarkLogic/jsearch.sjs');
jsearch.documents()
  .where(jsearch.byExample({'author': { '$word': 'twain' }}))
  .orderBy(
      cts.indexOrder(cts.jsonPropertyReference('title'), 'descending'))
  .result();
   

Example


// Example: Using a cts.reference to an XML element as input
// Order results by the value of the XML element "someElem" in the
// namespace "/my/namespace", ascending.
const jsearch = require('/MarkLogic/jsearch.sjs');
jsearch.documents()
  ...
  .orderby(cts.elementReference(fn.QName('/my/namespace','someElem'))
  .result();
   

Example


// Example: Using a cts.order object as input
// Order results by descending order of relevance score
const jsearch = require('/MarkLogic/jsearch.sjs');
jsearch.documents()
  ...
  .orderBy(cts.scoreOrder('descending'))
  .result();
   

Example


// Example: Using multiple sort keys
// Order result first by "price" values, and then by "title" values, ascending.
const jsearch = require('/MarkLogic/jsearch.sjs');
jsearch.documents()
  ...
  .orderBy(['price', 'title'])
  .result();
   

Example


// Example: Mixing different kinds of ordering specifiers
const jsearch = require('/MarkLogic/jsearch.sjs');
jsearch.documents()
  ...
  .orderBy([
      'jsonPropName', 
      cts.elementReference(fn.QName('/my/namespace','someElem')), 
      cts.indexOrder(cts.fieldReference('someField'), 'descending'),
      cts.qualityOrder()
   ])
  .result();
   

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.