Sort clauses for tables (HTTP)
|
For details about additional languages, see: |
Many Data API commands use sort clauses to sort rows by column values or to perform vector search.
Sort by column values
Use a sort clause to sort rows by column values in ascending or descending order.
When sorting by multiple columns, the order of the columns in the sort clause controls the order of the sorting.
For best performance, only sort on indexed columns, partition keys, and clustering keys. Otherwise, the Data API can perform in-memory sorting, which can have performance implications.
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"find": {
"filter": { "is_checked_out": false },
"sort": {
"rating": 1,
"title": -1
}
}
}'
Sort by vector similarity (vector search)
To find the rows with a vector column value that is most similar to a given vector, use a sort clause with the vector embeddings that you want to match. For more information, see Find data with vector search.
The vector column must be indexed.
If your table has multiple vector columns, you can only sort on one vector column at a time.
Vector search returns up to 1000 rows per search operation, unless you specify a lower limit.
Example sorting against a search vector
You can provide the search vector as an array of floats, or you can use $binary to provide the search vector as a Base64-encoded string.
$binary can be more performant.
For more information about how to convert an array of floats to a Base64-encoded string, see BLOB type (HTTP).
- Array of floats
-
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \ --header "Token: APPLICATION_TOKEN" \ --header "Content-Type: application/json" \ --data '{ "find": { "sort": { "summary_genres_vector": [0.08, -0.62, 0.39] } } }' - $binary
-
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \ --header "Token: APPLICATION_TOKEN" \ --header "Content-Type: application/json" \ --data '{ "find": { "sort": { "summary_genres_vector": {"$binary": "PaPXCr8euFI+x64U"} } } }'
Commands that support sort clauses for tables
There are many Data API commands that support sort clauses for tables.
For more examples of sort clauses, see the reference for the command that you want to run: