Stay organized with collectionsSave and categorize content based on your preferences.
Query expansion is the incremental loosening of query constraints to include more results when none or too few are initially found. This leads to an adjustment of the result size per query.
When there are no relevant documents at all for a query, Query expansion will return less relevant documents to ensure that query does not return zero search results.
Query expansion tutorial
This tutorial shows you how to enable the query expansion feature. When a shopper uses an ambiguous or a multi-word search phrase, they can get an empty response. After turning on query expansion, the request is analyzed and the expanded list of products based on the parsed search query gets returned.
To follow step-by-step guidance for this task directly in the
Cloud Shell Editor, clickGuide me:
This page uses the following dataset as an example. Expand it to view the fields within the sample product description dataset.
Example product dataset
ID
title
brands
categories
price_info.price
"nest_mini_2nd_gen"
"Nest Mini (2nd gen)"
["Google", "Nest"]
["Nest > speakers and displays"]
49.00
"nest_audio"
"Nest Audio"
["Google", "Nest"]
["Nest > speakers and displays"]
99.99
"nest_hub_max"
"Nest Hub Max"
["Google", "Nest"]
["Nest > speakers and displays"]
229.00
"nest_hub"
"Nest Hub"
["Google", "Nest"]
["Nest > speakers and displays"]
88.99
"google_home_max"
"Google Home Max"
["Google", "Nest"]
["Nest > speakers and displays"]
299.00
"google_home_mini"
"Google Home Mini"
["Google", "Nest"]
["Nest > speakers and displays"]
49.00
"google_pixel_5"
"Google Pixel 5"
["Google", "Pixel"]
["Pixel > phones"]
699.00
"google_pixel_4a_with_5g"
"Google Pixel 4a with 5G"
["Google", "Pixel"]
["Pixel > phones"]
499.00
"google_pixel_4a"
"Google Pixel 4a Phones"
["Google", "Pixel"]
["Pixel > phones"]
349.00
"google_pixel_stand"
"Google Pixel Stand"
["Google", "Pixel"]
["Pixel > featured accessories"]
79.00
"google_pixel_buds"
"Google Pixel Buds"
["Google", "Pixel"]
["Pixel > featured accessories"]
179.00
"google_pixel_5_case"
"Google Pixel 5 Case"
["Google", "Pixel"]
["Pixel > featured accessories"]
40.00
"google_pixel_4a_5g_case"
"Google Pixel 4a (5G) Case"
["Google", "Pixel"]
["Pixel > featured accessories"]
40.00
"google_pixel_4a_case"
"Google Pixel 4a Case"
["Google", "Pixel"]
["Pixel > featured accessories"]
40.00
Query expansion
Query expansion increases the recall for query terms with few results,
especially long tail queries.
This search feature is driven by aspecificationdetermining query-expansion conditions. It includes apinUnexpandedResultsoption that's off by default. When set totrue, it displays unexpanded products at the top of search results. The top is followed by the expanded results.
For example, if you search forGoogle Pixel 5without query expansion, the
result will be restricted togoogle_pixel_5IDs. However, with query
expansion, you might also getgoogle_pixel_4a_with_5g,google_pixel_4a, andgoogle_pixel_5_caseIDs in theexample product description datasetas well.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Query expansion is the incremental loosening of query constraints to include more results when none or too few are initially found. This leads to an adjustment of the result size per query.\n\nWhen there are no relevant documents at all for a query, Query expansion will return less relevant documents to ensure that query does not return zero search results.\n\nQuery expansion tutorial\n------------------------\n\nThis tutorial shows you how to enable the query expansion feature. When a shopper uses an ambiguous or a multi-word search phrase, they can get an empty response. After turning on query expansion, the request is analyzed and the expanded list of products based on the parsed search query gets returned.\n\n*** ** * ** ***\n\nTo follow step-by-step guidance for this task directly in the\nCloud Shell Editor, click **Guide me**:\n\n[Guide me](https://shell.cloud.google.com/?walkthrough_tutorial_id=retail--retail_api_v2_query_expansion_select_language&show=ide&environment_deployment=ide)\n\n*** ** * ** ***\n\nExample dataset\n---------------\n\nThis page uses the following dataset as an example. Expand it to view the fields within the sample product description dataset. \n\n#### Example product dataset\n\nQuery expansion\n---------------\n\nQuery expansion increases the recall for query terms with few results,\nespecially long tail queries.\n\nThis search feature is driven by a [specification](/retail/docs/reference/rest/v2/QueryExpansionSpec) determining query-expansion conditions. It includes a `pinUnexpandedResults` option that's off by default. When set to `true`, it displays unexpanded products at the top of search results. The top is followed by the expanded results. \n\n### Java\n\n import com.google.cloud.retail.v2.https://cloud.google.com/java/docs/reference/google-cloud-retail/latest/com.google.cloud.retail.v2.SearchRequest.html;\n import com.google.cloud.retail.v2.https://cloud.google.com/java/docs/reference/google-cloud-retail/latest/com.google.cloud.retail.v2.SearchRequest.html.https://cloud.google.com/java/docs/reference/google-cloud-retail/latest/com.google.cloud.retail.v2.SearchRequest.QueryExpansionSpec.html;\n import com.google.cloud.retail.v2.https://cloud.google.com/java/docs/reference/google-cloud-retail/latest/com.google.cloud.retail.v2.SearchRequest.html.https://cloud.google.com/java/docs/reference/google-cloud-retail/latest/com.google.cloud.retail.v2.SearchRequest.QueryExpansionSpec.html.https://cloud.google.com/java/docs/reference/google-cloud-retail/latest/com.google.cloud.retail.v2.Condition.html;\n import com.google.cloud.retail.v2.https://cloud.google.com/java/docs/reference/google-cloud-retail/latest/com.google.cloud.retail.v2.SearchResponse.html;\n import com.google.cloud.retail.v2.https://cloud.google.com/java/docs/reference/google-cloud-retail/latest/com.google.cloud.retail.v2.SearchServiceClient.html;\n\n public static void searchProductsWithQueryExpansion(String query, int pageSize,\n https://cloud.google.com/java/docs/reference/google-cloud-retail/latest/com.google.cloud.retail.v2.Condition.html condition) throws IOException, InterruptedException {\n https://cloud.google.com/java/docs/reference/google-cloud-retail/latest/com.google.cloud.retail.v2.SearchRequest.QueryExpansionSpec.html queryExpansionSpec = https://cloud.google.com/java/docs/reference/google-cloud-retail/latest/com.google.cloud.retail.v2.SearchRequest.QueryExpansionSpec.html.newBuilder()\n .setCondition(condition)\n .build();\n\n https://cloud.google.com/java/docs/reference/google-cloud-retail/latest/com.google.cloud.retail.v2.SearchRequest.html searchRequest = https://cloud.google.com/java/docs/reference/google-cloud-retail/latest/com.google.cloud.retail.v2.SearchRequest.html.newBuilder()\n .setPlacement(DEFAULT_SEARCH_PLACEMENT_NAME)\n .setBranch(DEFAULT_BRANCH_NAME)\n .setVisitorId(VISITOR_ID)\n .setQuery(query)\n .setPageSize(pageSize)\n .https://cloud.google.com/java/docs/reference/google-cloud-retail/latest/com.google.cloud.retail.v2.SearchRequest.Builder.html#com_google_cloud_retail_v2_SearchRequest_Builder_setQueryExpansionSpec_com_google_cloud_retail_v2_SearchRequest_QueryExpansionSpec_(queryExpansionSpec)\n .build();\n\n try (https://cloud.google.com/java/docs/reference/google-cloud-retail/latest/com.google.cloud.retail.v2.SearchServiceClient.html searchClient = https://cloud.google.com/java/docs/reference/google-cloud-retail/latest/com.google.cloud.retail.v2.SearchServiceClient.html.create()) {\n https://cloud.google.com/java/docs/reference/google-cloud-retail/latest/com.google.cloud.retail.v2.SearchResponse.html response = searchClient.search(searchRequest).getPage().getResponse();\n System.out.println(\"Search response: \" + searchResponse);\n }\n }\n\n\u003cbr /\u003e\n\nFor example, if you search for *Google Pixel 5* without query expansion, the\nresult will be restricted to `google_pixel_5` IDs. However, with query\nexpansion, you might also get `google_pixel_4a_with_5g`, `google_pixel_4a`, and\n`google_pixel_5_case` IDs in the [example product description dataset](#example-dataset) as well."]]