اقتراحات البحث

الحصول على عمليات البحث المقترَحة

للحصول على اقتراحات البحث، يمكنك استدعاء طريقة 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 أيضًا اقتراحات البحث المستندة إلى الموقع الجغرافي. أنشئ عنصر GetSearchSuggestionsOptions باستخدام LocationSuggestionsContext.

اتّبِع الخطوات التالية لإنشاء فئة LocationSuggestionsContext:

  1. (مطلوبة) اضبط latitude وlongitude وradiusMeters لمطابقة المنطقة المطلوبة.
  2. (اختياري) اضبط السمة startTime لمطابقة الوقت المطلوب الذي يجب أن تنطبق فيه اقتراحات البحث.
  3. (اختياري) اضبط قائمة 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)

الخطوة التالية: عرض نتائج البحث