Obtén búsquedas sugeridas
Para obtener sugerencias de búsqueda, llama al método getSearchSuggestions en ContextualSearchRuntime. Este método toma un solo
GetSearchSuggestionsOptions
en el que puedes especificar el texto que se usará para obtener sugerencias de búsqueda.
El objeto SearchSuggestions que se muestra no contiene ninguna propiedad accesible. Contiene toda la información necesaria para renderizar sugerencias de búsqueda, pero, de lo contrario, es opaco.
Swift usa el estilo de simultaneidad de Swift, y Objective-C usa una función de devolución de llamada.
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.
}];
Obtén búsquedas sugeridas basadas en la ubicación
La solicitud getSearchSuggestions también admite sugerencias de búsqueda basadas en la ubicación. Compila un GetSearchSuggestionsOptions objeto con un
LocationSuggestionsContext.
Sigue estos pasos para compilar la clase LocationSuggestionsContext:
- (Obligatorio) Establece
latitude,longitudeyradiusMeterspara que coincidan con el área solicitada. - (Opcional) Establece
startTimepara que coincida con la hora solicitada en la que se deben aplicar las sugerencias de búsqueda. - (Opcional) Establece la lista
geoTypescon los tipos geográficos solicitados. Los tipos proporcionados se usan para ajustar la clasificación y el filtrado de los resultados que muestra el servicio. Los valores admitidos se documentan en 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
Para mostrar chips de sugerencias de búsqueda, usa los siguientes métodos (el estilo varía si usas UIKit en comparación con SwiftUI).
Swift UIKit
Muestra chips de sugerencias de búsqueda con el 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
Muestra chips de sugerencias de búsqueda con el 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
Las sugerencias de búsqueda se pueden mostrar con una SearchSuggestionsView.
SearchSuggestionsView(suggestions: suggestions, options: options)