सुझाई गई खोजें पाना
खोज के सुझाव पाने के लिए, ContextualSearchRuntime
पर getSearchSuggestions
वाले तरीके को कॉल करें. इस तरीके में सिर्फ़ 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 का इस्तेमाल किया जा रहा है, तो इन तरीकों का इस्तेमाल करें.
स्विफ़्ट यूआईकिट
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)