Mastico, the new gem by Lean Panda!

Matteo Manzo
LeanPanda
Published in
4 min readApr 9, 2018

--

When it comes to developing a search system for our projects, we often have to deal with complex situations: we might need to cross-search on more fields, in more than one language and maybe with full-text search

In this case, our approach is often called Elasticseach.

Elasticsearch is a highly scalable open-source full-text search and analytics engine. It allows you to store, search, and analyze big volumes of data quickly and in near real time. It is generally used as the underlying engine/technology that powers applications that have complex search features and requirements.

ES is a powerful and complex system that manages to handle large amounts of data. As we might expect, it is not a plug-and-play solution so we rarely use it on his own, but luckily it is plenty of gems that we can use to ease our life.

One of these is called Chewy.

What is Chewy?

Chewy is an high-level framework based on the elasticsearch-ruby client.

Chewy simplifies how we manage an index (a collection of models that share similar attributes, which we can interrogate to get our search results).

Let’s make some examples using our search system implemented for the Uffizi website:

If we wanted to look for Botticelli’s Venus we could easily look for “venus”:

As we see, we receive only one result, the exact one, but what if there were more paintings with “Venus” in the title and we only wanted the painting by Botticelli?

In this case, we can concatenate multiple queries:

The result remains the same, but we may have noticed that if we wanted to make more specific queries to filter the results, we will write several lines of code.

Is not there a smarter way to do it? Yes. ES provides multi_match which allows you to do more searches on more fields, but let’s see how it behaves:

While we first wanted to filter Venus and only Botticelli’s Venus, we now have all the paintings that include “Venus” in the title and all the authors that contain “Botticelli” in the name. The opposite has happened: we have an OR instead of an AND.

Surely there will be dozens of ways to achieve our goal with Chewy, and for this, we decided to create a helper that can make our life easier every time we use this Toptal’s gem.

Welcome, Mastico!

Mastico helps us simplify the interface for building queries and provides us with a basic configuration of Chewy so that, once installed, we can immediately start doing our research!

This first query is yes longer than that of Chewy, but our purpose is to cross multiple attributes, and then we try to see how it should be the query that returns only the Botticelli’s Venus:

We got what we wanted and the query is just slightly longer than the previous one, but the even better thing is that we just need to add another field in the fields array to look inside more attributes.

Let’s see in detail what is running when we launch this command, or what we should have done by hand with ES: https://gist.github.com/mttmanzo/a7ffb82c312a3b3f4d027fb681e49ef6.

Once fields andquery are passed, Mastico starts to concatenate the search with other values such as the type of search (:word,:prefix, :infix and:fuzzy) and the boost (how much we want to emphasize that word.)

This is enough to make us start to implement even complex research in a simple and fast way. But what if I was wrong to write the word?

Mastico manages this eventuality automatically with the fuzzy type, so if we look for “Botticello” we would still find Botticelli’s works.

Nice, but what if I want to filter the “stop-word”? There is a solution to this, just pass the word_weight attribute to the query:

The returned values represent the boost, which can also be used to emphasize the search for other keywords.

All these options may seem complex to match, but in reality, it is possible to concatenate them in a simple hash:

The features we have just seen are of enormous help and help us daily in many of our projects, but we can’t wait to get feedback from external users and more ideas to improve Mastico! Whoever would like to contribute, obviously, can do it here: https://github.com/cantierecreativo/mastico.

Originally published at www.leanpanda.com on April 9, 2018.

--

--