AlgoliaSearchClient

9.0.0-beta.5

⚡️ A fully-featured and blazing-fast Swift API client to interact with Algolia.
algolia/algoliasearch-client-swift

What's New

9.0.0-beta.5

2024-06-21T08:54:47Z

9.0.0-beta.5

9.0.0-beta.4

9.0.0-beta.3

9.0.0-beta.2

9.0.0-beta.1

9.0.0-alpha.5

9.0.0-alpha.4

9.0.0-alpha.3

9.0.0-alpha.2

9.0.0-alpha.1

9.0.0-alpha.0

Algolia for Swift

The perfect starting point to integrate Algolia within your Swift project

DocumentationCommunity ForumStack OverflowReport a bugFAQSupport

✨ Features

  • Pure cross-platform Swift client
  • Typed requests and responses
  • Widespread use of Result type
  • Uses the power of Codable protocol for easy integration of your domain models
  • Thread-safe clients
  • Detailed logging
  • Injectable HTTP client

Install

Swift Package Manager

The Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies. Since the release of Swift 5 and Xcode 11, SPM is compatible with the iOS, macOS and tvOS build systems for creating apps.

To use SwiftPM, you should use Xcode 11 to open your project. Click File -> Swift Packages -> Add Package Dependency, enter the client repo's URL.

If you're a framework author and use Swift API Client as a dependency, update your Package.swift file:

let package = Package(
    // 9.0.0 ..< 10.0.0
    dependencies: [
        .package(url: "https://github.com/algolia/algoliasearch-client-swift", from: "9.0.0-alpha.0")
    ],
    // ...
)

Import the Core package and the required client package to your source code files:

#if canImport(Core)
    import Core
#endif
import Search // or Ingestion, for example

Cocoapods

CocoaPods is a dependency manager for Cocoa projects.

To install Algolia Swift Client, simply add the following line to your Podfile:

pod 'AlgoliaSearchClient', '~> 9.0.0-alpha.0'
# pod 'InstantSearchClient', '~> 7.0' // Swift 5.9 NOT YET RELEASED

Then, run the following command:

$ pod update

Carthage

Carthage is a simple, decentralized dependency manager for Cocoa.

  • To install InstantSearch, simply add the following line to your Cartfile:
github "algolia/algoliasearch-client-swift" ~> 9.0.0-alpha.0
  • Launch the following commands from the project directory (for v.8.0+)
carthage update
./Carthage/Checkouts/algoliasearch-client-swift/carthage-prebuild
carthage build

If this is your first time using Carthage in the project, you'll need to go through some additional steps as explained over at Carthage.

💡 Getting Started

Initialize the client

To start, you need to initialize the client. To do this, you need your Application ID and API Key. You can find both on your Algolia account.

let client = SearchClient(applicationID: applicationID, apiKey: apiKey)

Push data

Without any prior configuration, you can start indexing contacts in the contacts index using the following code:

struct Contact: Encodable {
  let firstname: String
  let lastname: String
  let followers: Int
  let company: String
}

let contacts: [Contact] = [
  .init(firstname: "Jimmie", lastname: "Barninger", followers: 93, company: "California Paint"),
  .init(firstname: "Warren", lastname: "Speach", followers: 42, company: "Norwalk Crmc")
]

for contact in contacts {
    let saveObjRes = try await client.saveObject(indexName: "contacts", body: contact)
    _ = try await client.getTask(indexName: "contacts", taskID: saveObjRes.taskID)
}

Search

You can now search for contacts by firstname, lastname, company, etc. (even with typos):

let searchParams = SearchParamsObject(query: "Jimmy")

let res = try await client.searchSingleIndex(indexName: "contacts", searchParams: .searchParamsObject(searchParams))

dump(res.hits[0])

Configure

Settings can be customized to tune the search behavior. For example, you can add a custom sort by number of followers to the already great built-in relevance:

let indexSettings = IndexSettings(customRanking: ["desc(followers)"])
try await client.setSettings(indexName: "contacts", indexSettings: indexSettings)

You can also configure the list of attributes you want to index by order of importance (first = most important):

Note: Since the engine is designed to suggest results as you type, you'll generally search by prefix. In this case the order of attributes is very important to decide which hit is the best:

let indexSettings = IndexSettings(searchableAttributes: ["lastname", "firstname", "company"])
try await client.setSettings(indexName: "contacts", indexSettings: indexSettings)

For full documentation, visit the Algolia's API Clients Automation documentation.

📝 Examples

You can find code samples in the Algolia's API Clients Automation guides.

Contributing to this repository

The Algolia API clients are automatically generated, you can find everything here https://github.com/algolia/api-clients-automation

📄 License

Algolia Swift API Client is an open-sourced software licensed under the MIT license.

Notes

Objective-C support

The Swift API client is compatible with Objective-C up to version 7.0.5. Please use this version of the client if you're working with an Objective-C project.

Swift 3

You can use this library with Swift by one of the following ways:

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

Swift 4

You can use the old library with Swift by one of the following ways:

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

Getting Help

  • Need help? Ask a question to the Algolia Community or on Stack Overflow.
  • Encountering an issue? Before reaching out to support, we recommend heading to our FAQ where you will find answers for the most common issues and gotchas with the client.
  • Found a bug? You can open a GitHub issue.

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

Last updated: Wed Nov 20 2024 08:24:39 GMT-1000 (Hawaii-Aleutian Standard Time)