Meilisearch Vapor is a thin wrapper around the official Meilisearch for Swift API client, designed for compatibility with Vapor (a server-side Swift framework).
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 MeiliSearchVapor as a dependency is as easy as adding it to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/sherlouk/meilisearch-vapor.git", from: "0.1.0")
]
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.
To do a simple insertion using the client, you can create a Swift script like this:
import MeiliSearchVapor
// in your Vapor startup:
func configure(app: Application) throws {
app.meilisearch.configure(host: "http://localhost:7700")
}
// in your Vapor route:
app.post("addDocuments") { req -> EventLoopFuture<String> in
let promise = req.eventLoop.makePromise(of: String.self)
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"])
]
// An index is where the documents are stored.
// The uid is the unique identifier to that index.
let index = req.meilisearch.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", ...)
promise.succeed(String(describing: task))
case .failure(let error):
promise.fail(error)
}
}
return promise.futureResult
}