afs:sort - AFS

AFS Integration Guide

Reference Guide


Sets the sorting order with a list of criteria of type facet, facet,order or facet,order,sort_type:

  • order specifies the sort order:
    • ASC or DESC (for ascending and descending sort order respectively). When omitted, the default order is DESC.
  • sort_type specifies the sort type:
    • ALPHA or NATURAL (for lexicographic or natural order respectively). When omitted, the default order is ALPHA.

      Syntax example: afs:sort=versionFacet,ASC,NATURAL

  • Multiple, cascading criteria. It can be used with the and keyword or the ; sign (semi-colon) to separate them.

When afs:sort is omitted, replies are sorted according to full-text search relevance.

The following built-in facets are available:

  • afs:words is the number of the query words found in the document. Example: if the query is composed of three different words, and all those words are present in the matched document, afs:words value will be 3. If one of the query words is not present in the matched document, afs:word value will be 2, etc.
  • afs:pathlen is the minimal distance between query words in the document.
  • afs:weight is the importance of the words in the document between 1 and 100, where 100 means very important and 1 not important.
  • afs:fieldMatch criterion allows replies containing exactly the keywords in an item to be more relevant. afs:fieldMatch is a boolean holding true if the FTS match matches a whole item, and false otherwise. afs:fieldMatch must be sorted in DESC order in order to have best matches first. afs:fieldMatch behavior is undefined in case of multiple FTS.
  • afs:relevance is the shortcut for afs:words,DESC and afs:fieldMatch,DESC and afs:pathlen,ASC and afs:weight,DESC
  • afs:docId is the docId of the document.
  • afs:uri is the URI of the document.
  • afs:lang is the language of the document.
  • afs:size is the size (in kb) of the document.
  • afs:doctype is the type of the document.

Note that some built-in facets have to be activated in the PaF configuration, otherwise they are not available at query time. For more information , see Built-in Facets topic of the AFS Configuration Guide.

In addition, the following pseudo facets are available:

  • random for random sort order. See afs:sort=random topic for more details about this pseudo facet.

They are called pseudo facets because they are not specific to each document.

When two replies are found to be equal once every criterion has been evaluated, the replies are sorted by descending docId (meaning that the newest document appears first).


Optional. It can be used several times, in which case the sorting criteria are evaluated in order in an and fashion.


  • Simple example

    Sort by user-specific price facet containing integer values:

    It is possible to use afs:sort=price,DESC or afs:sort=price,ASC to sort the documents according to the value of the price facet in each document, respectively in descending or ascending order.

    Note that DESC being the default value, the first case can be replaced by afs:sort=price.

  • Natural sort example

    Sort by user-specific version facet containing string values, in natural order:

    afs:sort=version,DESC,NATURAL sorts the documents according to the value of the version facet using natural sort order, in descending order.

    Note that afs:sort=version,DESC is equivalent to afs:sort=version,DESC,ALPHA since the lexicographic sort order is the default value.

  • Combination example

    Sort by built-in relevance facet, then by the user-specific price facet:

    afs:sort=afs:relevance,DESC and price,ASC

    Alternative syntaxes:



Focus on afs:sort=fieldMatch vs. afs:ftsMatch=field.
Example: a bookshop
- Search for "le petit prince" with afs:ftsMatch=field returns only one book: "Le petit prince".
- Search for "le petit prince" with afs:sort=fieldMatch returns many books: "le petit prince" is first, then "le petit prince II", "analyse du petit prince", ...