Find a row (HTTP)
|
For details about additional languages, see: |
|
Tables with the Data API are 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. |
Finds a single row 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 (HTTP).
|
Ready to write code? See the examples for this method to get started. |
Result
Returns a dictionary representation of a row that matches the specified filter and sort clauses, or returns null if no row was found.
The columns included in the returned row depend on the subset of columns that were requested in the projection.
If requested and applicable, the 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.
The response omits any columns with null values, regardless of the projection.
Example response:
{
"data": {
"document": {
"_id": 101,
"name": "John Doe",
"$vector": [0.12, 0.52, 0.32]
}
}
}
Example null response:
{
"data": {
"document": null
}
}
Signature
Use the findOne command.
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOne": {
"filter": FILTER,
"sort": SORT,
"projection": PROJECTION,
"options": {
"includeSimilarity": BOOLEAN
}
}
}'
Parameters
|
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 (HTTP). 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 a row. |
|
|
Optional. Sorts rows by one or more columns, or performs a vector search. For more information, see Sort clauses for tables (HTTP). For examples, see Use sorting to find a row and Use vector search with a search vector to find a row. |
|
|
Optional. Controls which columns are included or excluded in the returned rows. For more information, see Projections for tables (HTTP). 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 only applies if you use a vector search. If you use a projection and you set the Default: false For an example, see Include the similarity score with the result. |
Examples
The following examples demonstrate how to find a row in a table.
Use filters to find a row
You can use a filter to find a row that matches specific criteria.
For example, you can find a row with an is_checked_out value of false and a number_of_pages value less than 300.
For optimal performance, 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 (HTTP).
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOne": {
"filter": {"$and": [
{"is_checked_out": false},
{"number_of_pages": {"$lt": 300}}
]}
}
}'
Use vector search with a search vector to find a row
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 provide the search vector as an array of floats, or you can use $binary to provide the search vector as a Base64-encoded string.
$binary can be more performant.
For more information about how to convert an array of floats to a Base64-encoded string, see BLOB type (HTTP).
- Array of floats
-
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \ --header "Token: APPLICATION_TOKEN" \ --header "Content-Type: application/json" \ --data '{ "findOne": { "sort": { "summary_genres_vector": [0.08, -0.62, 0.39] } } }' - $binary
-
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \ --header "Token: APPLICATION_TOKEN" \ --header "Content-Type: application/json" \ --data '{ "findOne": { "sort": { "summary_genres_vector": {"$binary": "PaPXCr8euFI+x64U"} } } }'
Use sorting to find a row
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 (HTTP).
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOne": {
"filter": { "is_checked_out": false },
"sort": {
"rating": 1,
"title": -1
}
}
}'
Include the similarity score with the result
If you use a vector search to find a row, 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.
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOne": {
"sort": { "summary_genres_vector": [0.08, -0.62, 0.39] },
"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.
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOne": {
"filter": {"number_of_pages": {"$lt": 300}},
"projection": {"is_checked_out": 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.
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOne": {
"filter": {"number_of_pages": {"$lt": 300}},
"projection": {"is_checked_out": false, "title": false}
}
}'
Use filter, sort, and projection together
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOne": {
"filter": {"$and": [
{"is_checked_out": false},
{"number_of_pages": {"$lt": 300}}
]},
"sort": {
"rating": 1,
"title": -1
},
"projection": {"is_checked_out": true, "title": true}
}
}'