# A single expression or multiple expressions that are joined by "AND" or "OR".filter=expression,{" AND "|"OR",expression};# Expressions can be prefixed with "-" or "NOT" to express a negation.expression=["-"|"NOT "],
# A parenthetical expression.|"(",expression,")"# A simple expression applying to a text field.# Function "ANY" returns true if the field contains any of the literals.(text_field,":","ANY","(",literal,{",",literal},")"# A simple expression applying to a numerical field. Function "IN" returns true# if a field value is within the range. By default, lower_bound is inclusive and# upper_bound is exclusive.|numerical_field,":","IN","(",lower_bound,",",upper_bound,")"# A simple expression that applies to a numerical field and compares with a double value.|numerical_field,comparison,double);# A lower_bound is either a double or "*", which represents negative infinity.# Explicitly specify inclusive bound with the character 'i' or exclusive bound# with the character 'e'.lower_bound=(double,["e"|"i"])|"*";# An upper_bound is either a double or "*", which represents infinity.# Explicitly specify inclusive bound with the character 'i' or exclusive bound# with the character 'e'.upper_bound=(double,["e"|"i"])|"*";# Supported comparison operators.comparison="<="|"<"|">="|">"|"=";# A literal is any double quoted string. You must escape backslash (\) and# quote (") characters.literal=doublequotedstring;text_field=seethetablebelow;numerical_field=seethetablebelow;
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-18。"],[],[],null,["# Filter and order results\n\nThis page describes filtering and ordering with search.\n\nSemantic filtering optimizes non-default sorting\n------------------------------------------------\n\nVertex AI Search for commerce is designed to show a wide variety of results, including marginally relevant items that are popular or trending. This is effective in default searches (sorted by relevance), but in non-default searches that use the `order_by` feature, such as sort-by-price, less relevant items can appear more prominently due to lower price, for example.\n\nTo ameliorate this effect, Vertex AI Search for commerce has introduced semantic embedding-based filtering to remove these less relevant items from non-default search results, improving user experience.\n\nThis intended behavior could, however, lead fewer search results overall. Disabling filtering can impact search-result quality. So if fewer search results is of concern, contact [support](/retail/docs/support).\n\nFiltering tutorial\n------------------\n\nThis tutorial demonstrates the filtering feature. It lets you fine-tune search requests according to your or your customer's needs. You can filter by single or multiple fields, filter by text or numeric fields, or both. You can use an expression language to construct a predicate for each field or combine different expressions using logical operators. For example, a shopper looking for shoes can use filters to narrow their search to their preferred brand and color.\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_filtering_select_language&show=ide&environment_deployment=ide)\n\n*** ** * ** ***\n\nOrdering tutorial\n-----------------\n\nThis tutorial shows how to order items in a search response. When an end user looks for a product on your site, they are shown results ordered by multiple fields. For example, a user is searching for a dress with the best price and discount. Price and discount are multiple fields here. The user will see the dresses ordered by price, and for the dresses with the same price, ordered by discount.\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_ordering_select_language&show=ide&environment_deployment=ide)\n\n*** ** * ** ***\n\nExample dataset\n---------------\n\nThis page uses the following dataset as an example. Only the fields necessary\nfor the example are included.\n\n#### Example product dataset\n\nFilter\n------\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.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 searchFilteredProducts(String query, int pageSize,\n String filter) throws IOException, InterruptedException {\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 .setFilter(filter)\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\nThe filter expression syntax can be summarized by the following\n[EBNF](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form#:%7E:text=In%20computer%20science%2C%20extended%20Backus,as%20a%20computer%20programming%20language): \n\n```bash\n # A single expression or multiple expressions that are joined by \"AND\" or \"OR\".\n filter = expression, { \" AND \" | \"OR\", expression };\n\n # Expressions can be prefixed with \"-\" or \"NOT\" to express a negation.\n expression = [ \"-\" | \"NOT \" ],\n # A parenthetical expression.\n | \"(\", expression, \")\"\n # A simple expression applying to a text field.\n # Function \"ANY\" returns true if the field contains any of the literals.\n ( text_field, \":\", \"ANY\", \"(\", literal, { \",\", literal }, \")\"\n # A simple expression applying to a numerical field. Function \"IN\" returns true\n # if a field value is within the range. By default, lower_bound is inclusive and\n # upper_bound is exclusive.\n | numerical_field, \":\", \"IN\", \"(\", lower_bound, \",\", upper_bound, \")\"\n # A simple expression that applies to a numerical field and compares with a double value.\n | numerical_field, comparison, double );\n\n # A lower_bound is either a double or \"*\", which represents negative infinity.\n # Explicitly specify inclusive bound with the character 'i' or exclusive bound\n # with the character 'e'.\n lower_bound = ( double, [ \"e\" | \"i\" ] ) | \"*\";\n\n # An upper_bound is either a double or \"*\", which represents infinity.\n # Explicitly specify inclusive bound with the character 'i' or exclusive bound\n # with the character 'e'.\n upper_bound = ( double, [ \"e\" | \"i\" ] ) | \"*\";\n\n # Supported comparison operators.\n comparison = \"\u003c=\" | \"\u003c\" | \"\u003e=\" | \"\u003e\" | \"=\";\n\n # A literal is any double quoted string. You must escape backslash (\\) and\n # quote (\") characters.\n literal = double quoted string;\n\n text_field = see the table below;\n\n numerical_field = see the table below;\n```\n\nThe supported textual fields are summarized in the following table.\n\nThe supported numerical fields are summarized in the following table.\n\nAt most, 10 nested conjunctions or disjunctions are allowed.\n\nFor example, to search for a Google product in the following situations\nrespectively, you could set `query` as \"Google\" and set `filter` as the values\nshown in the following table:\n\n| **Note:** Due to the built-in pagination capability of Search, it's good to maintain local inventory fields when filtering. For more information, see [Update local inventory](/retail/docs/local-inventory-updates).\n\nOrder\n-----\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.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 searchOrderedProducts(String query, int pageSize,\n String orderBy) throws IOException, InterruptedException {\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 .setOrderBy(orderBy)\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\nThe supported orderable fields are summarized in the following table.\n\nBy default, the order is ascending. Descending order can be specified by \"desc\"\nsuffix, such as, \"rating desc\".\n\nFor a numeric field with multiple values, for example, a repeated field or a\nfield that is set for variant products, the minimum value is used for sorting by\nascending order and the maximum value is used for sorting by descending order.\n\nOrdering by multiple fields is supported through the use of comma-separated fields\nin order of priority. The lower priority fields are used to order items with\nequal values for higher priority fields. For example, \"rating desc, price\"\norders items with the same rating by price."]]