Find rows
This Astra DB Serverless feature 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. The Data API tables commands are available through HTTP and the clients. If you use a client, tables commands are available only in client versions 2.0-preview or later. For more information, see Data API client upgrade guide. |
Finds rows in a table using filter and sort clauses.
For general information about working with tables and rows, see About tables.
Method signature
-
Python
-
TypeScript
-
Java
-
curl
The following method belongs to the astrapy.Table
class.
find(
filter: Dict[str, Any],
*,
projection: Iterable[str] | Dict[str, bool],
row_type: type,
skip: int,
limit: int,
include_similarity: bool,
include_sort_vector: bool,
sort: Dict[str, Any],
request_timeout_ms: int,
timeout_ms: int,
) -> TableFindCursor
The following method belongs to the Table
class.
find(
filter: TableFilter<WSchema>,
options?: {
sort?: Sort,
projection?: Projection,
limit?: number,
skip?: number
includeSimilarity?: boolean,
includeSortVector?: boolean,
timeout?: number | TimeoutDescriptor,
}
): TableFindCursor<WithSim<RSchema>, WithSim<RSchema>>
The following methods belong to the com.datastax.astra.client.tables.Table
class.
TableCursor<T, T> find(
Filter filter,
TableFindOptions options
)
TableCursor<T, T> find(
Filter filter
)
TableCursor<T, T> find(
TableFindOptions options
)
<R> TableCursor<T, R> find(
Filter filter,
TableFindOptions options,
Class<R> rowClass
)
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"find": {
"filter": FILTER,
"sort": SORT,
"projection": PROJECTION,
"options": {
"includeSimilarity": BOOLEAN,
"includeSortVector": BOOLEAN,
"skip": INTEGER,
"limit": INTEGER
}
}
}'
Result
-
Python
-
TypeScript
-
Java
-
curl
Returns a cursor (astrapy.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.
If requested when executing a vector search with vectorize, the result will also include the sort vector.
When you iterate over the cursor, rows are fetched in batches. The fetched batches may reflect real-time updates on the table.
If you need a list of all results, you can invoke .to_list()
on the cursor instead of iterating over the cursor.
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 rows.
For more information about iterating over and manipulating the cursor, see FindCursor.
Returns a cursor (TableFindCursor<RSchema>
) 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.
If requested when executing a vector search with vectorize, the result will also include the sort vector.
If you need a list of all results, you can call toArray()
on the cursor instead of iterating over the cursor.
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 rows.
Returns a cursor (TableCursor<T, R>
) for iterating over rows that match the specified filter and sort clauses.
If rowClass
is not specified, then the type R
is the same type as that of the rows in the table.
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.
If requested when executing a vector search with vectorize, the result will also include the sort vector.
If you need a list of all results, you can use .to_list()
to exhaust the cursor.
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 rows.
The response includes a data.documents
property, which is an array of objects representing 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.
If the query supports pagination, the response also includes a data.nextPageState
property, which indicates the ID of the next page of results, if any.
For non-vector searches, the results will be paginated if more than 20 rows match the specified filter and sort clauses.
For vector search (with $vector
or $vectorize
), returns a single page of up to 1000 rows (or a lower amount if specified).
Some operations do not support pagination. These include:
-
Operations that require in-memory sort, such as allow filtering on non-indexed columns. The Data API returns a warning if this happens.
-
Vector searches.
-
Certain combinations of
sort
andfilter
options.
If requested when executing a vector search with vectorize, the result also includes a status.sortVector
property, which is the sort vector used for the search.
Example response:
{
"data": {
"documents":[
{
"_id":"85a54382-9227-4075-a543-829227407556",
"title":"Within Silence of the Past",
"isCheckedOut":false
},
{
"_id":"aa762475-4fc1-4477-b624-754fc1f477c7",
"title":"Beyond Dreams and Forgotten Worlds",
"isCheckedOut":false
}
],
"nextPageState":"LQAAAAEBAAAAJGQ2OTk5NzY2LTgyODQtNDc3Mi05OTk3LTY2ODI4NGU3NzJjYQDwf///6wA="
}
}
Example response if no rows were found:
{
"data": {
"documents": [],
"nextPageState": null
}
}
Parameters
-
Python
-
TypeScript
-
Java
-
curl
For best performance, filter and sort on indexed columns, partition keys, and clustering keys. Filtering on non-indexed columns can use allow filtering, which is inefficient and resource-intensive, especially for large datasets. With the Data API clients, allow filtering operations can hit the client timeout limit before the underlying HTTP operation is complete. An empty filter ( Additionally, the Data API can perform in-memory sorting, depending on the columns you sort on, the table’s partitioning structure, and whether the sorted columns are indexed. In-memory sorts can have performance implications. |
Name | Type | Summary |
---|---|---|
|
|
A dictionary expressing which condition the returned row must satisfy. You can use filter operators to compare columns with literal values. For example:
You cannot filter on To perform a vector search, use |
|
|
This dictionary parameter controls the sorting order and, therefore, determines which row is returned if there are multiple matches.
The |
|
|
Select a subset of columns to include in the response for the returned row:
DataStax recommends using projections to optimize bandwidth, especially to avoid unnecessarily returning large columns, such as For more information and examples, see Projections for tables. |
|
|
This parameter acts a formal specifier for the type checker.
If omitted, the resulting cursor is implicitly a |
|
|
Optionally specify a number of rows to bypass (skip) before returning rows.
The first This parameter is only valid with |
|
|
Limit the total number of rows returned from the table.
The returned cursor stops yielding rows either when it reaches the |
|
|
If true, the returned rows include a |
|
|
If true, you can call the You can’t use |
|
|
A timeout, in milliseconds, to impose on each individual HTTP request to the Data API to accomplish the operation. If not provided, the Table defaults apply. This parameter is aliased as |
For best performance, filter and sort on indexed columns, partition keys, and clustering keys. Filtering on non-indexed columns can use allow filtering, which is inefficient and resource-intensive, especially for large datasets. With the Data API clients, allow filtering operations can hit the client timeout limit before the underlying HTTP operation is complete. An empty filter ( Additionally, the Data API can perform in-memory sorting, depending on the columns you sort on, the table’s partitioning structure, and whether the sorted columns are indexed. In-memory sorts can have performance implications. |
Name | Type | Summary |
---|---|---|
|
|
An object that defines filter criteria using the Data API filter syntax. For more information and examples, see Filter operators for tables. You cannot filter on To perform a vector search, use |
|
|
The options for this operation |
Options (TableFindOptions
):
Name | Type | Summary |
---|---|---|
|
|
The |
|
|
Select a subset of columns to include in the response for the returned rows:
If empty or unspecified, the default projection (all columns) is used. DataStax recommends using projections to optimize bandwidth, especially to avoid unnecessarily returning large columns, such as Additionally, DataStax recommends providing your own type for the returned rows because projections can break typing guarantees.
If your query includes For more information and examples, see Projections for tables. |
|
|
Optionally specify a number of rows to bypass (skip) before returning rows.
The first This parameter is only valid with |
|
|
Limit the total number of rows returned from the table.
The returned cursor stops yielding rows either when it reaches the |
|
|
If true, the returned rows include a If your query includes |
|
|
If true, you can call the You can’t use |
|
|
The client-side timeout for this operation. |
For best performance, filter and sort on indexed columns, partition keys, and clustering keys. Filtering on non-indexed columns can use allow filtering, which is inefficient and resource-intensive, especially for large datasets. With the Data API clients, allow filtering operations can hit the client timeout limit before the underlying HTTP operation is complete. An empty filter ( Additionally, the Data API can perform in-memory sorting, depending on the columns you sort on, the table’s partitioning structure, and whether the sorted columns are indexed. In-memory sorts can have performance implications. |
Name | Type | Summary |
---|---|---|
|
A filter expressing which condition the returned rows must satisfy.
You can use filter operators to compare columns with literal values.
Filters can be instantiated with its constructor and specialized with method You cannot filter on To perform a vector search, use |
|
|
A wrapper for the different options and specialization of this search. |
|
|
|
This parameter acts a formal specifier for the type checker.
If omitted, the resulting cursor is implicitly a |
Name | Type | Summary |
---|---|---|
|
The |
|
|
Select a subset of columns to include in the response for the returned rows:
If empty or unspecified, the default projection (all columns) is used. DataStax recommends using projections to optimize bandwidth, especially to avoid unnecessarily returning large columns, such as For more information and examples, see Projections for tables. |
|
|
|
If true, the returned rows include a |
|
|
If true, you can call the You can’t use |
|
|
Optionally specify a number of rows to bypass (skip) before returning rows.
The first This parameter is only valid with |
|
|
Limit the total number of rows returned from the table.
The returned cursor stops yielding rows either when it reaches the |
|
|
A timeout, in milliseconds (long), to impose on the underlying API request. If not provided, the Table defaults apply. |
For best performance, filter and sort on indexed columns, partition keys, and clustering keys. Filtering on non-indexed columns can use allow filtering, which is inefficient and resource-intensive, especially for large datasets. With the Data API clients, allow filtering operations can hit the client timeout limit before the underlying HTTP operation is complete. An empty filter ( Additionally, the Data API can perform in-memory sorting, depending on the columns you sort on, the table’s partitioning structure, and whether the sorted columns are indexed. In-memory sorts can have performance implications. |
Name | Type | Summary |
---|---|---|
|
|
The Data API command to retrieve multiple rows in a table based on one or more of |
|
|
An object that defines filter criteria using the Data API filter syntax. For a list of available operators, see Filter operators for tables. You cannot filter on To perform a vector search, use |
|
|
Perform a vector search or set the order in which rows are returned. For more information and examples, see sort clauses and Vector type. |
|
|
Select a subset of columns to include in the response for each returned row.
If empty or unset, the default projection is used.
The default projection includes all columns, but it omits The response always omits For more information and examples, see Projections for tables. |
|
|
Specify a number of rows to bypass (skip) before returning rows.
The first This parameter is only valid with |
|
|
Limit the total number of rows returned.
Pagination can occur if more than 20 rows are returned in the current set of matching rows.
Once the |
|
|
If true, the response includes a |
|
|
If true, the response includes the
You can’t use |
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 matches specific criteria.
For example, you can find rows with an isCheckedOut
value of false
and a numberOfPages
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.
You cannot filter on map
, list
, or set
columns.
For a list of available filter operators, see Filter operators for tables.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find rows
cursor = table.find(
{
"$and": [
{"isCheckedOut": False},
{"numberOfPages": {"$lt": 300}},
]
}
)
# Iterate over the found rows
for row in cursor:
print(row)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing table
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const table = database.table('TABLE_NAME');
(async function () {
// Find rows
const cursor = table.find({
$and: [{ isCheckedOut: false }, { numberOfPages: { $lt: 300 } }],
});
// Iterate over the found rows
for await (const row of cursor) {
console.log(row);
}
})();
package com.example;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.definition.rows.Row;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.cursor.TableCursor;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
public class Find {
public static void main(String[] args) {
// Get an existing table
Table<Row> table = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter = Filters.and(
Filters.eq("isCheckedOut", false),
Filters.lt("numberOfPages", 300));
TableFindOptions options = new TableFindOptions();
TableCursor<Row, Row> cursor = table.find(filter, options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"find": {
"filter": {"$and": [
{"isCheckedOut": false},
{"numberOfPages": {"$lt": 300}}
]}
}
}'
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.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
from astrapy.data_types import DataAPIVector
# Get an existing table
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find rows
cursor = table.find(
{},
sort={"summaryGenresVector": DataAPIVector([.12, .52, .32])}
)
# Iterate over the found rows
for row in cursor:
print(row)
import { DataAPIClient, vector } from '@datastax/astra-db-ts';
// Get an existing table
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const table = database.table('TABLE_NAME');
(async function () {
// Find rows
const cursor = table.find(
{},
{ sort: { summaryGenresVector: vector([.12, .52, .32]) } }
);
// Iterate over the found rows
for await (const row of cursor) {
console.log(row);
}
})();
package com.example;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.vector.DataAPIVector;
import com.datastax.astra.client.tables.definition.rows.Row;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.cursor.TableCursor;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
public class Find {
public static void main(String[] args) {
// Get an existing table
Table<Row> table = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
TableFindOptions options = new TableFindOptions()
.sort(Sort.vector("summaryGenresVector", new DataAPIVector(new float[] {0.12f, 0.52f, 0.32f})));
TableCursor<Row, Row> cursor = table.find(options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
You can provide the search vector as an array or as a binary.
Example with the search vector as an array:
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"find": {
"sort": { "summaryGenresVector": [.12, .52, .32] }
}
}'
Example with the search vector as a binary:
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"find": {
"sort": { "summaryGenresVector": { "$binary": "PczMzb5MzM0+mZma" } }
}
}'
Use vector search with a search string to find rows
Perform a vector search by providing a search string in the sort clause. The search string is converted to a search vector, and the row whose vector column value is most similar to the search vector is returned.
The vector column must have a vectorize integration. The vector column must be indexed.
If your table has multiple vector columns, you can only sort on one vector column at a time.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find rows
cursor = table.find(
{},
sort={"summaryGenresVector": "Text to vectorize"}
)
# Iterate over the found rows
for row in cursor:
print(row)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing table
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const table = database.table('TABLE_NAME');
(async function () {
// Find rows
const cursor = table.find(
{},
{ sort: { summaryGenresVector: "Text to vectorize" } }
);
// Iterate over the found rows
for await (const row of cursor) {
console.log(row);
}
})();
package com.example;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.tables.definition.rows.Row;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.cursor.TableCursor;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
public class Find {
public static void main(String[] args) {
// Get an existing table
Table<Row> table = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
TableFindOptions options = new TableFindOptions()
.sort(Sort.vectorize("summaryGenresVector", "Text to vectorize"));
TableCursor<Row, Row> cursor = table.find(options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"find": {
"sort": { "summaryGenresVector": "Text to vectorize" }
}
}'
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.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
from astrapy.constants import SortMode
# Get an existing table
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find rows
cursor = table.find(
{"isCheckedOut": False},
sort={
"rating": SortMode.ASCENDING,
"title": SortMode.DESCENDING,
}
)
# Iterate over the found rows
for row in cursor:
print(row)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing table
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const table = database.table('TABLE_NAME');
(async function () {
// Find rows
const cursor = table.find(
{ "isCheckedOut": false },
{ sort: {
rating: 1, // ascending
title: -1 // descending
} }
);
// Iterate over the found rows
for await (const row of cursor) {
console.log(row);
}
})();
package com.example;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.definition.rows.Row;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.cursor.TableCursor;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
public class Find {
public static void main(String[] args) {
// Get an existing table
Table<Row> table = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter = Filters.eq("isCheckedOut", false);
TableFindOptions options = new TableFindOptions()
.sort(Sort.ascending("rating"))
.sort(Sort.descending("title"));
TableCursor<Row, Row> cursor = table.find(filter, options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"find": {
"filter": { "isCheckedOut": false },
"sort": {
"rating": 1,
"title": -1
}
}
}'
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.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find rows
cursor = table.find({})
# Iterate over the found rows
for row in cursor:
print(row)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing table
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const table = database.table('TABLE_NAME');
(async function () {
// Find rows
const cursor = table.find({});
// Iterate over the found rows
for await (const row of cursor) {
console.log(row);
}
})();
package com.example;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.tables.definition.rows.Row;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.cursor.TableCursor;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
public class Find {
public static void main(String[] args) {
// Get an existing table
Table<Row> table = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter = new Filter();
TableFindOptions options = new TableFindOptions();
TableCursor<Row, Row> cursor = table.find(filter, options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"find": {
"filter": {}
}
}'
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.
-
Python
-
TypeScript
-
Java
-
curl
# Get an existing table
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find rows
cursor = table.find(
{},
sort={"summaryGenresVector": "Text to vectorize"},
include_similarity=True
)
# Iterate over the found rows
for row in cursor:
print(row)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing table
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const table = database.table('TABLE_NAME');
(async function () {
// Find rows
const cursor = table.find(
{},
{
sort: { summaryGenresVector: "Text to vectorize" },
includeSimilarity: true
},
);
// Iterate over the found rows
for await (const row of cursor) {
console.log(row);
}
})();
package com.example;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.tables.definition.rows.Row;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.cursor.TableCursor;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
public class Find {
public static void main(String[] args) {
// Get an existing table
Table<Row> table = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
TableFindOptions options = new TableFindOptions()
.sort(Sort.vectorize("summaryGenresVector", "Text to vectorize"))
.includeSimilarity(true);
TableCursor<Row, Row> cursor = table.find(options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"find": {
"sort": { "summaryGenresVector": "Text to vectorize" },
"options": { "includeSimilarity": true }
}
}'
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.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find rows
cursor = table.find(
{"numberOfPages": {"$lt": 300}},
projection={"isCheckedOut": True, "title": True}
)
# Iterate over the found rows
for row in cursor:
print(row)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing table
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const table = database.table('TABLE_NAME');
(async function () {
// Find rows
const cursor = table.find(
{ numberOfPages: { $lt: 300 } },
{ projection: { isCheckedOut: true, title: true} },
);
// Iterate over the found rows
for await (const row of cursor) {
console.log(row);
}
})();
package com.example;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.core.query.Projection;
import com.datastax.astra.client.tables.definition.rows.Row;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.cursor.TableCursor;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
public class Find {
public static void main(String[] args) {
// Get an existing table
Table<Row> table = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter = Filters.lt("numberOfPages", 300);
TableFindOptions options = new TableFindOptions()
.projection(Projection.include("isCheckedOut", "title"));
TableCursor<Row, Row> cursor = table.find(filter, options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"find": {
"filter": {"numberOfPages": {"$lt": 300}},
"projection": {"isCheckedOut": true, "title": true}
}
}'
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.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find rows
cursor = table.find(
{"numberOfPages": {"$lt": 300}},
projection={"isCheckedOut": False, "title": False}
)
# Iterate over the found rows
for row in cursor:
print(row)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing table
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const table = database.table('TABLE_NAME');
(async function () {
// Find rows
const cursor = table.find(
{ numberOfPages: { $lt: 300 } },
{ projection: { isCheckedOut: false, title: false} },
);
// Iterate over the found rows
for await (const row of cursor) {
console.log(row);
}
})();
package com.example;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.core.query.Projection;
import com.datastax.astra.client.tables.definition.rows.Row;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.cursor.TableCursor;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
public class Find {
public static void main(String[] args) {
// Get an existing table
Table<Row> table = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter = Filters.lt("numberOfPages", 300);
TableFindOptions options = new TableFindOptions()
.projection(Projection.exclude("isCheckedOut", "title"));
TableCursor<Row, Row> cursor = table.find(filter, options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"find": {
"filter": {"numberOfPages": {"$lt": 300}},
"projection": {"isCheckedOut": false, "title": false}
}
}'
Limit the number of rows returned
Specify a limit to only fetch up to a certain number of rows.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find rows
cursor = table.find(
{
"$and": [
{"isCheckedOut": False},
{"numberOfPages": {"$lt": 300}},
]
},
limit=10
)
# Iterate over the found rows
for row in cursor:
print(row)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing table
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const table = database.table('TABLE_NAME');
(async function () {
// Find rows
const cursor = table.find(
{
$and: [{ isCheckedOut: false }, { numberOfPages: { $lt: 300 } }],
},
{ limit: 10 },
);
// Iterate over the found rows
for await (const row of cursor) {
console.log(row);
}
})();
package com.example;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.definition.rows.Row;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.cursor.TableCursor;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
public class Find {
public static void main(String[] args) {
// Get an existing table
Table<Row> table = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter = Filters.and(
Filters.eq("isCheckedOut", false),
Filters.lt("numberOfPages", 300));
TableFindOptions options = new TableFindOptions()
.limit(10);
TableCursor<Row, Row> cursor = table.find(filter, options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"find": {
"filter": {"$and": [
{"isCheckedOut": false},
{"numberOfPages": {"$lt": 300}}
]},
"options": {
"limit": 10
}
}
}'
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.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
from astrapy.constants import SortMode
# Get an existing table
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find rows
cursor = table.find(
{"isCheckedOut": False},
sort={
"rating": SortMode.ASCENDING,
"title": SortMode.DESCENDING,
},
skip=5
)
# Iterate over the found rows
for row in cursor:
print(row)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing table
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const table = database.table('TABLE_NAME');
(async function () {
// Find rows
const cursor = table.find(
{ "isCheckedOut": false },
{ sort: {
rating: 1, // ascending
title: -1 // descending
},
skip: 5,
}
);
// Iterate over the found rows
for await (const row of cursor) {
console.log(row);
}
})();
package com.example;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.definition.rows.Row;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.cursor.TableCursor;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
public class Find {
public static void main(String[] args) {
// Get an existing table
Table<Row> table = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter = Filters.eq("isCheckedOut", false);
TableFindOptions options = new TableFindOptions()
.sort(Sort.ascending("rating"))
.sort(Sort.descending("title"))
.skip(5);
TableCursor<Row, Row> cursor = table.find(filter, options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"find": {
"filter": { "isCheckedOut": false },
"sort": {
"rating": 1,
"title": -1
},
"options": {
"skip": 5
}
}
}'
Use filter, sort, and projection together
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
from astrapy.constants import SortMode
# Get an existing table
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find rows
cursor = table.find(
{
"$and": [
{"isCheckedOut": False},
{"numberOfPages": {"$lt": 300}},
]
},
sort={
"rating": SortMode.ASCENDING,
"title": SortMode.DESCENDING,
},
projection={"isCheckedOut": True, "title": True}
)
# Iterate over the found rows
for row in cursor:
print(row)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing table
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const table = database.table('TABLE_NAME');
(async function () {
// Find rows
const cursor = table.find(
{
$and: [{ isCheckedOut: false }, { numberOfPages: { $lt: 300 } }],
},
{
sort: {
rating: 1, // ascending
title: -1, // descending
},
projection: {
isCheckedOut: true,
title: true,
},
},
);
// Iterate over the found rows
for await (const row of cursor) {
console.log(row);
}
})();
package com.example;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.core.query.Projection;
import com.datastax.astra.client.tables.definition.rows.Row;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
import com.datastax.astra.client.tables.cursor.TableCursor;
public class Find {
public static void main(String[] args) {
// Get an existing table
Table<Row> table = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter = Filters.and(
Filters.eq("isCheckedOut", false),
Filters.lt("numberOfPages", 300));
TableFindOptions options = new TableFindOptions()
.sort(Sort.ascending("rating"))
.sort(Sort.descending("title"))
.projection(Projection.include("isCheckedOut", "title"));
TableCursor<Row, Row> cursor = table.find(filter, options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"find": {
"filter": {"$and": [
{"isCheckedOut": false},
{"numberOfPages": {"$lt": 300}}
]},
"sort": {
"rating": 1,
"title": -1
},
"projection": {"isCheckedOut": true, "title": true}
}
}'
Iterate over found rows
-
Python
-
TypeScript
-
Java
-
curl
Use a for
loop to iterate over the cursor. The client will periodically fetch more rows until no matching rows remain.
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find rows
cursor = table.find(
{
"$and": [
{"isCheckedOut": False},
{"numberOfPages": {"$lt": 300}},
]
})
# Iterate over the found rows
for row in cursor:
print(row)
The cursor returned by find()
is compatible with for
loops and next()
. The client will periodically fetch more documents until no matching documents remain.
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing table
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const table = database.table('TABLE_NAME');
(async function () {
// Find rows
const cursor = table.find({
$and: [{ isCheckedOut: false }, { numberOfPages: { $lt: 300 } }],
});
// Get the next item in the cursor
console.log(await cursor.next());
// Iterate over the found rows
for await (const row of cursor) {
console.log(row);
}
})();
The cursor returned by find()
is an Iterable
and is compatible with for
loops. The client will periodically fetch more documents until no matching documents remain.
package com.example;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.definition.rows.Row;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
import com.datastax.astra.client.tables.cursor.TableCursor;
public class Find {
public static void main(String[] args) {
// Get an existing table
Table<Row> table = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter = Filters.and(
Filters.eq("isCheckedOut", false),
Filters.lt("numberOfPages", 300));
TableFindOptions options = new TableFindOptions();
TableCursor<Row, Row> cursor = table.find(filter, options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
If the response includes a non-null nextPageState
, then the specified sort
or filter
operation supports pagination, and more documents than the ones already returned exist.
To fetch additional documents, you must send a request with the nextPageState
value from your previous request. For example:
-
Send an initial request
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \ --header "Token: ASTRA_DB_APPLICATION_TOKEN" \ --header "Content-Type: application/json" \ --data '{ "find": { "filter": {"isCheckedOut": false} } }'
-
Get the
data.documents.nextPageState
value from the response{ "data": { "documents": [ { "_id": { "$uuid": "018e65c9-df45-7913-89f8-175f28bd7f74" } }, { "_id": { "$uuid": "018e65c9-e33d-749b-9386-e848739582f0" } } ], "nextPageState": "NEXT_PAGE_STATE" } }
-
Use the
data.documents.nextPageState
from the previous response to request the next page of results.curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \ --header "Token: ASTRA_DB_APPLICATION_TOKEN" \ --header "Content-Type: application/json" \ --data '{ "find": { "filter": {"isCheckedOut": false}, "options": { "pageState": "NEXT_PAGE_STATE_FROM_PRIOR_RESPONSE" } } }'
-
Once
nextPageState
isnull
, you have fetched all matching documents.{ "data": { "documents": [ { "_id": { "$uuid": "018e65c9-df45-7913-89f8-175f28bd7f74" } }, { "_id": { "$uuid": "018e65c9-e33d-749b-9386-e848739582f0" } } ], "nextPageState": null } }
Client reference
-
Python
-
TypeScript
-
Java
-
curl
For more information, see the client reference.
For more information, see the client reference.
For more information, see the client reference.
Client reference documentation is not applicable for HTTP.