Skip to content

Lightning Fast, Ultra Relevant, and Typo-Tolerant Search Engine Client written in Swift

License

Notifications You must be signed in to change notification settings

doxa-labs/meilisearch-swift

Β 
Β 

Repository files navigation

MeiliSearch-Swift

MeiliSearch Swift

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

πŸƒβ€β™€οΈ 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.
    client.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.

About

Lightning Fast, Ultra Relevant, and Typo-Tolerant Search Engine Client written in Swift

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 98.9%
  • Other 1.1%