Search suggestions
Stay organized with collections
Save and categorize content based on your preferences.
Get Suggested Searches
To get search suggestions, call the getSearchSuggestions
method on the
ContextualSearchRuntime
. This method takes a single
GetSearchSuggestionsOptions
where you can specify the text to use to get search suggestions for.
The returned SearchSuggestions
object does not contain any accessible
properties. It contains all of the necessary information to render search
suggestions, but otherwise is opaque.
Swift uses the Swift Concurrency style, Objective-C uses a callback function.
Swift
getSearchSuggestions(withOptions:)
Task {
let suggestions = try await searchRuntime.getSearchSuggestions(
withOptions: GetSearchSuggestionsOptions(withTextContext:['Sample Query', 'Another query string']))
}
Objective-C
getSearchSuggestionsWithOptions
[searchRuntime
getSearchSuggestionsWithOptions:
[[GetSearchSuggestionsOptions alloc] initWithTextContext: @[@'Sample Query', @'Another query string' ]]
completionHandler:^(SearchSuggestions *_Nullable suggestions,
NSError *_Nullable error) {
// error will be not null if there is an error.
// On success, suggestions will be non null.
}];
Get Location-based Suggested Searches
The getSearchSuggestions
request also supports location-based search
suggestions. Build a GetSearchSuggestionsOptions
object with a
LocationSuggestionsContext.
Follow these steps to build the LocationSuggestionsContext
class:
- (Required) Set
latitude
, longitude
and radiusMeters
to match the
requested area.
- (Optional) Set
startTime
to match the requested time at which the search
suggestions should apply.
- (Optional) Set
geoTypes
list with the requested geo types. Provided types
are used to adjust the ranking and filtering of results returned by the
service. The supported values are documented in
LocationSuggestionsContext.
Swift
getSearchSuggestions(withOptions:)
Task {
let locationContext: LocationSuggestionsContext = {
let context = LocationSuggestionsContext()
context.latitude = 40.748595
context.longitude = -73.992365
context.radiusMeters = 2500
context.startTime = try?
Date.ISO8601FormatStyle.iso8601.parse("2024-03-14T19:20:30.000+02:00")
context.geoTypes = ["restaurant", "cafe"]
return context
}()
let suggestions = try await searchRuntime.getSearchSuggestions(
withOptions: GetSearchSuggestionsOptions(locationContext: [
locationContext
]))
}
Objective-C
getSearchSuggestionsWithOptions
LocationSuggestionsContext *locationContext;
locationContext = [[LocationSuggestionsContext alloc] init];
locationContext.latitude = 40.748595;
locationContext.longitude = -73.992365;
locationContext.radiusMeters = @2500;
[searchRuntime
getSearchSuggestionsWithOptions:
[[GetSearchSuggestionsOptions alloc] initWithLocationContext: locationContext]
completionHandler:^(SearchSuggestions *_Nullable suggestions,
NSError *_Nullable error) {
// error will be not null if there is an error.
// On success, suggestions will be non null.
}];
Display
To display search suggestions chips, use the following methods (the style varies
if you're using UIKit versus SwiftUI).
Swift UIKit
Display search suggestion chips using the
SearchSuggestionsViewController.
// Create the SearchSuggestionsViewController with specified display options.
var searchSuggestionsController = SearchSuggestionsViewController(options: viewOptions)
...
// Add the view controller to the existing view hierarchy
searchSuggestionsController.addToViewContoller(self)
view.addSubview(searchSuggestionsController.view)
...
// Assign a specific set of search suggestions to this view controller.
searchSuggestionsController.searchSuggestions = suggestions
Objective-C
Display search suggestion chips using the
SearchSuggestionsViewController.
// Create the SearchSuggestionsController with specified display options.
SearchSuggestionsViewController *_searchSuggestionsController =
[[SearchSuggestionsViewController alloc] initWithOptions:_customizationOptions];
...
// Add the view controller to the existing view hierarchy
[_searchSuggestionsController addToViewController:self];
[self.view addSubview:_searchSuggestionsController.view];
...
// Assign a specific set of search suggestions to this view controller.
_searchSuggestionsController.searchSuggestions = suggestions
SwiftUI
Search suggestions can be displayed with a
SearchSuggestionsView
SearchSuggestionsView(suggestions: suggestions, options: options)
Next: Display search resultsarrow_forward
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-11-05 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-11-05 UTC."],[[["\u003cp\u003eCall the \u003ccode\u003egetSearchSuggestions\u003c/code\u003e method on the \u003ccode\u003eContextualSearchRuntime\u003c/code\u003e to retrieve search suggestions, customizing it with text or location-based context.\u003c/p\u003e\n"],["\u003cp\u003eThe returned \u003ccode\u003eSearchSuggestions\u003c/code\u003e object is opaque and primarily used for rendering suggestions within the UI.\u003c/p\u003e\n"],["\u003cp\u003eDisplay search suggestion chips using \u003ccode\u003eSearchSuggestionsViewController\u003c/code\u003e in UIKit or \u003ccode\u003eSearchSuggestionsView\u003c/code\u003e in SwiftUI, ensuring they are integrated into your view hierarchy.\u003c/p\u003e\n"],["\u003cp\u003eLocation-based suggestions require setting latitude, longitude, radius, and optionally, start time and geo types within the \u003ccode\u003eLocationSuggestionsContext\u003c/code\u003e.\u003c/p\u003e\n"]]],["To retrieve search suggestions, utilize the `getSearchSuggestions` method with `GetSearchSuggestionsOptions`, specifying text or a `LocationSuggestionsContext` (including latitude, longitude, radius, optional start time, and geo types). Display results using `SearchSuggestionsViewController` in UIKit, adding it to the view hierarchy and assigning suggestions. In SwiftUI use the `SearchSuggestionsView`. The `SearchSuggestions` object contains the render data for the suggestions.\n"],null,["# Search suggestions\n\nGet Suggested Searches\n----------------------\n\nTo get search suggestions, call the `getSearchSuggestions` method on the\n`ContextualSearchRuntime`. This method takes a single\n[GetSearchSuggestionsOptions](/search-in-apps/ios/reference/api/swift_reference/Classes/GetSearchSuggestionsOptions)\nwhere you can specify the text to use to get search suggestions for.\n\nThe returned `SearchSuggestions` object does not contain any accessible\nproperties. It contains all of the necessary information to render search\nsuggestions, but otherwise is opaque.\n\nSwift uses the Swift Concurrency style, Objective-C uses a callback function. \n\n### Swift\n\n[getSearchSuggestions(withOptions:)](/search-in-apps/ios/reference/api/swift_reference/Classes/ContextualSearchRuntime#getsearchsuggestionswithoptions:) \n\n Task {\n let suggestions = try await searchRuntime.getSearchSuggestions(\n withOptions: GetSearchSuggestionsOptions(withTextContext:['Sample Query', 'Another query string']))\n }\n\n### Objective-C\n\n[getSearchSuggestionsWithOptions](/search-in-apps/ios/reference/api/objc_reference/Classes/ContextualSearchRuntime#-getsearchsuggestionswithoptions:completionhandler:) \n\n [searchRuntime\n getSearchSuggestionsWithOptions:\n [[GetSearchSuggestionsOptions alloc] initWithTextContext: @[@'Sample Query', @'Another query string' ]]\n completionHandler:^(SearchSuggestions *_Nullable suggestions,\n NSError *_Nullable error) {\n // error will be not null if there is an error.\n // On success, suggestions will be non null.\n }];\n\nGet Location-based Suggested Searches\n-------------------------------------\n\nThe `getSearchSuggestions` request also supports location-based search\nsuggestions. Build a `GetSearchSuggestionsOptions` object with a\n[LocationSuggestionsContext](/search-in-apps/ios/reference/api/objc_reference/Classes/LocationSuggestionsContext).\n\nFollow these steps to build the `LocationSuggestionsContext` class:\n\n1. (Required) Set `latitude`, `longitude` and `radiusMeters` to match the requested area.\n2. (Optional) Set `startTime` to match the requested time at which the search suggestions should apply.\n3. (Optional) Set `geoTypes` list with the requested geo types. Provided types are used to adjust the ranking and filtering of results returned by the service. The supported values are documented in [LocationSuggestionsContext](/search-in-apps/ios/reference/api/swift_reference/Classes/LocationSuggestionsContext#geotypes).\n\n### Swift\n\n[getSearchSuggestions(withOptions:)](/search-in-apps/ios/reference/api/swift_reference/Classes/ContextualSearchRuntime#getsearchsuggestionswithoptions:) \n\n Task {\n let locationContext: LocationSuggestionsContext = {\n let context = LocationSuggestionsContext()\n context.latitude = 40.748595\n context.longitude = -73.992365\n context.radiusMeters = 2500\n context.startTime = try?\n Date.ISO8601FormatStyle.iso8601.parse(\"2024-03-14T19:20:30.000+02:00\")\n context.geoTypes = [\"restaurant\", \"cafe\"]\n return context\n }()\n let suggestions = try await searchRuntime.getSearchSuggestions(\n withOptions: GetSearchSuggestionsOptions(locationContext: [\n locationContext\n ]))\n }\n\n### Objective-C\n\n[getSearchSuggestionsWithOptions](/search-in-apps/ios/reference/api/objc_reference/Classes/ContextualSearchRuntime#-getsearchsuggestionswithoptions:completionhandler:) \n\n\n LocationSuggestionsContext *locationContext;\n locationContext = [[LocationSuggestionsContext alloc] init];\n locationContext.latitude = 40.748595;\n locationContext.longitude = -73.992365;\n locationContext.radiusMeters = @2500;\n [searchRuntime\n getSearchSuggestionsWithOptions:\n [[GetSearchSuggestionsOptions alloc] initWithLocationContext: locationContext]\n completionHandler:^(SearchSuggestions *_Nullable suggestions,\n NSError *_Nullable error) {\n // error will be not null if there is an error.\n // On success, suggestions will be non null.\n }];\n\nDisplay\n-------\n\nTo display search suggestions chips, use the following methods (the style varies\nif you're using UIKit versus SwiftUI). \n\n### Swift UIKit\n\nDisplay search suggestion chips using the\n[SearchSuggestionsViewController](/search-in-apps/ios/reference/api/swift_reference/Classes/SearchSuggestionsViewController). \n\n // Create the SearchSuggestionsViewController with specified display options.\n var searchSuggestionsController = SearchSuggestionsViewController(options: viewOptions)\n ...\n // Add the view controller to the existing view hierarchy\n searchSuggestionsController.addToViewContoller(self)\n view.addSubview(searchSuggestionsController.view)\n ...\n // Assign a specific set of search suggestions to this view controller.\n searchSuggestionsController.searchSuggestions = suggestions\n\n### Objective-C\n\nDisplay search suggestion chips using the\n[SearchSuggestionsViewController](/search-in-apps/ios/reference/api/objc_reference/Classes/SearchSuggestionsViewController). \n\n // Create the SearchSuggestionsController with specified display options.\n SearchSuggestionsViewController *_searchSuggestionsController =\n [[SearchSuggestionsViewController alloc] initWithOptions:_customizationOptions];\n ...\n // Add the view controller to the existing view hierarchy\n [_searchSuggestionsController addToViewController:self];\n [self.view addSubview:_searchSuggestionsController.view];\n ...\n // Assign a specific set of search suggestions to this view controller.\n _searchSuggestionsController.searchSuggestions = suggestions\n\n### SwiftUI\n\nSearch suggestions can be displayed with a\n[SearchSuggestionsView](/search-in-apps/ios/reference/api/swift_reference/Structs/SearchSuggestionsView) \n\n SearchSuggestionsView(suggestions: suggestions, options: options)\n\n[Next: Display search resultsarrow_forward](/search-in-apps/ios/display_search_results)"]]