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
Swift client for the Meilisearch API
โญ๏ธ 77
๐Ÿ•“ 29 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. Learn more about Meilisearch.

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.14.1")
]

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 simple 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 task):
            print(task) // => Task(uid: 0, status: "enqueued", ...)
        case .failure(let error):
            print(error.localizedDescription)
        }
        semaphore.signal()
      }
    semaphore.wait()

With the uid of the task, you can check the status (enqueued, processing, succeeded 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
  - estimatedTotalHits: 1
  - facetDistribution: 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 compatibility with the version v0.28.0 of Meilisearch.

:warning: This package also can work with the version v0.29.0 of Meilisearch, but you may notice some missing features. Check the issues page for more information.

๐Ÿ’ก Learn more

The following sections in our main documentation website may interest you:

โš™๏ธ 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: 77
Last commit: 2 days ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Dependencies

Release Notes

0.14.1 ๐Ÿ•Š
15 weeks ago

๐ŸŽ‰ This release adds support to Linux environments. Let us know if there is something not working ;)

๐Ÿš€ Enhancements

  • Improve Docker configuration in the package (#304) @brunoocasali
  • Add Linux support (#245) (#292) @bidoubiwa co-authored by @brunoocasali

๐Ÿ’… Misc

  • Added the try keyword in front of MeiliSearch() constructor in the code samples (#325) @mgregoire254

Thanks again to @aronbudinszky, @bidoubiwa, @brunoocasali, and @mgregoire254! ๐ŸŽ‰

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