MeiliSearch: Next generation search API
Tags: python, showdev, tutorial, productivity
Published: 2022-01-20 14:49:51

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n9lo10cdv23exdhodgv9.png)
 


For a long time, medium-sized datasets and smaller applications have gone through the nightmare of using insufficient search engines, which result in hidden costs in terms of user experience and retention due to poor search fulfillment.There are numerous search engines available on the internet, both open-source and commercial. Choosing the ideal search solution for your project is crucial, but it's also complicated. In this article, i am going to discuss why Meilisearch is the suitable choice among other search engines.

## **What is MeiliSearch?**🤔
**MeiliSearch** is a powerful, fast, open-source, easy to use and deploy search engine. It is a REST-based search API. It intends to be a ready-to-use solution for everyone who wishes to give a speedy and relevant search experience to their end-users.

- Created by Meili SAS company based in paris in 2018
- It is an open source search engine

It promises:

- Blazingly fast and relevant search experience for the end user
- Is scalable, mantainable, customizable and that is easy to get it setup for developers.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ohn7tqylqwvkxa8rwflr.png)
 
 

## **Why MeiliSearch ?**
It is a free, open-source solution that can be used by anybody and is tailored to meet a variety of needs. It requires very little setting upon installation, but it is highly customizable. With features like typo correction, filters, custom ranks, and more, it gives an immediate search experience.

## **Features of MeiliSearch**💡

- **_Search as you type_** - Also known as "quick search." While you're still typing in your query, you'll get results. When you type more text into the search box, the displayed results alter in real time.

- **_Ultra relevant_** - MeiliSearch's default relevancy rules are designed to give a simple search experience with minimal setup. They can be tinkered with to get the best results for your dataset.

- **_Typo tolerant_** - MeiliSearch will always find the results you're looking for, rather than letting typos ruin your search.

- **_Synonyms_** - Synonyms allow you to make your search experience more personalized and intuitive.

- **_Highlighting_** - Highlight query phrases to make matches stand out. It is not necessary for users to read the complete paragraph in order to find a match.

- **_Filters_** - MeiliSearch allows you to apply filters to your search results to reduce them down based on user-defined criteria.

- **_Faceting_** - It aids in the classification of search results and the creation of user-friendly navigation interfaces.

- **_Sorting_** - By sorting search results at query time, users can pick which sorts of results they want to see first.

- **_Placeholder search_** - If you search without any query words, MeiliSearch will return all of the documents in that index, organized by its custom ranking rules.

- **_Phrase search_** - MeiliSearch will only return documents that contain your search phrases in the order you specify if you use double quotes (") around them. As a result, users will be able to conduct more precise searches.

- **_API key management_** - The usage of API keys in MeiliSearch allows you to secure your instances. You may control which users have access to specific indexes, routes, and endpoints using API keys.

- **_Comprehensive language support_** - MeiliSearch is a multilingual search engine! Every language spoken in the global community will be supported.

## **Module's Architecture**
Divided into two main parts:

- **Service**, which servers as a wrapper for MeiliSearch PHP library. It's purpose is making API calls to the MeiliSearch server.
- **Search API backend plugin** extends the functionality of the search API module and provides a backend on which you can attach indexes.

## **Demo of SearchAPI MeiliSearch**

### **Installation**
There are many ways to [install and run meilisearch](https://docs.meilisearch.com/learn/getting_started/installation.html#download-and-launch). Be sure to check them out. 

**_NB:_** you can also download MeiliSearch from Homebrew or APT or cURL or others. The link on how to install using all these ways is attached.

In this article, i will install using the pip3 commandline:

```bash
$ pip3 install meilisearch
```

### **Getting Started : MeiliSearch Python** 
#### **Add Documents**

```python
import meilisearch

client = meilisearch.Client('http://127.0.0.1:7700', 'masterKey')

# An index is where the documents are stored.
index = client.index('books')

documents = [
  { 'book_id': 123,  'title': 'Pride and Prejudice' },
  { 'book_id': 456,  'title': 'Le Petit Prince' },
  { 'book_id': 1,    'title': 'Alice In Wonderland' },
  { 'book_id': 1344, 'title': 'The Hobbit' },
  { 'book_id': 4,    'title': 'Harry Potter and the Half-Blood Prince' },
  { 'book_id': 42,   'title': 'The Hitchhiker\'s Guide to the Galaxy' }
]

# If the index 'books' does not exist, MeiliSearch creates it when you first add the documents.
index.add_documents(documents) # => { "updateId": 0 }
```

#### **Basic Search**
```python
# MeiliSearch is typo-tolerant:
index.search('harry pottre')
```

Output
```bash
{
  "hits" => [{
    "book_id" => 4,
    "title" => "Harry Potter and the Half-Blood Prince"
  }],
  "offset" => 0,
  "limit" => 20,
  "processingTimeMs" => 1,
  "query" => "harry pottre"
}
```

#### **Custom Search**
```python
index.search(
  'prince',
  {
    'attributesToHighlight': ['title'],
    'filters': 'book_id > 10'
  }
)
```
Output
```bash
{
    "hits": [
        {
            "book_id": 456,
            "title": "Le Petit Prince",
            "_formatted": {
                "book_id": 456,
                "title": "Le Petit <em>Prince</em>"
            }
        }
    ],
    "offset": 0,
    "limit": 20,
    "processingTimeMs": 10,
    "query": "prince"
}
```

#### **Module compatibility**
- meilisearch - v0.18.1

### **Take away**
MeiliSearch comes with a lot of customizing options. These customizing choices, unlike those offered by other search engines, are only that: optional. Try out MeiliSearch now. Thank me later.

Feel free to connect with me on [Twitter](https://twitter.com/Emma_Donery) or [LinkedIn](https://www.linkedin.com/in/emma-donery-0831a7188/). Please feel free to leave a feedback on the comment section.