ค้นหาคำแนะนำ

รับการค้นหาที่แนะนำ

หากต้องการรับคำแนะนำในการค้นหา ให้เรียกใช้เมธอด getSearchSuggestions ใน ContextualSearchRuntime เมธอดนี้ใช้ GetSearchSuggestionsOptions รายการเดียว ซึ่งคุณสามารถระบุข้อความที่จะใช้เพื่อรับคำแนะนำการค้นหาได้

ออบเจ็กต์ SearchSuggestions ที่แสดงไม่มีพร็อพเพอร์ตี้ที่เข้าถึงได้ ซึ่งมีข้อมูลที่จำเป็นทั้งหมดในการแสดงคำแนะนำในการค้นหา แต่จะไม่มีข้อมูลอื่น

Swift ใช้รูปแบบการทำงานพร้อมกันของ Swift ส่วน Objective-C ใช้ฟังก์ชัน Callback

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 list ด้วยประเภททางภูมิศาสตร์ที่ขอ ระบบจะใช้ประเภทที่ระบุ เพื่อปรับการจัดอันดับและการกรองผลลัพธ์ที่บริการแสดง ค่าที่รองรับจะบันทึกไว้ใน 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)

ถัดไป: แสดงผลการค้นหา