Find rows (Go)
Finds rows in a table using filter and sort clauses, including vector search.
For general information about working with tables and rows, see About tables with the Data API (Go).
|
Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart. |
Result
Returns a cursor (*cursors.TableFindCursor) for iterating over rows that match the specified filter and sort clauses.
The columns included in the returned rows depend on the subset of columns that were requested in the projection.
If requested and applicable, each row will also include a $similarity key with a numeric similarity score that represents the closeness of the sort vector and the row’s vector.
You must iterate over the cursor to fetch matching rows. For details about iteration, see Iterate over found rows.
Parameters
Use the Find method, which belongs to the Table type.
Method signature
func (t *Table) Find(
f TableFilter,
opts ...options.TableFindOption
) *cursors.TableFindCursor
| Name | Type | Summary |
|---|---|---|
|
|
Optional. An object that defines filter criteria using the Data API filter syntax. The method only finds rows that match the filter criteria. Filters can improve performance by reducing the number of rows that the Data API processes. For a list of available filter operators and more examples, see Filter operators for tables (Go). To perform a vector search, use To avoid fetching unnecessary rows, which can contain tombstones, DataStax recommends that you use a filter that limits the number of rows scanned. For example, filter on partition key columns or indexed columns. Default: No filter For an example, see Use filters to find rows. |
|
Optional.
A builder to generate options for this operation.
See Methods of the |
| Method | Summary |
|---|---|
|
Optional. Controls which columns are included or excluded in the returned rows. For more information, see Projections for tables (Go). DataStax recommends a projection to avoid unnecessarily returning large columns, such as Default: All columns For examples, see Include only specific columns in the response and Exclude specific columns from the response. |
|
Optional. Sorts rows by one or more columns, or performs a vector search. For more information, see Sort clauses for tables (Go). For examples, see Use sorting to find rows and Use vector search with a search vector to find rows. |
|
Optional.
Limit the total number of rows returned.
Once For vector search, a lower limit reduces the accuracy of the search and the time required for the search. |
|
Optional. The number of rows to bypass (skip) before returning rows. The API excludes the first This parameter only applies if you also explicitly specify an ascending or descending sort criterion. This parameter is not valid with vector search. |
|
Optional.
Whether to include a Default: false For an example, see Include the similarity score with the result. |
|
Optional.
The Used to manually request the next page of results. This is useful for cases where an external action triggers fetching the next page of results. For an example, see Iterate over found rows. |
|
Optional. General API options for this operation, including the timeout. |
Examples
The following examples demonstrate how to find rows in a table.
Use filters to find rows
You can use a filter to find rows that match specific criteria.
For example, you can find rows with an is_checked_out value of false and a number_of_pages value less than 300.
For optimal performance, you only filter on indexed columns. The Data API returns a warning if you filter on a non-indexed column.
For a list of available filter operators, see Filter operators for tables (Go).
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"
)
func main() {
// Get an existing table
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
table := database.Table("TABLE_NAME")
ctx := context.Background()
// Find rows
cursor := table.Find(
filter.And(
filter.Eq("is_checked_out", false),
filter.Lt("number_of_pages", 300),
),
)
// 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())
}
}
Use vector search with a search vector to find rows
Perform a vector search by providing a search vector in the sort clause. This returns the row whose vector column value is most similar to the provided search vector.
The vector column must be indexed.
If your table has multiple vector columns, you can only sort on one vector column at a time.
You can use the datatypes.Vector struct to binary-encode your search vector.
DataStax recommends that you always use a datatypes.Vector struct instead of a slice of floats to improve performance.
When you read the value of a vector column, the client always returns a datatypes.Vector struct when you decode the results into astra.Row or map[string]any.
To decode the vector as a slice of floats, decode into a custom struct that defines the vector column as []float32.
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(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
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())
}
}
Use lexicographical matching to find rows
|
Lexicographical matching is currently in public preview. Development is ongoing, and the features and functionality are subject to change. Hyper-Converged Database (HCD), and the use of such, is subject to the DataStax Preview Terms. |
There are two ways to use lexicographical matching to find rows with the Data API:
-
Sort to find rows with a
textorasciicolumn value that is most relevant to a given string of space-separated keywords or terms. -
Filter with the
$matchoperator to find rows with atextorasciicolumn value that is a lexicographical match to the specified string of space-separated keywords or terms
You can use these strategies together or separately.
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).
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(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
table := database.Table("TABLE_NAME")
ctx := context.Background()
// Find rows
cursor := table.Find(
filter.Table.LexicalMatch(
"summary",
"futuristic laboratory discovery",
),
options.TableFind().
SetSort(sort.Table.Lexical("summary", "futuristic laboratory")),
)
// 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())
}
}
Use sorting to find rows
You can use a sort clause to sort rows by one or more columns.
For best performance, only sort on columns that are indexed or that are part of the primary key.
For more information, see Sort clauses for tables (Go).
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(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
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())
}
}
Use an empty filter to find all rows
To find all rows, use an empty filter.
Avoid this if you have a large number of rows.
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"
)
func main() {
// Get an existing table
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
table := database.Table("TABLE_NAME")
ctx := context.Background()
// Find rows
cursor := table.Find(filter.F{})
// 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())
}
}
Include the similarity score with the result
If you use a vector search to find rows, you can also include a $similarity property in the result. The $similarity value represents the closeness of the sort vector and the value of the row’s vector column.
This parameter doesn’t work with vectorize; it only works if you provide the search vector for vector search directly.
The client always returns the similarity score as a datatypes.Vector struct.
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(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
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})).
SetIncludeSimilarity(true),
)
// 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())
}
}
Include only specific columns in the response
To specify which columns to include or exclude in the returned row, use a projection.
The following example demonstrates an inclusive projection.
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"
)
func main() {
// Get an existing table
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
table := database.Table("TABLE_NAME")
ctx := context.Background()
// Find rows
cursor := table.Find(
filter.Lt("number_of_pages", 300),
options.TableFind().
SetProjection(map[string]any{
"is_checked_out": true,
"title": true,
}),
)
// 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())
}
}
Exclude specific columns from the response
To specify which columns to include or exclude in the returned row, use a projection.
The following example demonstrates an exclusive projection.
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"
)
func main() {
// Get an existing table
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
table := database.Table("TABLE_NAME")
ctx := context.Background()
// Find rows
cursor := table.Find(
filter.Lt("number_of_pages", 300),
options.TableFind().
SetProjection(map[string]any{
"is_checked_out": false,
"title": false,
}),
)
// 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())
}
}
Limit the number of rows returned
Specify a limit to only fetch up to a certain number of rows.
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"
)
func main() {
// Get an existing table
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
table := database.Table("TABLE_NAME")
ctx := context.Background()
// Find rows
cursor := table.Find(
filter.And(
filter.Eq("is_checked_out", false),
filter.Lt("number_of_pages", 300),
),
options.TableFind().
SetLimit(3),
)
// 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())
}
}
Skip rows
You can specify a number of rows to skip (bypass) before returning rows.
You can only do this if your find explicitly includes an ascending or descending sort criterion.
You cannot do this in conjunction with vector search.
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(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
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")).
SetSkip(5),
)
// 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())
}
}
Use filter, sort, and projection together
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(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
table := database.Table("TABLE_NAME")
ctx := context.Background()
// Find rows
cursor := table.Find(
filter.And(
filter.Eq("is_checked_out", false),
filter.Lt("number_of_pages", 300),
),
options.TableFind().
SetSort(sort.Asc("rating").Desc("title")).
SetProjection(map[string]any{
"is_checked_out": true,
"title": true,
}),
)
// 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())
}
}
Iterate over found rows
Use a for loop and the Next() method on the cursor to iterate over the found rows.
The Data API returns results in pages.
The TableFindCursor result handles the paging internally.
Alternatively, you can use the SetInitialPageState() method to fetch a specific page of results.
This is useful for cases where an external action triggers fetching the next page of results.
For example, you might use this feature if you implement a "Load More" button or an infinite scroll interface.
If you need a list of all results, call All().
However, the time and memory required for this operation depend on the number of results.
This is not recommended when you expect a large number of roes.
Example using for and Next():
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"
)
func main() {
// Get an existing table
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
table := database.Table("TABLE_NAME")
ctx := context.Background()
// Find rows
cursor := table.Find(
filter.And(
filter.Eq("is_checked_out", false),
filter.Lt("number_of_pages", 300),
),
)
// 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 using SetInitialPageState():
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"
)
func main() {
// Get an existing table
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
table := database.Table("TABLE_NAME")
ctx := context.Background()
// Create the filter
filterClause := filter.And(
filter.Eq("is_checked_out", false),
filter.Lt("number_of_pages", 300),
)
// Get the first page
cursor1 := table.Find(
filterClause,
)
cursor1.Next(ctx)
var results1 []astra.Row
if err := cursor1.DecodeBuffered(&results1, 0); err != nil {
log.Fatal(err)
}
for _, row := range results1 {
fmt.Println(row.ToMap())
}
if err := cursor1.Err(); err != nil {
log.Fatal(err)
}
paginationState1 := cursor1.NextPageState()
// Get the next page
if paginationState1 != nil {
cursor2 := table.Find(
filterClause,
options.TableFind().
SetInitialPageState(*paginationState1),
)
cursor2.Next(ctx)
var results2 []astra.Row
if err := cursor2.DecodeBuffered(&results2, 0); err != nil {
log.Fatal(err)
}
for _, row := range results2 {
fmt.Println(row.ToMap())
}
if err := cursor2.Err(); err != nil {
log.Fatal(err)
}
paginationState2 := cursor2.NextPageState()
_ = paginationState2 // Can be used for further pagination
}
}
Client reference
For more information, see the client reference.