Query and filter records¶
This guide provides an overview of how to query and filter a dataset in Argilla.
You can search for records in your dataset by querying or filtering. The query focuses on the content of the text field, while the filter is used to filter the records based on conditions. You can use them independently or combine multiple filters to create complex search queries. You can also export records from a dataset either as a single dictionary or a list of dictionaries.
Main Classes
Check the Query - Python Reference to see the attributes, arguments, and methods of the
Query
class in detail.
Check the Filter - Python Reference to see the attributes, arguments, and methods of the
Filter
class in detail.
Query with search terms¶
To search for records with terms, you can use the Dataset.records
attribute with a query string. The search terms are used to search for records that contain the terms in the text field. You can search a single term or various terms, in the latter, all of them should appear in the record to be retrieved.
Filter by conditions¶
You can use the Filter
class to define the conditions and pass them to the Dataset.records
attribute to fetch records based on the conditions. Conditions include "==", ">=", "<=", or "in". Conditions can be combined with dot notation to filter records based on metadata, suggestions, or responses. You can use a single condition or multiple conditions to filter records.
operator | description |
---|---|
== |
The field value is equal to the value |
>= |
The field value is greater than or equal to the value |
<= |
The field value is less than or equal to the value |
in |
TThe field value is included in a list of values |
import argilla as rg
client = rg.Argilla(api_url="<api_url>", api_key="<api_key>")
dataset = client.datasets(name="my_dataset", workspace="my_workspace")
filter_label = rg.Filter(("label", "==", "positive"))
filtered_records = dataset.records(query=rg.Query(filter=filter_label)).to_list(
flatten=True
)
import argilla as rg
client = rg.Argilla(api_url="<api_url>", api_key="<api_key>")
dataset = client.datasets(name="my_dataset", workspace="my_workspace")
filters = rg.Filter(
[
("label.suggestion", "==", "positive"),
("metadata.count", ">=", 10),
("metadata.count", "<=", 20),
("label", "in", ["positive", "negative"])
]
)
filtered_records = dataset.records(
query=rg.Query(filter=filters), with_suggestions=True
).to_list(flatten=True)
Filter by status¶
You can filter records based on record or response status. Record status can be pending
or completed
, and response status can be pending
, draft
, submitted
, or discarded
.
import argilla as rg
client = rg.Argilla(api_url="<api_url>", api_key="<api_key>")
dataset = client.datasets(name="my_dataset", workspace="my_workspace")
status_filter = rg.Query(
filter=rg.Filter(
[
("status", "==", "completed"),
("response.status", "==", "discarded")
]
)
)
filtered_records = dataset.records(status_filter).to_list(flatten=True)
Query and filter a dataset¶
As mentioned, you can use a query with a search term and a filter or various filters to create complex search queries.
import argilla as rg
client = rg.Argilla(api_url="<api_url>", api_key="<api_key>")
dataset = client.datasets(name="my_dataset", workspace="my_workspace")
query_filter = rg.Query(
query="my_term",
filter=rg.Filter(
[
("label.suggestion", "==", "positive"),
("metadata.count", ">=", 10),
]
)
)
queried_filtered_records = dataset.records(
query=query_filter,
with_metadata=True,
with_suggestions=True
).to_list(flatten=True)