Filter operators for tables (Java)
Many Data API commands, like Find rows (Java), accept a filter parameter to determine which rows to return or update. The filter parameter uses one or more filter operators.
You can use filter operators on all supported types except vector.
For information about vector search on tables, see Sort clauses for tables (Java).
Operators
You can use the following operators in a filter. All operators are case-sensitive.
Equal (default)
The $eq operator matches rows where the specified column’s value is equal to the specified value.
You can’t use this operator on a column that is associated with a text index.
This is the default when you don’t specify an operator and the column has a regular index or is not indexed.
$eq is not supported for map, list, or set columns.
import com.datastax.astra.client.DataAPIClients;
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.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find a row
Filter filter = Filters.eq("number_of_pages", 714);
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
Not equal
The $ne operator matches rows where the specified column’s value is not equal to the specified value.
You can’t use this operator on a column that is associated with a text index.
$ne is not supported for map, list, or set columns.
import com.datastax.astra.client.DataAPIClients;
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.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find a row
Filter filter = Filters.ne("number_of_pages", 300);
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
Greater than
The $gt operator matches rows where the specified column’s value is greater than the specified value.
You can’t use this operator on a column that is associated with a text index.
import com.datastax.astra.client.DataAPIClients;
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.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find a row
Filter filter = Filters.gt("number_of_pages", 300);
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
Greater than or equal
The $gte operator matches rows where the specified column’s value is greater than or equal to the specified value.
You can’t use this operator on a column that is associated with a text index.
import com.datastax.astra.client.DataAPIClients;
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.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find a row
Filter filter = Filters.gte("number_of_pages", 300);
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
Less than
The $lt operator matches rows where the specified column’s value is less than the specified value.
You can’t use this operator on a column that is associated with a text index.
import com.datastax.astra.client.DataAPIClients;
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.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find a row
Filter filter = Filters.lt("number_of_pages", 300);
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
Less than or equal
The $lte operator matches rows where the specified column’s value is less than or equal to the specified value.
You can’t use this operator on a column that is associated with a text index.
import com.datastax.astra.client.DataAPIClients;
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.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find a row
Filter filter = Filters.lte("number_of_pages", 300);
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
In
The behavior of $in depends on the type of column it is applied to:
-
List and set columns: The
$inoperator matches rows where the filtered column contains any of the specified values. -
Map columns: The
$inoperator matches rows where the filtered column contains any of the specified key-value pairs. Each key-value pair is specified as an array. -
Other column types: The
$inoperator matches rows where the filtered column has a value equal to any of the specified values.
For a non-map column, specify the values as an array:
You can’t use this operator on a column that is associated with a text index.
import com.datastax.astra.client.DataAPIClients;
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.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find a row
Filter filter = Filters.in("author", "Sara Jones", "Jennifer Ray");
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
For map columns, specify the values as an array of key-value pairs, where each key-value pair is an array:
import com.datastax.astra.client.DataAPIClients;
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.definition.rows.Row;
import java.util.List;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find a row
Filter filter =
Filters.in(
"metadata", List.of("language", "French"), List.of("edition", "Illustrated Edition"));
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
Not in
The behavior of $nin depends on the type of column it is applied to:
-
List and set columns: The
$ninoperator matches rows where the column doesn’t contain any of the specified values. -
Map columns: The
$ninoperator matches rows where the column doesn’t contain any of the specified key-value pairs. Each key-value pair is specified as an array. -
Other column types: The
$ninoperator matches rows that don’t contain any of the specified values.
For a non-map column, specify the values as an array:
You can’t use this operator on a column that is associated with a text index.
import com.datastax.astra.client.DataAPIClients;
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.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find a row
Filter filter = Filters.nin("author", "Sara Jones", "Jennifer Ray");
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
For map columns, specify the values as an array of key-value pairs, where each key-value pair is an array:
import com.datastax.astra.client.DataAPIClients;
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.definition.rows.Row;
import java.util.List;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find a row
Filter filter =
Filters.nin(
"metadata", List.of("language", "French"), List.of("edition", "Illustrated Edition"));
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
All
The behavior of $all depends on the type of column it is applied to:
-
List and set columns: The
$alloperator matches rows where the column contains all of the specified values. -
Map columns: The
$alloperator matches rows where the column contains all of the specified key-value pairs. Each key-value pair is specified as an array.
The $all operator is only supported for map, list, and set columns.
For a list or set column, specify the values as an array:
import com.datastax.astra.client.DataAPIClients;
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.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find a row
Filter filter = Filters.all("genres", "Fantasy", "Romance");
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
For map columns, specify the values as an array of key-value pairs, where each key-value pair is an array:
import com.datastax.astra.client.DataAPIClients;
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.definition.rows.Row;
import java.util.List;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find a row
Filter filter =
Filters.all(
"metadata", List.of("language", "Italian"), List.of("edition", "Illustrated Edition"));
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
Keys
Only supported for map columns.
In conjunction with $in, the $keys operator matches rows where the map column contains any of the specified keys.
In conjunction with $all, the $keys operator matches rows where the map column contains all of the specified keys.
In conjunction with $nin, the $keys operator matches rows where the map column doesn’t contain any of the specified keys.
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find a row
Filter filter =
new Filter(
Map.of(
"metadata", Map.of("$keys", Map.of("$in", Arrays.asList("language", "edition")))));
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
Values
Only supported for map columns.
In conjunction with $in, the $values operator matches rows where the map column contains any of the specified values.
In conjunction with $all, the $values operator matches rows where the map column contains all of the specified values.
In conjunction with $nin, the $values operator matches rows where the map column doesn’t contain any of the specified values.
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find a row
Filter filter =
new Filter(
Map.of(
"metadata",
Map.of("$values", Map.of("$in", Arrays.asList("French", "Illustrated Edition")))));
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
Match
|
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. |
The $match operator matches rows where the specified column’s value is a lexicographical match to the specified string of space-separated keywords or terms.
You can only use the $match operator on a column that is associated with a text index.
This is the only filter operator that you can use on columns associated with a text index.
import com.datastax.astra.client.DataAPIClients;
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.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find a row
Filter filter = Filters.match("summary", "futuristic laboratory discovery");
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
Combine operators (And, Or)
The $and operator joins clauses with a logical AND.
Only rows that match the conditions of all clauses are returned.
The $or operator joins clauses with a logical OR.
Rows that match the condition of any of the clauses are returned.
You can use $and and $or separately or together.
If you want to match multiple flexible conditions, use $and and $or together in the same filter:
import com.datastax.astra.client.DataAPIClients;
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.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find a row
Filter filter =
Filters.and(
Filters.or(Filters.eq("is_checked_out", false), Filters.lt("number_of_pages", 300)),
Filters.or(Filters.lt("rating", 4.3), Filters.gte("publication_year", 2002)));
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
To filter on a range of values, use $and with $lt/$lte and $gt/$gte:
import com.datastax.astra.client.DataAPIClients;
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.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find a row
Filter filter =
Filters.and(Filters.lt("number_of_pages", 300), Filters.gte("number_of_pages", 200));
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}