Swiftpack.co - meilisearch/meilisearch-swift as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by meilisearch.
meilisearch/meilisearch-swift v0.7.0
Lightning Fast, Ultra Relevant, and Typo-Tolerant Search Engine Client written in Swift
⭐️ 64
🕓 42 weeks ago
.package(url: "https://github.com/meilisearch/meilisearch-swift.git", from: "v0.7.0")

MeiliSearch-Swift

MeiliSearch Swift

MeiliSearch | Documentation | Slack | Roadmap | Website | FAQ

GitHub Workflow Status License Bors enabled

⚡ The MeiliSearch API client written for Swift 🍎

MeiliSearch Swift is the MeiliSearch API client for Swift developers.

MeiliSearch is an open-source search engine. Discover what MeiliSearch is!

Table of Contents

📖 Documentation

For more information about this API see our Swift documentation.

For more information about MeiliSearch see our Documentation or our API References.

🔧 Installation

With Cocoapods

CocoaPods is a dependency manager for Cocoa projects.

MeiliSearch-Swift is available through CocoaPods. To install it, add the following line to your Podfile:

pod 'MeiliSearch'

Then, run the following command:

pod install

This will download the latest version of MeiliSearch pod and prepare the xcworkspace.

With the Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, adding MeiliSearch-Swift as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/meilisearch/meilisearch-swift.git", from: "0.12.0")
]

🏃‍♀️ Run MeiliSearch

There are many easy ways to download and run a MeiliSearch instance.

For example, using the curl command in your Terminal:

#Install MeiliSearch
curl -L https://install.meilisearch.com | sh

# Launch MeiliSearch
./meilisearch --master-key=masterKey

NB: you can also download MeiliSearch from Homebrew or APT or even run it using Docker.

🎬 Getting Started

To do a simply search using the client, you can create a Swift script like this:

Add documents

    import MeiliSearch

    // Create a new client instance of MeiliSearch.
    let client = try! MeiliSearch(host: "http://localhost:7700")

    struct Movie: Codable, Equatable {
        let id: Int
        let title: String
        let genres: [String]
    }

    let movies: [Movie] = [
        Movie(id: 1, title: "Carol", genres: ["Romance", "Drama"]),
        Movie(id: 2, title: "Wonder Woman", genres: ["Action", "Adventure"]),
        Movie(id: 3, title: "Life of Pi", genres: ["Adventure", "Drama"]),
        Movie(id: 4, title: "Mad Max: Fury Road", genres: ["Adventure", "Science Fiction"]),
        Movie(id: 5, title: "Moana", genres: ["Fantasy", "Action"]),
        Movie(id: 6, title: "Philadelphia", genres: ["Drama"])
    ]

    let semaphore = DispatchSemaphore(value: 0)

    // An index is where the documents are stored.
    // The uid is the unique identifier to that index.
    let index = client.index("movies")

    // If the index 'movies' does not exist, MeiliSearch creates it when you first add the documents.
    index.addDocuments(
        documents: movies,
        primaryKey: nil
    ) { result in
        switch result {
        case .success(let update):
            print(update) // => Update(updateId: 0)
        case .failure(let error):
            print(error.localizedDescription)
        }
        semaphore.signal()
      }
    semaphore.wait()

With the updateId, you can check the status (enqueued, processing, processed or failed) of your documents addition using the update endpoint.

Basic Search


let semaphore = DispatchSemaphore(value: 0)

// Typealias that represents the result from MeiliSearch.
typealias MeiliResult = Result<SearchResult<Movie>, Swift.Error>

// Call the function search and wait for the closure result.
client.index("movies").search(SearchParameters( query: "philoudelphia" )) { (result: MeiliResult) in
    switch result {
    case .success(let searchResult):
        dump(searchResult)
    case .failure(let error):
        print(error.localizedDescription)
    }
    semaphore.signal()
}
semaphore.wait()

Output:

 MeiliSearch.SearchResult<SwiftWork.(unknown context at $10d9e7f3c).Movie>
  ▿ hits: 1 element
    ▿ SwiftWork.(unknown context at $10d9e7f3c).Movie
      - id: 6
      - title: "Philadelphia"
      ▿ genres: 1 element
        - "Drama"
  - offset: 0
  - limit: 20
  - nbHits: 1
  ▿ exhaustiveNbHits: Optional(false)
    - some: false
  - facetsDistribution: nil
  - exhaustiveFacetsCount: nil
  ▿ processingTimeMs: Optional(1)
    - some: 1
  ▿ query: Optional("philoudelphia")
    - some: "philoudelphia"

Since MeiliSearch is typo-tolerant, the movie philadelphia is a valid search response to philoudelphia.

🤖 Compatibility with MeiliSearch

This package only guarantees the compatibility with the version v0.24.0 of MeiliSearch.

💡 Learn More

The following sections may interest you:

⚙️ Development Workflow and Contributing

Any new contribution is more than welcome in this project!

If you want to know more about the development workflow or want to contribute, please visit our contributing guidelines for detailed instructions!

📜 Demos

To try out a demo you will need to go to its directory in Demos/. In that directory you can either:

  • Open the SwiftPM project in Xcode and press run, or
  • Run swift build or swift run in the terminal.

Vapor

Please check the Vapor Demo source code here.

Perfect

Please check the Perfect Demo source code here.


MeiliSearch provides and maintains many SDKs and Integration tools like this one. We want to provide everyone with an amazing search experience for any kind of project. If you want to contribute, make suggestions, or just know what's going on right now, visit us in the integration-guides repository.

GitHub

link
Stars: 64
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Dependencies

Release Notes

0.12.0 🕊
8 weeks ago

This package version is compatible with MeiliSearch v0.24.0 🎉

⚠️ Breaking changes

  • Changes related to the next MeiliSearch release (v0.24.0) (#224)

Errors API changes

MeiliSearchApiError interface receives an update in the fields naming. Error fields are replaced by the following:

  • errorCode -> code
  • errorLink -> link
  • errorType -> type
  struct MSErrorResponse: Decodable, Encodable, Equatable {
    public let message: String
    public let code: String
    public let type: String
    public let link: String?
  }

When an update fails, previously the error fields in the update body were also namederrorCode, errorLink, ... Now error fields are moved inside the error with the same MSErrorResponse type.

  /// Result type for the Update.
  public struct Result: Codable, Equatable {
    /// ...
    /// Error information in case of failed update.
    public let error: MeiliSearch.MSErrorResponse?
  }

Thanks everyone!

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics