Press enter or click to view image in full size
Our team is currently in the middle of developing Pigeon Post, a new type of transactional email service that aims to make it easy for developers to common emails without setting up any sort of infrastructure.
As part of our dashboard and API we wanted developers to be able to quickly search for emails by their tags, properties, or even content. We started looking into Algolia, which is a hosted site search engine, however it felt too expensive to sustain as our company grew. That’s when we discovered MeiliSearch — completely open source, written in Rust, and extremely fast. We could host this on the existing Loophole Labs infrastructure and scale without worrying.
However, there was one problem remaining: how do we get the data we store in our MongoDB into MeiliSearch? It felt ridiculous to have to call the MeiliSearch indexing operations every time we wanted to add, update, or delete a document from MongoDB, so we went about creating a better solution.
Introducing MongoMeili, the easiest way to synchronize MongooseJS Collections with MeiliSearch indexes. I think the best way to introduce this is to just show you how it works:
Install MongoMeili
Installing MeiliMongo is super easy, you can user npm or yarn:
npm install mongomeiliyarn add mongomeili
Using MongoMeili
In standard Loophole Labs fashion, we’re keeping things simple. To use MongoMeili simply import it into your Mongoose Schema definition and set it up as a plugin:
// ES6
import mongoose from 'mongoose';
import mongomeili from 'mongomeili';// ES5
const mongoose = require('mongoose');
const mongomeili = require('mongomeili');
// Add the '{ meiliIndex: true }' property to index these attributes with MeiliSearch
const MovieSchema = new mongoose.Schema({
title: { type: String, required: true, meiliIndex: true },
director: { type: String, required: true, meiliIndex: true },
year: { type: String, required: true, meiliIndex: true }
});
// Specify your MeiliSearch credentials
MovieSchema.plugin(mongomeili, {
host: 'https://...'
apiKey: '...',
indexName: '...' // Will get created automatically if it doesn't exist already
})
And that’s it! Now, whenever a document is created, update, or deleted by Mongoose the information will automatically be synced up to your MeiliSearch instance. Morever, we’ve gone ahead and provided the following helper functions that will get injected into your Mongoose Models:
Model.syncWithMeili(): Promise
Index the existing collection into your MeiliSearch index — useful if you’re starting to use MongoMeili in the middle of your project.
Model.clearMeiliSearchIndex(): Promise
Clear the MeiliSearch index and set _meiliIndex = false.
Model.setMeiliIndexSettings(settings): Promise
Set one or more settings of the MeiliSearch index.
Model.meiliSearch({ query, params?, populate? }): Promise
Search your MeiliSearch index for a specific query. You can customize the search parameters and populate information not indexed from the MongoDB collection as well.
This is my favourite one by far. You can search for your indexed documents and then retrieve any extra information that you didn’t index from the MongoDB — all without any extra work.
Did we mention it’s Open Source? Check out our work at https://github.com/loophole-labs/mongomeili.