Honcho provides a sophisticated filtering system that allows you to query workspaces, peers, sessions, and messages with precise control. The filtering system supports logical operators, comparison operators, metadata filtering, and wildcards to help you find exactly what you need.
Basic Filtering Concepts
Filters in Honcho are expressed as dictionaries that define conditions for matching resources. The system supports both simple equality filters and complex queries with multiple conditions.
Simple Filters
The most basic filters check for exact matches:
from honcho import Honcho
# Initialize client
honcho = Honcho()
# Simple peer filter
peers = honcho.get_peers(filter={"peer_id": "alice"})
# Simple session filter with metadata
sessions = honcho.get_sessions(filter={
"metadata": {"type": "support"}
})
# Simple message filter
messages = honcho.get_messages(filter={
"session_id": "support-chat-1",
"peer_id": "alice"
})
Logical Operators
Combine multiple conditions using logical operators for complex queries:
AND Operator
Use AND to require all conditions to be true:
messages = honcho.get_messages(filter={
"AND": [
{"session_id": "chat-1"},
{"created_at": {"gte": "2024-01-01"}}
]
})
OR Operator
Use OR to match any of the specified conditions:
# Find messages from either alice or bob
messages = session.get_messages(filter={
"OR": [
{"peer_id": "alice"},
{"peer_id": "bob"}
]
})
# Complex OR with metadata conditions
sessions = honcho.get_sessions(filter={
"OR": [
{"metadata": {"priority": "high"}},
{"metadata": {"urgent": True}},
{"metadata": {"escalated": True}}
]
})
NOT Operator
Use NOT to exclude specific conditions:
# Find all peers except alice
peers = honcho.get_peers(filter={
"NOT": [
{"peer_id": "alice"}
]
})
# Find sessions that are NOT completed
sessions = honcho.get_sessions(filter={
"NOT": [
{"metadata": {"status": "completed"}}
]
})
Combining Logical Operators
Create sophisticated queries by combining different logical operators:
# Find messages from alice OR bob, but NOT where message has archived set to true in metadata
messages = session.get_messages(filter={
"AND": [
{
"OR": [
{"peer_id": "alice"},
{"peer_id": "bob"}
]
},
{
"NOT": [
{"metadata": {"archived": True}}
]
}
]
})
Comparison Operators
Use comparison operators for range queries and advanced matching:
Numeric Comparisons
# Find sessions created after a specific date
sessions = honcho.get_sessions(filter={
"created_at": {"gte": "2024-01-01"}
})
# Find messages within a date range
messages = session.get_messages(filter={
"created_at": {
"gte": "2024-01-01",
"lte": "2024-12-31"
}
})
# Metadata numeric comparisons
sessions = honcho.get_sessions(filter={
"metadata": {
"score": {"gt": 8.5},
"duration": {"lte": 3600}
}
})
List Membership
# Find messages from specific peers in a session
messages = session.get_messages(filter={
"peer_id": {"in": ["alice", "bob", "charlie"]}
})
# Find sessions with specific tags
sessions = honcho.get_sessions(filter={
"metadata": {
"tag": {"in": ["important", "urgent", "follow-up"]}
}
})
# Not equal comparisons
peers = honcho.get_peers(filter={
"metadata": {
"status": {"ne": "inactive"}
}
})
Metadata filtering is particularly powerful in Honcho, supporting nested conditions and complex queries:
# Simple metadata equality
sessions = honcho.get_sessions(filter={
"metadata": {
"type": "customer_support",
"priority": "high"
}
})
# Nested metadata objects
peers = honcho.get_peers(filter={
"metadata": {
"profile": {
"role": "admin",
"department": "engineering"
}
}
})
If you want to do advanced queries like these, make sure not to create metadata fields that use the same names as the included comparison operators! For example, if you have a metadata field called contains
, it will conflict with the contains
operator.
# Metadata with comparison operators
sessions = honcho.get_sessions(filter={
"metadata": {
"score": {"gte": 4.0, "lte": 5.0},
"created_by": {"ne": "system"},
"tags": {"contains": "important"}
}
})
# Complex metadata conditions
messages = session.get_messages(filter={
"AND": [
{"metadata": {"sentiment": {"in": ["positive", "neutral"]}}},
{"metadata": {"confidence": {"gt": 0.8}}},
{"content": {"icontains": "thank"}}
]
})
Wildcards
Use wildcards (*) to match any value for a field:
# Find all sessions with any peer_id (essentially all sessions)
sessions = honcho.get_sessions(filter={
"peer_id": "*"
})
# Wildcard in lists - matches everything
messages = session.get_messages(filter={
"peer_id": {"in": ["alice", "bob", "*"]}
})
# Metadata wildcards
sessions = honcho.get_sessions(filter={
"metadata": {
"type": "*", # Any type
"status": "active" # But status must be active
}
})
Resource-Specific Examples
Filtering Workspaces
# Find workspaces by name pattern
workspaces = honcho.get_workspaces(filter={
"name": {"contains": "prod"}
})
# Filter by metadata
workspaces = honcho.get_workspaces(filter={
"metadata": {
"environment": "production",
"team": {"in": ["backend", "frontend", "devops"]}
}
})
Filtering Messages
# Find error messages from the last week
from datetime import datetime, timedelta
week_ago = (datetime.now() - timedelta(days=7)).isoformat()
messages = session.get_messages(filter={
"AND": [
{"content": {"icontains": "error"}},
{"created_at": {"gte": week_ago}},
{"metadata": {"level": {"in": ["error", "critical"]}}}
]
})
# Find messages in specific sessions with sentiment analysis
messages = session.get_messages(filter={
"AND": [
{"session_id": {"in": ["support-1", "support-2", "support-3"]}},
{"metadata": {"sentiment": "negative"}},
{"metadata": {"confidence": {"gte": 0.7}}}
]
})
Error Handling
Handle filter errors gracefully:
from honcho.exceptions import FilterError
try:
# Invalid filter - unsupported operator
messages = session.get_messages(filter={
"created_at": {"invalid_operator": "2024-01-01"}
})
except FilterError as e:
print(f"Filter error: {e}")
# Handle the error appropriately
try:
# Invalid column name
sessions = honcho.get_sessions(filter={
"nonexistent_field": "value"
})
except FilterError as e:
print(f"Invalid field: {e}")
Conclusion
Honcho’s filtering system provides powerful capabilities for querying your conversational data. By understanding how to:
- Use simple equality filters and complex logical operators
- Apply comparison operators for range and pattern matching
- Filter metadata with nested conditions
- Handle wildcards and dynamic filter construction
- Follow best practices for performance and validation
You can build sophisticated applications that efficiently find and process exactly the conversations, messages, and insights you need from your Honcho data.