- 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().
Build a Query
ACurationQuery 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 everyF.framecondition 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.frameconditions 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 matchF.frameconditions. - Lists passed to
is_inandnot_inmust 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
Setrank_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 raiseInvalidArgumentsErrorwhen you search, count, or link.RankBy.similar_to_files(files, top_k=100)ranks files by similarity to 1 to 100 files, given asStorageItemobjects 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_metadataisNone. Passinclude_client_metadata=Trueto fill it in. This makes the search slower. - Pass
get_signed_urls=Trueto fill in each file’s signed URL, which you read withitem.get_signed_url(). - Pass
page_sizeto set how many files each page holds, from 1 to 1,000. The default is 100. Queries withrank_byreturn all of theirtop_kfiles in one page, and do not acceptpage_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
Link Matching Files to a Dataset
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_behaviordecides what happens to files that are already in the Dataset:DataLinkDuplicatesBehavior.SKIP(the default) leaves them out, andDataLinkDuplicatesBehavior.DUPLICATElinks them again.FAILis not supported and raisesValueError.- A query can match up to 25,000 files. Larger queries raise
InvalidArgumentsError. A query withrank_bythat matches no files also raisesInvalidArgumentsError. - If the job fails,
link_query_items()raisesQueryItemsJobFailedError. Itsjobattribute holds the job as it ended. Files linked before the failure stay linked, so running the same query again withSKIPlinks the rest. - If the job is still running after
timeout_seconds(one day by default), it is returned with the statePENDING.
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
Link Files Without Waiting
To start a linking job and check on it later, uselink_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

