Skip to main content
Curation queries are in early access, as part of the Curation beta, and may change before general availability. They require version 0.2.4 or later of the encord SDK, and are imported from encord.beta.curation_query.
A Curation query describes a set of files in Storage. You can use a query to:
  • List the matching files with user_client.query_storage_items().
  • Count the matching files with user_client.count_storage_items().
  • Link the matching files to a Dataset with dataset.link_query_items().
A query does nothing on its own. It is an input to these methods.

Build a Query

A CurationQuery has three parts: Create each filter condition from F. Conditions on the same field combine, so gte and lte together give a range.
  • F.metadata, F.metric, and every F.frame condition require premium access to Index.
  • Quality metrics and advanced quality metrics are computed only when the scope’s top-level folder is upgraded in Index to compute them.
  • A file matches the F.frame conditions when one of its frames meets all of them. An image’s only frame is the image itself. Files without frames, such as audio files, never match F.frame conditions.
  • Lists passed to is_in and not_in must contain between 1 and 1,000 values.
  • To filter on a metadata key that is not in your schema, add it to the schema first. See Metadata Schema.
Build a query

Save and Load a Query

query.to_dict() converts a query to a JSON-compatible dictionary, and CurationQuery.from_dict() converts it back. Use them to keep queries in configuration files. Loading a query with a key the SDK does not recognize raises an error, so a saved query never matches more files than it did when you saved it.
Save and load a query

Rank Files by Similarity

Set rank_by to match only the files most similar to a description or to other files. Ranking uses the files’ embeddings, so it requires premium access to Index, and the scope’s top-level folder must be upgraded in Index to compute embeddings. Files without embeddings are left out.
  • RankBy.similar_to_text(text, top_k=100) ranks files by similarity to a description, such as "a dog on a beach", of up to 75 tokens. Longer descriptions raise InvalidArgumentsError when you search, count, or link.
  • RankBy.similar_to_files(files, top_k=100) ranks files by similarity to 1 to 100 files, given as StorageItem objects or UUIDs. The files you pass are left out of the results. Files without embeddings are ignored, so if none of them has an embedding, the query matches no files.
top_k is the number of files the query matches, from 1 to 1,000. You can combine rank_by with filters, to rank only the files that meet the filters.
Rank files by similarity

List and Count Matching Files

user_client.query_storage_items() returns the matching files as StorageItem objects, and fetches them a page at a time as you iterate. Files are returned most similar first for queries with rank_by.
  • By default, each file’s client_metadata is None. Pass include_client_metadata=True to fill it in. This makes the search slower.
  • Pass get_signed_urls=True to fill in each file’s signed URL, which you read with item.get_signed_url().
  • Pass page_size to set how many files each page holds, from 1 to 1,000. The default is 100. Queries with rank_by return all of their top_k files in one page, and do not accept page_size.
user_client.count_storage_items() returns the number of matching files. The count can be up to five minutes old. Recently deleted files are left out of searches, so a search can return fewer files than the count for the same query.
List and count matching files
dataset.link_query_items() links every file that matches a query to a Dataset, and waits for the linking job to end. Linking creates tasks in every Project attached to the Dataset.
  • duplicates_behavior decides what happens to files that are already in the Dataset: DataLinkDuplicatesBehavior.SKIP (the default) leaves them out, and DataLinkDuplicatesBehavior.DUPLICATE links them again. FAIL is not supported and raises ValueError.
  • A query can match up to 25,000 files. Larger queries raise InvalidArgumentsError. A query with rank_by that matches no files also raises InvalidArgumentsError.
  • If the job fails, link_query_items() raises QueryItemsJobFailedError. Its job attribute holds the job as it ended. Files linked before the failure stay linked, so running the same query again with SKIP links the rest.
  • If the job is still running after timeout_seconds (one day by default), it is returned with the state PENDING.
The method returns a QueryItemsJob. Its state is a QueryItemsJobState value: PENDING, SUCCEEDED, FAILED, or CANCELLED. Its counts are in files: processed and total show progress, applied is the number of files linked, and failed is the number of files that could not be linked. failures lists the first 100 files that could not be linked, with a reason for each.
Link matching files to a Dataset
To start a linking job and check on it later, use link_query_items_start() and link_query_items_get_result(). link_query_items_start() takes the same arguments as link_query_items(), except timeout_seconds, and returns the job’s ID. The job keeps running if your script stops. link_query_items_get_result() waits up to timeout_seconds for the job to end, and returns as soon as it does. Pass timeout_seconds=0 to check the job once without waiting. Unlike link_query_items(), it does not raise if the job failed, so check the job’s state.
Start a link job and check it later