Page tree
Skip to end of metadata
Go to start of metadata

Overview

The Attivio Intelligence Engine (AIE) provides various options for sorting query results. These options can be specified via any of the supported APIs (Java Client and JSON REST API).  

This document describes these options and how to use them.

Sorting for simple query language or the advanced query language queries is specified on the query request, not in the actual query string.

View incoming links.

 

Sort Criteria

Sorting can be done using any of the following criteria. Each type of sorting can be performed in either ascending or descending order.

Sorting by Field Value

Sorts records by the value of a field.  It is a best practice to apply sorting to schema fields where the sort attribute is set to "true". This setting optimizes the field to support rapid sorting.  If the sort attribute is "false" the field can still be sorted, but the amount of work done will be much greater and the user may notice a slowdown in response time.

<project_dir>/conf/schema.xml
...
  <!-- Sortable field -->
  <field name="age" type="integer" sort="true" indexed="true" stored="true" />
  ...
  <!-- Sortable real time field -->
  <realtimeField name="popularity" type="integer" sort="true" indexed="true" stored="true"/>
...

If a field contains multiple values, the first value is used for sorting. For example, in a field containing the values "11,7,23", the value "11" is used for sorting.

The following example sorts the result set of the query ":" by the "age" field in ascending order.

Java Client API

QueryRequest query = new QueryRequest("*:*");
  query.addSort(new Sort("age",SortOrder.ASCENDING));
  ...

REST API

http://...&sort=age:asc...

Either ":asc" or ":desc" can be specified for ascending or descending order, respectively. Any other value after the colon generates an error response. If neither is specified, the results are sorted in descending order:

http://...&sort=age...

 

Sorting by Relevancy Score

This is the default sorting method. This method sorts records by the relevancy score assigned to them by the query. In most cases, there is no need to specify this order, since it is the default; however, when used as a secondary sort criterion (see multi-level sorting, below) this type of sorting needs to be explicitly specified.

Main article: Machine Learning Relevancy

The following example sorts ascending by "age", and if multiple documents have the same "age" value, sorts them by the relevancy score assigned to them by the query, in descending order:

Java Client API

QueryRequest query = new QueryRequest("*:*");
  query.addSort( new Sort("age", SortOrder.ASCENDING) );
  query.addSort( SortSpecification.RELEVANCY_SORT );
  ...

REST API

http://...&sort=age:asc,$score...

 

Sorting by Field Expression

AIE provides a set of functions that operate on field values, named Field Expressions. Any field expression can be used as a sort criterion.

Any fields used in a field expression should be declared in the schema as either index="true", or sort="true", otherwise, non-ideal sorting performance will result.

Similar to Sorting by Field Value, if a field contains multiple values, the first value is used for sorting.

The following example uses the Freshness function as a secondary sort, which gives more recent documents higher scores:

Java Client API

QueryRequest query = new QueryRequest("*:*");
  query.addSort( new Sort("age", SortOrder.ASCENDING) );
  query.addSort( new Sort( new Freshness("date"), SortOrder.DESCENDING ) );
  ...

REST API

http://...&sort=age:asc,freshness(date):desc...

 

Sorting in Pseudo Random Order

A pseudo random order can be useful under some circumstances when ordering of search results should change from user to user. A request for random sort must include a seed value for the function that generates pseudo random numbers.

When requesting multiple subsets of a query result list (e.g., when paging through the results), all the requests MUST specify the same seed in order to ensure that the results are sorted the same way for each request.

Java Client API

QueryRequest query = new QueryRequest("*:*");
  query.setSeed( 893892398L );
  query.addSort( new Sort(FieldNames.RANDOM_SORT) );
  ...

REST API

http://...&sort=.random:asc&seed=893892398...

 

Multi-Level Sorting

Multiple sort criteria can be specified for a single query. The results are sorted first by the first sort option; records that have the same sort value for the first option are then sorted by the second option, and so on.

Any of the sort criteria defined above can be combined into multi-level sorting.

The following example sorts the query results by ascending "age" order, and documents that have the same value for "age" are then sorted descending by "size".

Java Client API

QueryRequest query = new QueryRequest("*:*");
  query.addSort( new Sort("age", SortOrder.ASCENDING) );
  query.addSort( new Sort("size", SortOrder.DESCENDING) );
  ...

REST API

http://...&sort=age:asc,size:desc...

 

Sorting the Results of Join Queries

When sorting the result of a join query, only the top-level records in the join are sorted. Thus, fields used for sorting must exist in the top-level records.

The order of child records is specified in the join query itself. See Advanced Query Language  and com.attivio.sdk.search.query.JoinClause .

  • No labels