Simple Query Syntax in Azure AI Search: A Complete Guide to Keyword, Boolean, and Prefix Search
In-depth discussion
Technical
0 0 1
This reference article documents the Simple Query Parser syntax used in Azure AI Search for full text search. It explains keyword and phrase search, Boolean operators (+, |, -) and how the searchMode parameter affects NOT behavior, prefix queries using the * suffix operator, escaping of search operators and unsafe URL characters, handling of special characters via analyzer choice, grouping with parentheses, and query size limits. Examples use the REST API with the hotel-rooms-sample index.
main points
unique insights
practical applications
key topics
key insights
learning outcomes
• main points
1
Precise, well-organized reference covering every operator in the simple query syntax with a character-based operator table
2
Clearly explains the non-obvious interaction between the NOT operator and searchMode (any vs. all), with concrete examples
3
Provides concrete operational limits (URL length, body size, clause count, term length) and guidance on escaping and special characters
• unique insights
1
The searchMode=any vs. searchMode=all distinction changes how a standalone '-' is interpreted (OR NOT vs. AND NOT), a frequent source of unexpected results
2
Special-character loss is caused by analyzer choice at index time; the article advises testing analyzer tokenization and using multiple analyzer-specific fields as a workaround
• practical applications
Highly practical for developers constructing search queries programmatically, especially for correctly escaping user input, choosing searchMode for precision vs. recall, and avoiding unexpected results from NOT and special characters.
• key topics
1
Simple Query Parser syntax and its relation to the Lucene Simple Query Parser
2
Boolean operators (+, |, -) and searchMode interaction
3
Prefix queries and escaping of search operators and URL-reserved characters
• key insights
1
Authoritative, precise reference for query syntax behavior in Azure AI Search, reducing trial-and-error in query construction
2
Explains subtle NOT-operator semantics and special-character tokenization pitfalls tied to analyzer selection
3
Documents concrete query size and complexity limits needed for safe programmatic query generation
• learning outcomes
1
Construct simple keyword, phrase, and Boolean queries correctly in Azure AI Search
2
Choose between searchMode any and all based on precision vs. recall requirements and understand NOT-operator behavior
3
Escape special characters and handle analyzer-dependent tokenization to avoid unexpected query results
Azure AI Search offers two Lucene-based query languages for full text search. The Simple Query Parser is the default and is designed to interpret requests even when they are not perfectly composed. The Lucene Query Parser supports more advanced query constructions. This guide focuses on the simple syntax, which is based on the Apache Lucene Simple Query Parser but excludes fuzzy search. Query syntax applies to the search parameter of a query request and is distinct from the OData syntax used for filter and orderby expressions. A basic example sets "queryType": "simple" with a search string such as budget hotel +pool and searchMode set to all to ensure every criterion is matched.
“ Keyword Search on Terms and Phrases
Search strings can contain terms, phrases, boolean operators, precedence operators, prefix characters, escape characters, and URL-encoded characters. A term search matches documents containing any of the supplied terms. A phrase search, enclosed in quotation marks, matches only the exact phrase in order, such as "Roach Motel". Depending on the client, quotation marks may need escaping in raw request bodies. All search strings pass through lexical analysis, so reviewing the tokenization behavior of the chosen analyzer is essential for predictable results. If no search text is provided, the query returns the top 50 documents in unranked order.
“ Boolean Operators and searchMode
Boolean operators in the simple syntax are character-based: + performs AND, | performs OR (and OR is the default when terms are simply adjacent), and - performs NOT. The searchMode parameter controls how a NOT term combines with other terms. With searchMode=any, which favors recall, pool - ocean is interpreted as an OR NOT and returns documents containing pool or lacking ocean. With searchMode=all, which favors precision, the same query is interpreted as AND NOT, returning documents containing pool but not ocean. Choose all for precision-focused applications, especially when users frequently rely on the - operator, and choose any when broader recall is preferred.
“ Prefix (Starts-With) Queries
Prefix queries use the asterisk (*) suffix operator to match terms that begin with a given string, such as lingui* matching linguistic or linguini. A prefix must start with at least one plain text character. Prefix matches are exact, so every result receives a score of 1.0 and no relevance ranking is applied. Prefix queries can be slow on large indexes when the prefix is short, and alternatives such as edge n-gram tokenization may perform better. Prefix terms cannot exceed 1000 characters. Suffix and infix wildcard matching require the full Lucene syntax.
“ Grouping with Precedence Operators
Parentheses create subqueries and control evaluation order. For example, motel+(wifi|luxury) returns documents containing motel together with either wifi or luxury, or both. Use grouping whenever mixing AND and OR logic to ensure the intended match behavior.
“ Escaping Search Operators
The simple syntax treats the characters + | " ( ) ' and \ as operators. When these characters appear as part of an indexed token, prefix them with a single backslash. For instance, to match the indexed token Luxury+Hotel, use search=luxury\+hotel. Two cases do not require escaping: a hyphen is only treated as NOT when it follows whitespace, so identifiers like 3352CDD0-EF30-4A2E-A512-3B30AF40F3FD need no escape, and an asterisk is only a prefix operator when it ends a term before whitespace, so expressions like 4*4=16 are safe.
“ Encoding Unsafe and Reserved Characters in URLs
Characters that are unsafe in URLs, including " ` < > # % { } | \ ^ ~ [ ], must be percent-encoded. Reserved characters, including ; / ? : @ = + &, have structural meaning in URLs and must be encoded when used as data. For example, the # symbol must be encoded as %23 in a URL. Many web clients perform this encoding automatically, but programmatically built queries should verify it explicitly.
“ Handling Special Characters and Analyzers
Special characters such as currency symbols or emojis may be dropped by default analyzers like the standard analyzer. To preserve them, choose an analyzer suited to the requirement: the whitespace analyzer treats whitespace-separated sequences as tokens, while language analyzers such as en.microsoft may retain symbols like $ or €. A common workaround is to index the same content into multiple fields with different analyzers. Test tokenization to confirm behavior, and when using Unicode characters in URLs, ensure they are properly escaped, for example ❤ as %E2%9D%A4.
“ Query Size Limits
Applications generating queries programmatically should bound query size. GET request URLs cannot exceed 8 KB. POST requests, which carry search, filter, and orderby parameters in the body, can be up to 16 MB. The search clause is limited to 100,000 characters and 1024 clauses joined by AND or OR. Prefix search terms cannot exceed 1000 characters, and individual terms in a query are limited to roughly 32 KB. Consult the API request limits documentation for complete details.
“ Next Steps and Related Resources
For programmatic query construction, review Full text search in Azure AI Search to understand query processing stages and the effects of text analysis. Additional resources include query examples for simple and full Lucene search, the Search Documents REST API reference, Lucene query syntax documentation, and the OData expression syntax reference for filter and select operations.
We use cookies that are essential for our site to work. To improve our site, we would like to use additional cookies to help us understand how visitors use it, measure traffic to our site from social media platforms and to personalise your experience. Some of the cookies that we use are provided by third parties. To accept all cookies click ‘Accept’. To reject all optional cookies click ‘Reject’.
Comment(0)