Find rows (Java)
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 (Java).
|
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 (TableFindCursor<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.
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 com.datastax.astra.client.tables.Table class.
Method signature
TableFindCursor<T, T> find(
Filter filter,
TableFindOptions options
)
TableFindCursor<T, T> find(
Filter filter
)
TableFindCursor<T, T> find(
TableFindOptions options
)
<R> TableFindCursor<T, R> find(
Filter filter,
TableFindOptions options,
Class<R> rowClass
)
|
For best performance, filter and sort on indexed columns, partition keys, and clustering keys. Filtering on non-indexed columns is inefficient and resource-intensive, especially for large datasets. With the Data API clients, such operations can hit the client timeout limit before the underlying HTTP operation is complete. If you filter on non-indexed columns, the Data API will give a warning. An empty filter or omitted filter may also result in an inefficient and long-running operation. 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 |
|---|---|---|
|
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 (Java). 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 specification of the class of the returned row object. This parameter is useful if your code is strictly typed and you use a projection. Default: The same class as the rows in the table.
If you didn’t specify this when you instantiated the |
|
Optional.
The options for this operation. See Methods of the |
| Method | Parameters | Summary |
|---|---|---|
|
Optional. Sorts rows by one or more columns, or performs a vector search. For more information, see Sort clauses for tables (Java). |
|
|
Optional. Controls which columns are included or excluded in the returned rows. For more information, see Projections for tables (Java). 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.
Whether to include a This parameter doesn’t work with vectorize; it only works if you provide the search vector for vector search directly. Default: false For an example, see Include the similarity score with the result. |
|
|
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.
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 timeout(s) to apply to HTTP request(s) originating from this method. Default: The default value for the table. This default is 30 seconds unless you specified a different default when you initialized the |
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 (Java).
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.Table;
import com.datastax.astra.client.tables.cursor.TableFindCursor;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter =
Filters.and(Filters.eq("is_checked_out", false), Filters.lt("number_of_pages", 300));
TableFindCursor<Row, Row> cursor = table.find(filter);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
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 DataAPIVector class to binary-encode your search vector.
DataStax recommends that you always use a DataAPIVector object instead of a list of floats to improve performance.
When you read the value of a vector column, the client always returns a DataAPIVector object, unless you change the default serialization/deserialization behavior.
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.vector.DataAPIVector;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
import com.datastax.astra.client.tables.cursor.TableFindCursor;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
TableFindOptions options =
new TableFindOptions()
.sort(
Sort.vector(
"summary_genres_vector",
new DataAPIVector(new float[] {0.08f, -0.62f, 0.39f})));
TableFindCursor<Row, Row> cursor = table.find(options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
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 an embedding provider integration. You can configure embedding provider integrations when you create a table, add a vector column to an existing table, or alter an existing vector column. The vector column must be indexed.
If your table has multiple vector columns, you can only sort on one vector column at a time.
import com.datastax.astra.client.DataAPIClient;
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.TableFindCursor;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
TableFindOptions options =
new TableFindOptions().sort(Sort.vectorize("summary_genres_vector", "Text to vectorize"));
TableFindCursor<Row, Row> cursor = table.find(options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
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. Astra DB Serverless, 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 (Java) and Indexes in tables (Java).
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.Sort;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
import com.datastax.astra.client.tables.cursor.TableFindCursor;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter = Filters.match("summary", "futuristic laboratory discovery");
TableFindOptions options =
new TableFindOptions().sort(Sort.lexical("summary", "futuristic laboratory"));
TableFindCursor<Row, Row> cursor = table.find(filter, options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
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 (Java).
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.Sort;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
import com.datastax.astra.client.tables.cursor.TableFindCursor;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter = Filters.eq("is_checked_out", false);
TableFindOptions options =
new TableFindOptions().sort(Sort.ascending("rating"), Sort.descending("title"));
TableFindCursor<Row, Row> cursor = table.find(filter, options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
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.
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.cursor.TableFindCursor;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter = new Filter();
TableFindCursor<Row, Row> cursor = table.find(filter);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
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 DataAPIVector object, unless you change the default serialization/deserialization behavior.
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.vector.DataAPIVector;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
import com.datastax.astra.client.tables.cursor.TableFindCursor;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
TableFindOptions options =
new TableFindOptions()
.sort(
Sort.vector(
"summary_genres_vector", new DataAPIVector(new float[] {0.08f, -0.62f, 0.39f})))
.includeSimilarity(true);
TableFindCursor<Row, Row> cursor = table.find(options);
// Iterate over the found rows
for (Row row : cursor) {
Double similarity = row.getDouble("$similarity");
System.out.println(similarity);
}
}
}
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.
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.Table;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
import com.datastax.astra.client.tables.cursor.TableFindCursor;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter = Filters.lt("number_of_pages", 300);
TableFindOptions options =
new TableFindOptions().projection(Projection.include("is_checked_out", "title"));
TableFindCursor<Row, Row> cursor = table.find(filter, options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
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.
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.Table;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
import com.datastax.astra.client.tables.cursor.TableFindCursor;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter = Filters.lt("number_of_pages", 300);
TableFindOptions options =
new TableFindOptions().projection(Projection.exclude("is_checked_out", "title"));
TableFindCursor<Row, Row> cursor = table.find(filter, options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
Limit the number of rows returned
Specify a limit to only fetch up to a certain number of rows.
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.Table;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
import com.datastax.astra.client.tables.cursor.TableFindCursor;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter =
Filters.and(Filters.eq("is_checked_out", false), Filters.lt("number_of_pages", 300));
TableFindOptions options = new TableFindOptions().limit(3);
TableFindCursor<Row, Row> cursor = table.find(filter, options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
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.
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.Sort;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
import com.datastax.astra.client.tables.cursor.TableFindCursor;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter = Filters.eq("is_checked_out", false);
TableFindOptions options =
new TableFindOptions().sort(Sort.ascending("rating"), Sort.descending("title")).skip(5);
TableFindCursor<Row, Row> cursor = table.find(filter, options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
Use filter, sort, and projection together
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.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.TableFindCursor;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter =
Filters.and(Filters.eq("is_checked_out", false), Filters.lt("number_of_pages", 300));
TableFindOptions options =
new TableFindOptions()
.sort(Sort.ascending("rating"), Sort.descending("title"))
.projection(Projection.include("is_checked_out", "title"));
TableFindCursor<Row, Row> cursor = table.find(filter, options);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
Iterate over found rows
The cursor returned by find() is an Iterable and is compatible with for loops. The client will periodically fetch more rows until no matching rows remain.
Alternatively, you can use the findPage 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 toList().
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:
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.Table;
import com.datastax.astra.client.tables.cursor.TableFindCursor;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find rows
Filter filter =
Filters.and(Filters.eq("is_checked_out", false), Filters.lt("number_of_pages", 300));
TableFindCursor<Row, Row> cursor = table.find(filter);
// Iterate over the found rows
for (Row row : cursor) {
System.out.println(row);
}
}
}
Example using findPage:
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.paging.Page;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Create the filter
Filter filter =
Filters.and(Filters.eq("is_checked_out", false), Filters.lt("number_of_pages", 300));
// Get the first page
Page<Row> page1 = table.findPage(filter, null);
page1.getResults().forEach(System.out::println);
String paginationState1 = page1.getPageState().orElse(null);
// Get the next page
if (paginationState1 != null) {
Page<Row> page2 = table.findPage(filter, new TableFindOptions().pageState(paginationState1));
page2.getResults().forEach(System.out::println);
}
}
}
Client reference
For more information, see the client reference.