Filters
Advanced filtering for precise results
Use filters to refine your queries and get more precise, targeted results from the BrainUs API.
Available Filters
The Query API supports filtering by document metadata. All filters are optional and use exact matching.
Filter Fields
| Field | Type | Description | Example |
|---|---|---|---|
subject | string | Subject name | "ict" |
grade | string | Grade level | "11" |
category | string | Document category (past-paper, textbook, etc.) | "textbook" |
language | string | Content language (english, sinhala, tamil) | "english" |
year | string | Academic year (mainly for past papers) | "2024" |
Filtering Examples
Filter by Grade
Target specific grade levels:
curl -X POST https://api.brainus.lk/api/v1/dev/query \
-H "X-API-Key: $BRAINUS_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"query": "Explain quadratic equations",
"store_id": "default",
"filters": {
"grade": "11"
}
}'# Using Python with actual API
import requests
response = requests.post(
"https://api.brainus.lk/api/v1/dev/query",
headers={"X-API-Key": "your_api_key"},
json={
"query": "Explain quadratic equations",
"store_id": "default",
"filters": {
"grade": "11"
}
}
)const response = await fetch('https://api.brainus.lk/api/v1/dev/query', {
method: 'POST',
headers: {
'X-API-Key': 'your_api_key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
query: 'Explain quadratic equations',
store_id: 'default',
filters: {
grade: '11'
}
})
});Filter by Subject
Get subject-specific results:
{
"query": "Explain Newton's laws",
"store_id": "default",
"filters": {
"subject": "physics"
}
}Filter by Category
Target specific document types:
{
"query": "Solve past paper questions",
"store_id": "default",
"filters": {
"category": "past-paper",
"year": "2024"
}
}Valid categories:
past-paper- Past exam papersmodel-paper- Model exam paperstextbook- Textbook contenttutorial- Tutorial materialsshort-notes- Quick reference notestutes-by-teachers- Teacher-created materialsreference- Reference materials
Filter by Language
Request results in a specific language:
{
"query": "පයිතන් ප්රොග්රැමිং කුමක්ද?",
"store_id": "default",
"filters": {
"language": "sinhala"
}
}Valid languages:
english- English contentsinhala- Sinhala (සිංහල) contenttamil- Tamil (தமிழ்) content
Combine Multiple Filters
Apply multiple filters for highly targeted results:
{
"query": "DNA replication",
"store_id": "default",
"filters": {
"grade": "11",
"subject": "biology",
"language": "english",
"category": "textbook"
}
}Combining filters helps reduce irrelevant results and improves response accuracy. All filters use exact matching.
Notes
- Optional filters: All filters are optional. Use them to narrow results
- Exact matching: Filters match exact values
- Multiple filters: When combining filters, ALL filters must match (AND logic)
- Case sensitive: Filter values are case-sensitive
Next Steps
- Basic Usage - Get started with simple queries
- Best Practices - Optimization patterns
Complete Example
Here's a comprehensive example using all filters and options:
import os
import asyncio
from brainus_ai import BrainusAI
async def main():
async with BrainusAI() as client:
response = await client.query(
query="Explain the water cycle",
store_id="default",
filters={
"grade": "9", # Single string, not array
"subject": "science", # Single string, not array
"language": "english"
}
)
print(f"Answer: {response.answer}\n")
if response.citations:
print(f"Found {len(response.citations)} relevant sources:")
for i, citation in enumerate(response.citations, 1):
# Accessing metadata dict if available
grade = citation.metadata.get('grade', 'N/A') if citation.metadata else 'N/A'
subject = citation.metadata.get('subject', 'N/A') if citation.metadata else 'N/A'
print(f"\n{i}. {citation.document_name}")
if citation.pages:
print(f" Pages: {', '.join(map(str, citation.pages))}")
print(f" Grade: {grade}")
print(f" Subject: {subject}")
if citation.chunk_text:
print(f" Snippet: {citation.chunk_text[:100]}...")
if __name__ == "__main__":
asyncio.run(main())import { BrainusAI } from '@brainus/ai';
const client = new BrainusAI(); // reads BRAINUS_API_KEY from environment
async function main() {
const response = await client.query({
query: 'Explain the water cycle',
storeId: 'default',
filters: {
grade: '9',
subject: 'Science',
language: 'english'
}
});
console.log(`Answer: ${response.answer}\n`);
if (response.hasCitations) {
console.log(`Found ${response.citations.length} relevant sources:`);
response.citations.forEach((citation, i) => {
const grade = citation.metadata?.grade || 'N/A';
const subject = citation.metadata?.subject || 'N/A';
console.log(`\n${i + 1}. ${citation.documentName}`);
if (citation.pages.length > 0) {
console.log(` Pages: ${citation.pages.join(", ")}`);
}
console.log(` Grade: ${grade}`);
console.log(` Subject: ${subject}`);
if (citation.chunkText) {
console.log(` Snippet: ${citation.chunkText.substring(0, 100)}...`);
}
});
}
}
main();Tips for Effective Filtering
Start Broad, Then Narrow
Begin with fewer filters and add more if needed:
# First attempt - broad
response = await client.query("Cell biology", store_id="default")
# If too many results - narrow down
response = await client.query(
"Cell biology",
store_id="default",
filters={"grade": "11", "subject": "Biology"}
)Each filter field accepts a single string value. Use the most specific filter that matches your use case.
Next Steps
- Best Practices - Optimization and performance tips
- Code Examples - Real-world usage patterns
- Rate Limits - Understanding quotas