Boost results

This page outlines boosting of search results.

Boosting tutorial

This tutorial shows you some examples of product boosting.


To follow step-by-step guidance for this task directly in the Cloud Shell Editor, click Guide me:

Guide me


Example dataset

This page uses the following dataset as an example. Only the fields necessary for explanation are included.

Prerequisites

See Filter and order results for the filter expression syntax.

Boost

With boosting, you can control the result ranking by apply a boost to prioritize or deprioritize the results.

For example, if you search for "Google speaker", then you would get "nest_mini_2nd_gen", "nest_audio", "nest_hub_max", "nest_hub", "google_home_max" and "google_home_mini" in no specific order.

Java

import com.google.cloud.retail.v2.SearchRequest;
import com.google.cloud.retail.v2.SearchRequest.BoostSpec;
import com.google.cloud.retail.v2.SearchRequest.BoostSpec.ConditionBoostSpec;
import com.google.cloud.retail.v2.SearchResponse;
import com.google.cloud.retail.v2.SearchServiceClient;

public static void searchProductsWithBoostSpec(String query, int pageSize,
    String condition, float boostStrength) throws IOException, InterruptedException {
  BoostSpec boostSpec = BoostSpec.newBuilder()
      .addConditionBoostSpecs(ConditionBoostSpec.newBuilder()
          .setCondition(condition)
          .setBoost(boostStrength)
          .build())
      .build();

  SearchRequest searchRequest = SearchRequest.newBuilder()
      .setPlacement(DEFAULT_SEARCH_PLACEMENT_NAME)
      .setBranch(DEFAULT_BRANCH_NAME)
      .setVisitorId(VISITOR_ID)
      .setQuery(query)
      .setPageSize(pageSize)
      .setBoostSpec(boostSpec)
      .build();

  try (SearchServiceClient searchClient = SearchServiceClient.create()) {
    SearchResponse response = searchClient.search(searchRequest).getPage().getResponse();
    System.out.println("Search response: " + searchResponse);
  }
}

Suppose that you want to prioritize the cheaper products (less than 95 dollars) and deprioritize the expensive ones (higher than 95 dollars). You can apply a boost spec as:

JSON

{
  condition_boost_specs {
    condition: "price: IN(*, 95.0e)"
    boost: 0.5
  }
  condition_boost_specs {
    condition: "price: IN(95.0e, *)"
    boost: -0.5
  }
}

In the result, "nest_mini_2nd_gen", "google_home_mini" and "nest_hub" might be the first three, while "nest_audio", "nest_hub_max" and "google_home_max" might be last three. However, no specific order is guaranteed, which is different from ordering by price, as discussed in Filter and order results.