הצעות חיפוש

הצעות לחיפוש

כדי לקבל הצעות לחיפוש, מבצעים קריאה לשיטה 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)

השלב הבא: הצגת תוצאות חיפוש