Troubleshooting Guide - Module 6: Filters & Sorting¶
This guide helps you diagnose and resolve common issues when working with filters and sorting in Azure AI Search.
Common Filter Issues¶
1. Field Not Filterable Error¶
Error Message:
The field 'fieldName' is not filterable. Only filterable fields can be referenced in $filter expressions.
Cause: The field is not marked as filterable: true in the index schema.
Solution: 1. Update your index schema to mark the field as filterable:
- Rebuild or update your index with the new schema.
Prevention: - Plan filterable fields during index design - Review field attributes before creating the index - Use the index analyzer to verify field configurations
2. Invalid OData Filter Syntax¶
Error Message:
Common Syntax Issues:
Missing Quotes Around String Values¶
Incorrect Operator Usage¶
Unbalanced Parentheses¶
# ❌ Wrong
(category eq 'Electronics' and price gt 100
# ✅ Correct
(category eq 'Electronics' and price gt 100)
Incorrect Date Format¶
Debugging Tips: 1. Test filter components individually 2. Use parentheses to group complex expressions 3. Validate date formats (ISO 8601) 4. Check for proper quote escaping
3. Collection Filter Issues¶
Error Message:
Common Collection Issues:
Missing Lambda Expression¶
Incorrect Collection Syntax¶
Nested Collection Operators¶
# ❌ Wrong (not supported)
tags/any(t: t/any(s: s eq 'value'))
# ✅ Alternative approach
tags/any(t: contains(t, 'value'))
4. Geographic Filter Problems¶
Error Message:
Common Geographic Issues:
Incorrect Coordinate Order¶
# ❌ Wrong (latitude first)
geography'POINT(47.608013 -122.335167)'
# ✅ Correct (longitude first)
geography'POINT(-122.335167 47.608013)'
Invalid Coordinate Ranges¶
# ❌ Wrong (invalid latitude)
geography'POINT(-122.335167 95.0)'
# ✅ Correct (valid ranges: lat -90 to 90, lon -180 to 180)
geography'POINT(-122.335167 47.608013)'
Malformed Polygon¶
# ❌ Wrong (not closed)
geography'POLYGON((-122.5 47.4, -122.1 47.4, -122.1 47.8))'
# ✅ Correct (closed polygon)
geography'POLYGON((-122.5 47.4, -122.1 47.4, -122.1 47.8, -122.5 47.4))'
5. Data Type Mismatch Errors¶
Error Message:
Common Type Issues:
String vs Numeric Comparison¶
Date Format Issues¶
Boolean Value Format¶
Common Sorting Issues¶
1. Field Not Sortable Error¶
Error Message:
The field 'fieldName' is not sortable. Only sortable fields can be referenced in $orderby expressions.
Solution: 1. Update index schema to mark field as sortable:
- Rebuild the index with updated schema.
2. Invalid OrderBy Syntax¶
Common Sorting Syntax Issues:
Missing Sort Direction¶
Incorrect Field Reference¶
Invalid Multi-field Syntax¶
3. Geographic Sorting Issues¶
Error Message:
Common Geographic Sorting Issues:
Missing Geography Literal¶
# ❌ Wrong
$orderby=geo.distance(location, -122.335167 47.608013)
# ✅ Correct
$orderby=geo.distance(location, geography'POINT(-122.335167 47.608013)')
Null Location Values¶
# ❌ Problem: Null locations cause sorting issues
# ✅ Solution: Filter out nulls
filter=location ne null&$orderby=geo.distance(location, geography'POINT(-122.335167 47.608013)')
Performance Issues¶
1. Slow Filter Performance¶
Symptoms: - Queries taking longer than expected - Timeouts on complex filters - High resource usage
Diagnostic Steps:
Check Filter Selectivity¶
# Test individual filter components
category eq 'Electronics' # How many results?
price gt 100 # How many results?
rating ge 4.0 # How many results?
Analyze Filter Order¶
# ❌ Less efficient (complex filter first)
contains(description, 'wireless') and category eq 'Electronics'
# ✅ More efficient (selective filter first)
category eq 'Electronics' and contains(description, 'wireless')
Use Performance Monitoring¶
// JavaScript example
const startTime = Date.now();
const results = await searchClient.search('*', {
filter: "category eq 'Electronics' and price gt 100"
});
const duration = Date.now() - startTime;
console.log(`Query took ${duration}ms`);
2. Complex Filter Optimization¶
Issue: Complex filters with multiple OR conditions perform poorly.
Solution: Use search.in() function:
# ❌ Slower
category eq 'Electronics' or category eq 'Computers' or category eq 'Phones'
# ✅ Faster
search.in(category, 'Electronics,Computers,Phones', ',')
3. Large Result Set Performance¶
Issue: Retrieving large result sets is slow.
Solutions:
Limit Result Size¶
Use Field Selection¶
Implement Pagination¶
Debugging Strategies¶
1. Systematic Filter Testing¶
Step-by-Step Approach:
-
Test Basic Components
-
Combine Gradually
-
Validate Data Types
2. Query Validation Tools¶
REST API Testing¶
# Use tools like Postman or curl to test queries
curl -X POST "https://[service].search.windows.net/indexes/[index]/docs/search?api-version=2024-07-01" \
-H "Content-Type: application/json" \
-H "api-key: [key]" \
-d '{"search": "*", "filter": "category eq '\''Electronics'\''"}'
SDK Debugging¶
# Python example with error handling
try:
results = search_client.search(
search_text="*",
filter="category eq 'Electronics'",
top=10
)
for result in results:
print(result)
except Exception as e:
print(f"Error: {e}")
print(f"Filter: category eq 'Electronics'")
3. Index Analysis¶
Check Field Attributes¶
# GET index definition to verify field attributes
GET https://[service].search.windows.net/indexes/[index]?api-version=2024-07-01
Verify Data Types¶
Error Code Reference¶
Common HTTP Status Codes¶
| Code | Meaning | Common Causes |
|---|---|---|
| 400 | Bad Request | Invalid filter syntax, malformed query |
| 404 | Not Found | Index doesn't exist, invalid endpoint |
| 403 | Forbidden | Invalid API key, insufficient permissions |
| 500 | Internal Server Error | Service issues, complex query timeout |
Specific Error Messages¶
Filter-Related Errors¶
"The field 'X' is not filterable"→ Update index schema"Invalid expression: Syntax error"→ Check OData syntax"Cannot convert value to expected type"→ Verify data types"Invalid geography literal"→ Check coordinate format
Sorting-Related Errors¶
"The field 'X' is not sortable"→ Update index schema"Invalid orderby expression"→ Check sorting syntax"Too many orderby clauses"→ Limit to 32 sort criteria
Best Practices for Troubleshooting¶
1. Preventive Measures¶
- Plan Index Schema Carefully
- Mark appropriate fields as filterable/sortable
- Choose correct data types
-
Consider performance implications
-
Validate Input Data
- Check data formats before indexing
- Handle null values appropriately
-
Validate coordinate ranges for geographic data
-
Test Incrementally
- Start with simple filters
- Add complexity gradually
- Test with realistic data volumes
2. Monitoring and Logging¶
- Log Query Performance
- Track response times
- Monitor resource usage
-
Identify slow queries
-
Set Up Alerts
- Query timeout alerts
- Performance degradation alerts
- Error rate monitoring
3. Documentation and Standards¶
- Document Filter Patterns
- Common filter combinations
- Performance benchmarks
-
Known limitations
-
Establish Coding Standards
- Consistent filter syntax
- Error handling patterns
- Testing procedures
Getting Additional Help¶
Microsoft Resources¶
Community Support¶
Professional Support¶
- Azure Support Plans
- Microsoft Professional Services
- Azure AI Search Consulting Partners
Quick Reference Checklist¶
When troubleshooting filters and sorting:
- [ ] Verify field is marked as filterable/sortable
- [ ] Check OData syntax (quotes, operators, parentheses)
- [ ] Validate data types and formats
- [ ] Test filter components individually
- [ ] Check for null values
- [ ] Verify coordinate formats for geographic queries
- [ ] Monitor query performance
- [ ] Review error messages carefully
- [ ] Test with minimal data set first
- [ ] Document working solutions for future reference
This troubleshooting guide should help you quickly identify and resolve common issues with Azure AI Search filters and sorting operations.