Skip to content

Sorting search results

1.4.0 1.1.0

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 cannot specify any sort order.

Annotated sort options, as you would define them in the Java SDK
1
2
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
3
4
5
6
7
IndexSearchRequest index = Atlan.getDefaultClient().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
 8
 9
10
11
IndexSearchResponse response = index.search();
for (Asset result : response) {
    // Do something with the ordered results...
}
Annotated sort options, as you would define them in the Python SDK
1
2
3
4
5
6
7
from pyatlan.client.atlan import AtlanClient
from pyatlan.model.enums import SortOrder
from pyatlan.model.assets import Referenceable
from pyatlan.model.search import IndexSearchRequest, DSL

by_update = Referenceable.UPDATE_TIME.order(SortOrder.DESCENDING)  # (1)
by_creation = Referenceable.CREATE_TIME.order(SortOrder.ASCENDING)  # (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.
  2. In this example, our first results would be the oldest ones (based on when they were created).
Build the request
 8
 9
10
11
12
13
index = (
    FluentSearch()
    .where(...)  # (1)
    .sort(by_update)  # (2)
    .sort(by_creation)  # (3)
).to_request()
  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
14
15
16
17
client = AtlanClient();
response = client.asset.search(index)
for result in response:
    # Do something with the ordered results...
Annotated sort options, as you would define them in the Java SDK
1
2
val byUpdate = Asset.UPDATE_TIME.order(SortOrder.Desc) // (1)
val 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
    val 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
    val byCreation = SortOptions.of(s -> s
        .field(FieldSort.of(f -> f // (2)
            .field("__timestamp")
            .order(SortOrder.Asc))))
    
Build the request
3
4
5
6
7
val index = Atlan.getDefaultClient().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
 8
 9
10
11
val response = index.search()
for (result in response) {
    // Do something with the ordered results...
}
POST /api/meta/search/indexsearch
1
2
3
4
5
6
7
8
9
{
  "dsl": {
    "query": {...}, // (1)
    "sort": [ // (2)
      { "__modificationTimestamp": { "order": "desc" }}, // (3)
      { "__timestamp": { "order": "asc" }} // (4)
    ]
  }
}
  1. You still need a query, to get some results to sort.
  2. Note that the sort itself is an array — hence you can sort by multiple properties.
  3. 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.
  4. For any results that have identical modification dates, we would then sort within those results to present the oldest (created) results first.

See Elastic's documentation for more details

The examples above should cover most scenarios. If you want to get fancy, see Elastic's documentation on sorting .