खोज के लिए सुझाव पाना
खोज के सुझाव पाने के लिए, getSearchSuggestions तरीके को ContextualSearchRuntime पर कॉल करें. इस तरीके में, एक GetSearchSuggestionsOptions होता है. इसमें, खोज के सुझाव पाने के लिए इस्तेमाल किए जाने वाले टेक्स्ट के बारे में बताया जा सकता है.
जवाब में मिले SearchSuggestions ऑब्जेक्ट में, ऐक्सेस की जा सकने वाली कोई प्रॉपर्टी नहीं है. इसमें खोज के सुझाव दिखाने के लिए ज़रूरी सभी जानकारी होती है. हालांकि, इसके अलावा यह जानकारी नहीं देता.
Swift, Swift Concurrency स्टाइल का इस्तेमाल करता है. वहीं, Objective-C, कॉलबैक फ़ंक्शन का इस्तेमाल करता है.
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.
}];
जगह की जानकारी के आधार पर सुझाई गई खोजें पाना
getSearchSuggestions अनुरोध में, जगह के हिसाब से खोज के सुझाव भी मिलते हैं. LocationSuggestionsContext की मदद से, GetSearchSuggestionsOptions ऑब्जेक्ट बनाएं.
LocationSuggestionsContext क्लास बनाने के लिए, यह तरीका अपनाएं:
- (ज़रूरी है) अनुरोध किए गए इलाके से मेल खाने के लिए,
latitude,longitude, औरradiusMetersसेट करें. - (ज़रूरी नहीं)
startTimeको उस समय के हिसाब से सेट करें जिस समय खोज के सुझावों को लागू किया जाना चाहिए. - (ज़रूरी नहीं) अनुरोध किए गए जियो टाइप के साथ
geoTypesकी सूची सेट करें. इन टाइप का इस्तेमाल, सेवा से मिले नतीजों की रैंकिंग और फ़िल्टर करने के लिए किया जाता है. इस्तेमाल की जा सकने वाली वैल्यू के बारे में 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.
}];
डिसप्ले
खोज के सुझाव वाले चिप दिखाने के लिए, इन तरीकों का इस्तेमाल करें. अगर UIKit के बजाय SwiftUI का इस्तेमाल किया जा रहा है, तो स्टाइल अलग-अलग होती है.
Swift UIKit
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
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
खोज से जुड़े सुझावों को SearchSuggestionsView के साथ दिखाया जा सकता है
SearchSuggestionsView(suggestions: suggestions, options: options)