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

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
meilisearch/meilisearch-swift
Lightning Fast, Ultra Relevant, and Typo-Tolerant Search Engine Client written in Swift
.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")
]

πŸƒβ€β™€οΈ Run MeiliSearch

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

For example, if you use Docker:

docker pull getmeili/meilisearch:latest # Fetch the latest version of MeiliSearch image from Docker Hub
docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --master-key=masterKey

NB: you can also download MeiliSearch from Homebrew or APT.

🎬 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.22.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: 53
Last commit: 4 days ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Dependencies

Release Notes

0.10.0 πŸ•Š
4 days ago

Changes

Breaking changes ⚠️

  • Move stats method to index class (#200) @bidoubiwa
  • Move settings methods to index class (#198) @bidoubiwa
  • Move updates method in index class (#197) @bidoubiwa
  • Move search method to index (#196) @bidoubiwa
  • Move documents method to index (#195) @bidoubiwa
  • Make Indexes a public class containing all index related methods (#193) @bidoubiwa

Index is now a public Class required for most of the indexes related methods

In MeiliSearch some actions are for the whole MeiliSearch and other specifically for indexes. For example GET /stats is about the MeiliSearch instance but GET indexes/myIndex/documents is about a specific index.

All specific actions linked to an index are now available through an instance of the Indexes class.

Before

client.addDocuments(documents)

After

Now, these methods are only available in the Indexes class.

let index = client.index("myIndex")
index.getDocuments().

Or

client.index(myIndex").getDocuments()

The following method are still available in a Client instance and return an Index instance:

  • client.index("myIndex")
  • createIndex(uid: "myIndex")
  • getOrCreateIndex(uid: "myIndex")
  • getindex("myIndex")
  • getindexes("myIndex") -> returns an array of Indexes()

Methods moved

Every method related to a specific index are now only available in an Index instance.

Please refer to every related PR to find out.

Parameters label removed/added/renamed

Some methods saw their parameter declaration changed to make the usage more explicit and to add some needed data.

Example: createIndex(UID: String, _ completion: ...) Became: createIndex(uid: String, primaryKey: String? = nil, _ completion: ...)

All required UID labels became uid. As seen in the example above.

Index instance creation does not make an HTTP call

When creating an index instance

let index = client.index("myIndex")

It will not make an HTTP call. Thus the index instance will only have the information about the uid. To complete the information of the Index or just if you need some additional information please use client.getIndex("myIndex"). This will also return an Index instance but with all information related to an index:

  • uid
  • primaryKey
  • createdAt
  • updatedAt

Thanks again to @bidoubiwa ! πŸŽ‰

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