InstantSearchClient 7.0.5

InstantSearchClient 7.0.5

vladislavRobert Mogos 维护。



  • Algolia

Build Status Carthage compatible SwiftPM Compatible CocoaPods CocoaPods Swift 4.0 Objective-C compatible

Algolia Search API Client for Swift and Objective-C

Algolia Search 是一个基于云的全文本、数值和分面搜索引擎,能够在第一次按键时提供实时结果。使用 Algolia Search API Client 可以轻松地从 Swift 代码中获取Algolia Search REST API

顺便说一下,这个库与 Objective-C 100% 兼容

作为本指南的补充,您可以浏览自动生成的 参考文档。(也请参阅离线版本。)

API 文档

您可以在Algolia 网站上找到完整的参考

目录

  1. 支持的平台

  2. 安装

  3. 快速入门

  4. 寻求帮助

入门

支持的平台

我们的Swift客户端支持在iOS、macOS、tvOS和watchOS上运行,且可以从Swift和Objective-C中使用。

安装

Swift 5.0

  1. 在InstantSearchClient上添加依赖。
    • CocoaPods:在Podfile中添加pod 'InstantSearchClient', '~> 7.0'
    • Carthage:在Cartfile中添加github "algolia/algoliasearch-client-swift" ~> 7.0.0
    • SwiftPM:在Package.swift中的包依赖数组中添加.package(url:"https://github.com/algolia/algoliasearch-client-swift", from: "7.0.0"),然后添加InstantSearchClient到目标依赖中。
  2. 在源文件中添加import InstantSearchClient

Swift 4.2

  1. 在InstantSearchClient上添加依赖。
    • CocoaPods:在Podfile中添加pod 'InstantSearchClient', '~> 6.0'
    • Carthage:在Cartfile中添加github "algolia/algoliasearch-client-swift" ~> 6.0.0
    • SwiftPM:在Package.swift中包依赖数组中添加.package(url:"https://github.com/algolia/algoliasearch-client-swift", from: "6.0.0"),然后添加InstantSearchClient到目标依赖中。
  2. 在源文件中添加import InstantSearchClient

Swift 4.1

  1. 在InstantSearchClient上添加依赖。
    • CocoaPods:在Podfile中添加pod 'InstantSearchClient', '~> 5.0'
    • Carthage:在Cartfile中添加github "algolia/algoliasearch-client-swift" ~> 5.0.0
    • SwiftPM:在Package.swift中包依赖数组中添加.package(url:"https://github.com/algolia/algoliasearch-client-swift", from: "5.0.0"),然后添加InstantSearchClient到目标依赖中。
  2. 在源文件中添加import InstantSearchClient

快速入门

本快速入门教程将向您展示如何在30秒内索引和搜索对象。

初始化客户端

首先,您需要初始化客户端。为此,您需要您的 应用程序IDAPI密钥。您可以在您的 Algolia 账户 中找到它们。

let client = Client(appID: "YourApplicationID", apiKey: "YourAPIKey")

推送数据

在没有任何先前配置的情况下,您可以使用以下代码在 contacts 索引中索引 500 个联系人

// Load content file
let jsonURL = Bundle.main.url(forResource: "contacts", withExtension: "json")
let jsonData = try! Data(contentsOf: jsonURL!)
let dict = try! JSONSerialization.jsonObject(with: jsonData!)

// Load all objects in the JSON file into an index named "contacts".
let index = client.index(withName: "contacts")
index.addObjects(dict["objects"])

搜索

现在您可以使用姓名、姓氏、公司等信息(即使是拼写错误)来搜索联系人

// search by firstname
index.search(Query(query: "jimmie"), completionHandler: { (content, error) -> Void in
	if error == nil {
		print("Result: \(content)")
	}
})
// search a firstname with typo
index.search(Query(query: "jimie"), completionHandler: { (content, error) -> Void in
	if error == nil {
		print("Result: \(content)")
	}
})
// search for a company
index.search(Query(query: "california paint"), completionHandler: { (content, error) -> Void in
	if error == nil {
		print("Result: \(content)")
	}
})
// search for a firstname & company
index.search(Query(query: "jimmie paint"), completionHandler: { (content, error) -> Void in
	if error == nil {
		print("Result: \(content)")
	}
})

配置

可以根据调整搜索行为。例如,您可以将按粉丝数量排序的定制排序添加到内置相关性之上。

let customRanking = ["desc(followers)"]
let settings = ["customRanking": customRanking]
index.setSettings(settings, completionHandler: { (content, error) -> Void in
	if error != nil {
		print("Error when applying settings: \(error!)")
	}
})

您还可以根据重要程度配置要按顺序索引的属性列表(最前面 = 最重要)

注意:由于该引擎设计为在您输入时提示结果,因此您通常将按前缀搜索。在这种情况下,属性的顺序对于决定哪个结果最佳非常重要。

let customRanking = ["lastname", "firstname", "company", "email", "city", "address"]
let settings = ["searchableAttributes": customRanking]
index.setSettings(settings, completionHandler: { (content, error) -> Void in
	if error != nil {
		print("Error when applying settings: \(error!)")
	}
})

笔记

之前的 Objective-C API 客户端

2015年7月,我们发布了 Swift 客户端的新版本,该版本支持 Swift 和 Objective-C 的协同工作。从3.0版本开始(2016年4月),Swift 成为 Swift 和 Objective-C 项目的参考实现。《Objective-C API 客户端》目前已不再进行活跃开发。它仍然会修复错误,但不会增加新功能。如果你之前使用的是我们的 Objective-C 客户端,请阅读从 Objective-C 迁移到 Swift API 客户端的迁移指南

迁移指南

如果你之前使用的是我们的 Swift 客户端 2.x 版本,请阅读升级到 3.x 版本的迁移指南

Swift 3

你可以通过以下方式之一使用 Swift 库:

  • pod 'AlgoliaSearch-Client-Swift', '~> 4.8.1'
  • pod 'AlgoliaSearch-Client-Swift', :git => 'https://github.com/algolia/algoliasearch-client-swift.git', :branch => 'swift-3'

获取帮助