Sort clauses for tables (Go)
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.
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/sort"
)
func main() {
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
ctx := context.Background()
// Find rows
cursor := table.Find(
filter.Eq("is_checked_out", false),
options.TableFind().
SetSort(sort.Asc("rating").Desc("title")),
)
// Iterate over the found rows
for cursor.Next(ctx) {
var row astra.Row
if err := cursor.Decode(&row); err != nil {
log.Fatal(err)
}
fmt.Println(row.ToMap())
}
}
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. To find the rows with a vector column value that is most similar to the vector embeddings of a given string, use a sort with the string that you want to vectorize and match. For more information, see Find data with vector search.
The vector column must be indexed.
To perform a vector search with a string instead of vector embeddings, the vector column must have a vectorize integration.
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
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/sort"
)
func main() {
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
ctx := context.Background()
// Find rows
cursor := table.Find(
filter.F{},
options.TableFind().
SetSort(sort.Table.Vector("summary_genres_vector", []float32{0.08, -0.62, 0.39})),
)
// Iterate over the found rows
for cursor.Next(ctx) {
var row astra.Row
if err := cursor.Decode(&row); err != nil {
log.Fatal(err)
}
fmt.Println(row.ToMap())
}
}
Example sorting against a search string
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/sort"
)
func main() {
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
ctx := context.Background()
// Find rows
cursor := table.Find(
filter.F{},
options.TableFind().
SetSort(sort.Table.Vectorize("summary_genres_vector", "Text to vectorize")),
)
// Iterate over the found rows
for cursor.Next(ctx) {
var row astra.Row
if err := cursor.Decode(&row); err != nil {
log.Fatal(err)
}
fmt.Println(row.ToMap())
}
}
Sort by lexicographical matching (BM25-based ranking)
|
Lexicographical matching is currently in public preview. Development is ongoing, and the features and functionality are subject to change. Astra DB Serverless, and the use of such, is subject to the DataStax Preview Terms. |
To find rows with a text or ascii column value that is most relevant to a given string of space-separated keywords or terms, sort on a column that is associated with a text index.
Lexicographical matching is only available for text or ascii columns that have a text index, not a regular index.
For more information, see Create a text index (Go) and Indexes in tables (Go).
When performing this type of sort, you can’t include additional sort clauses.
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/filter"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/sort"
)
func main() {
ctx := context.Background()
// Get an existing table
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
table := database.Table("TABLE_NAME")
// Find a row
var result astra.Row
err := table.FindOne(
ctx,
filter.F{},
options.TableFindOne().
SetSort(sort.Table.Lexical("summary", "futuristic laboratory")),
).Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.ToMap())
}
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: