Giter Site home page Giter Site logo

dichotommy / meilisearch-swift Goto Github PK

View Code? Open in Web Editor NEW

This project forked from meilisearch/meilisearch-swift

0.0 0.0 0.0 1.41 MB

Swift client for the Meilisearch API

Home Page: https://www.meilisearch.com/

License: MIT License

Swift 98.77% Dockerfile 0.09% Shell 0.54% Ruby 0.60%

meilisearch-swift's Introduction

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", 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.