Query latency scales with the number of documents your query retrieves — the more documents matched, the longer the search takes. The optimizations below reduce retrieval cost and resolve common query errors. If search results still don't meet expectations after applying these, contact Alibaba Cloud technical support.
Always specify an index
Every query must reference an index defined in your application. If you omit the index name, the engine falls back to the default index. If no default index exists, the query returns an error with no results.
query='mp3' # Equivalent to query=default:'mp3'
query=default:'mp3' # Explicit index referenceEnclose keywords in single quotation marks
The query parser requires keywords to be wrapped in single quotation marks (' '). Without them, the query fails to parse and returns no results.
# Error — missing single quotation marks
query=default:mp3
# Correct
query=default:'mp3'Escape apostrophes and backslashes
Two characters require special handling inside a keyword string:
Apostrophe (`'`) — escape it as
\', or remove it. An unescaped apostrophe terminates the keyword string early.Backslash (`\`) — escape it as
\\. A backslash at the end of a keyword acts as an escape character for the closing', which breaks parsing.
# Retrieves documents containing both "Beijing" and "University"
query=default:'Beijing University'
# Error — the apostrophe in "abc's" ends the string prematurely
query=default:'abc's efg'
# Correct — escape the apostrophe
query=default:'abc\'s efg'
# Error — the backslash escapes the closing quote
query=default:'abc\'Use the query clause to filter indexed fields
When filtering on a field, index that field and query it through the query clause rather than the filter clause. This can improve query performance.
# Slower — filter clause
query=user_id:'123'&&filter=type_id=1
# Faster — both conditions use the query clause on indexed fields
query=user_id:'123' AND type_id:'1'Use range search for large time-based datasets
When a query matches a large number of documents — for example, 50 million records for user_id=123 — and only a small subset meets the time condition, such as 1,000 records, the engine may cause a timeout.
Use range search instead, which is more efficient.
# Slow — may cause timeout when result set is large
query=user_id:'123'&&filter=time>"2016-09-16"
# Fast — range search, more efficient
query=user_id:'123' AND index_timestamp:(1473955200000,)Reduce result data size
After matching documents are identified, the engine fetches and returns their field data. Returning large amounts of field data per document increases transfer time.
Two approaches reduce this cost:
Limit the result set size. Pagination defaults to 20 results per page — avoid fetching more than necessary.
Return only required fields. Modify the
fetch_fieldsparameter (or the default display fields configuration) to include only the fields your application actually uses.