Skip to main content

Sorting search results

You can sort search results by one or multiple properties.

Limited to iterating through less than 100,000 results

You can sort any number of results; however, if you plan to page through more than 100,000 total results you can't specify any sort order.

Annotated sort options, as you would define them in the Java SDK
SortOptions byUpdate = Asset.UPDATE_TIME.order(SortOrder.Desc); // (1)
SortOptions byCreation = Asset.CREATE_TIME.order(SortOrder.Asc); // (2)
  1. For each property you can specify whether to sort the results in descending order or ascending order. In this example our first results would be the ones changed most recently.

    Equivalent sort option from Elastic
    SortOptions byUpdate = SortOptions.of(s -> s
    .field(FieldSort.of(f -> f // (1)
    .field("__modificationTimestamp")
    .order(SortOrder.Desc))));
  2. In this example, our first results would be the oldest ones (based on when they were created).

    Equivalent sort option from Elastic
    SortOptions byCreation = SortOptions.of(s -> s
    .field(FieldSort.of(f -> f // (2)
    .field("__timestamp")
    .order(SortOrder.Asc))));
Build the request
IndexSearchRequest index = client.assets.select()
.where(...) // (1)
.sort(byUpdate) // (2)
.sort(byCreation) // (3)
.toRequest();
  1. You still need a query, to get some results to sort.
  2. Note that the sort itself is an array—hence we can add multiple sort options and they'll be evaluated in order (to sort by multiple properties). In this example our first results would be the ones changed most recently.
  3. For any results that have identical modification dates, we would then sort within those results to present the oldest (created) results first.
Run the search
IndexSearchResponse response = index.search(client);
for (Asset result : response)
Was this page helpful?