Querying indexes with syntax, sorting, and filtering
5 minutes
5 Questions
Querying indexes in Azure Cognitive Search involves using a powerful query syntax to retrieve relevant documents from your search index. The query language supports various operators and expressions to refine your search results effectively.
**Basic Query Syntax:**
Azure Cognitive Search uses ODat…Querying indexes in Azure Cognitive Search involves using a powerful query syntax to retrieve relevant documents from your search index. The query language supports various operators and expressions to refine your search results effectively.
**Basic Query Syntax:**
Azure Cognitive Search uses OData syntax for queries. The primary parameter is 'search' which accepts simple or full Lucene query syntax. Simple syntax supports basic operators like AND, OR, and NOT, while full Lucene enables wildcards, fuzzy matching, proximity searches, and field-scoped queries.
Example: `search=azure AND cloud` finds documents containing both terms.
**Filtering:**
Filters use the '$filter' parameter with OData expressions to narrow results based on field values. Filters operate on filterable fields and support comparison operators (eq, ne, gt, lt, ge, le), logical operators (and, or, not), and functions.
Example: `$filter=category eq 'Technology' and rating gt 4`
Filters are evaluated before query execution, reducing the document set that needs scoring, which improves performance significantly.
**Sorting:**
The '$orderby' parameter controls result ordering. You can sort by one or multiple fields in ascending (asc) or descending (desc) order. Only sortable fields can be used.
Example: `$orderby=publishDate desc, title asc`
By default, results are ordered by relevance score when no orderby is specified.
**Additional Parameters:**
- '$select': Specifies which fields to return
- '$top': Limits the number of results
- '$skip': Enables pagination
- '$count': Returns total matching document count
- 'facets': Returns aggregated category counts
**Combined Example:**
search=machine learning
&$filter=year ge 2020 and category eq 'AI'
&$orderby=relevanceScore desc
&$select=title,author,summary
&$top=10
Understanding these query capabilities allows you to build sophisticated search experiences that return precisely the information users need from your knowledge mining solutions.
Querying Indexes with Syntax, Sorting, and Filtering in Azure AI Search
Why It Is Important
Querying indexes is a fundamental skill for the AI-102 exam because Azure AI Search (formerly Azure Cognitive Search) is a core component of knowledge mining solutions. Understanding how to construct effective queries ensures you can retrieve relevant information efficiently, which is essential for building intelligent search applications and chatbots.
What It Is
Querying indexes in Azure AI Search involves using the Search API to retrieve documents from an index. The query syntax supports:
• Simple query syntax - Basic keyword searches with optional operators • Full Lucene query syntax - Advanced queries with wildcards, fuzzy search, proximity search, and regular expressions • OData filter expressions - Structured filtering on specific fields • $orderby - Sorting results by one or more fields • $select - Choosing which fields to return • $top and $skip - Pagination control
How It Works
Query Parameters: • search - The search text (use * for all documents) • $filter - OData expression to narrow results (e.g., $filter=rating ge 4) • $orderby - Sort order (e.g., $orderby=price desc) • $select - Fields to return (e.g., $select=name,description) • queryType - Set to 'simple' or 'full' for Lucene syntax • searchMode - 'any' (OR) or 'all' (AND) for terms
Filter Operators: • eq - equals • ne - not equals • gt, ge, lt, le - comparison operators • and, or, not - logical operators • search.in() - matches any value in a list • geo.distance() - geographic filtering
Exam Tips: Answering Questions on Querying Indexes
1. Know the difference between $filter and search - Filters apply to filterable fields and use exact matching; search applies to searchable fields with text analysis.
2. Remember field requirements - Fields must be marked as sortable for $orderby, filterable for $filter, and searchable for full-text search.
3. Understand queryType parameter - Choose 'full' when questions mention wildcards, fuzzy matching, or regular expressions.
4. Watch for searchMode - When questions ask about requiring ALL terms to match, the answer involves searchMode=all.
5. OData syntax matters - String values in filters require single quotes: $filter=category eq 'Hotels'
6. Scoring profiles - If asked about boosting certain fields or fresh content, look for answers involving scoring profiles.
7. Facets vs Filters - Facets (facet parameter) return aggregated counts for navigation; filters narrow the result set.
8. Case sensitivity - Filter comparisons on string fields are case-sensitive by default.
9. Geographic queries - Know that geo.distance() returns distance in kilometers and requires Edm.GeographyPoint fields.