MQL language

In this post, we will learn:

Introduction

Have you ever filtered products or created queries in the MERGADO Editor on the Products page?

image

Or maybe you’ve targeted parameter values within element path?

If you answered “yes” to at least one of the above questions, then you can confidently say that you have encountered MQL.

What is MQL?

MQL stands for MERGADO Query Language. It is a language created by us (MERGADO), which is used to write conditions.

What is a condition (in MQL)?

When we filter products on the Products page, we filter by condition. So, we enter the condition according to which it should be filtered, and after searching for products, we will only see those products that meet the condition we entered.

:information_source:  In the MERGADO Editor, there are 2 ways to display conditions, and both ways are in the end in MQL:

1. A simple query form, also called Oneliner, displays the condition on one line with three fields for clicking on the condition.



2. The condition can also be displayed as plain text. For example, the form for Custom MQL query displays the condition in this way.

image

This display of the condition can also occur in different paths to the element.

Both ways of displaying the condition are equivalent. So, for example, if I create the condition ITEM_ID = 43 in Oneliner or write ITEM_ID = 43 in the form for Custom MQL query, the result after searching for products will be identical.

Since MQL is a language, MQL, like ordinary languages, has its own vocabulary (terms that can be used in conditions) and grammar (a set of rules for how conditions are written).

MQL vocabulary and grammar

In this section we will explain all the possibilities and limitations of MQL. In the course, we will use a lot of comparisons with the English language - the condition in MQL has certain similarities with a sentence in the English language.

The condition consists of 3 parts

The condition is the basic building block of MQL and consists of 3 parts:

Screenshot from 2023-06-06 13-07-55

In the 1st part we write path to element - in the English language we can imagine subject.

In the 2nd part we write operator - in the English language we can imagine verb. We have the following operators available:

Operator Meaning
CONTAINS contains the text
NOT CONTAINS does not contain the text
= is equal to
!= is not equal to
~ contains the regular expression
!~ does not contain the regular expression
< is less
<= is less than or equal to
> is greater
>= is greater than or equal to
IN is contained in the list
NOT IN is not contained in the list

In the 3rd part, either a value (numerical/textual), a path to an element, or a regular expression is written - in the English language we can imagine, for example, object. Text value and regular expression must be enclosed in quotation marks, e.g. "t-shirt". The numerical value doesn’t need to be enclosed in quotation marks.

image

:information_source:  The quotes must be the "straight" ones and not the “crooked” ones, otherwise the condition will not be valid. The crookedness of the quotation marks can be caused, for example, by the formatting of different editors and browsers in the case that we copy the given path to the element from somewhere (for example, from our forum).

pasted image 0 (3)

The first part of the condition tells us “where we look” within the products — we look at the element (and its values) targeted in the given path. The 2nd and 3rd parts of the condition together tell us “how we look” at the element targeted in the 1st part of the condition — we look through such “glasses” that allow us to see only those values of the element that satisfy the condition. E.g., with the condition ITEM_ID = 43, only such products are searched that have a value equal to 43 in the element ITEM_ID.

In the 1st and 3rd parts, when using the path to the element, the elements must be written in square brackets [ ], if the names of the given elements contain other characters than letters without diacritics and numbers, e.g. a space. Exceptions are special attributes and Oneliner.

random

Connecting conditions

Conditions can be connected (just as sentences can be joined) using conjunctive operators (similarly in English, conjunctions). There are 2 such operators in MQL:

  • AND, which means “and at the same time”,
    e.g. ITEM_ID = 43 AND DESCRIPTION CONTAINS "t-shirt" means that we are looking for products that have an ITEM_ID equal to 43 and a DESCRIPTION containing the text “t-shirt”, so the product must meet both conditions.

    image

  • OR, which means “or”,
    e.g. ITEM_ID = 43 OR DESCRIPTION CONTAINS "t-shirt" means that we are looking for products that have an ITEM_ID equal to 43 or a DESCRIPTION containing the text “t-shirt”, so it is enough that the product meets only one of the conditions.

    image

Let’s illustrate the difference with an example. Let’s have the following data:

If we type ITEM_ID = 43 AND DESCRIPTION CONTAINS "t-shirt" into the form, no product will be found because a product with ITEM_ID equal to 43 does not have a DESCRIPTION containing the text “t-shirt”.

And if we type ITEM_ID = 43 OR DESCRIPTION CONTAINS "t-shirt" in the form, we get 2 products.

:information_source:  Both normal and conjunctive operators can be written in lower case, e.g. “and” or “not in”. However, the convention is to write them in capital letters.

Grouping conditions

What if we wanted to filter for products that have “t-shirt” in their description or have parameters Color and Size? Then we write the conditions as follows:

image

:bulb: For clarity, we recommend writing conditions with multiple conjunctions and groupings similarly or directly as they are in the previous image. Just avoid writing all conditions on one line. This will make the conditions easier to read.

This adds round brackets ( ) to our vocabulary. We group conditions that are to be evaluated together into round brackets. Because if we had written the conditions above without the round brackets,

we don’t know whether the middle condition PARAM | PARAM_NAME = "Color" belongs to the OR operator (before the condition) or to the AND operator (after the condition). Thus, either we want to search for a product with DESCRIPTION containing “T-shirt” or having the parameter Color and the product must also have the parameter Size, or we want to search for a product with DESCRIPTION containing “T-shirt” or having both parameters Color and Size at the same time.

:information_source:  The conditions in the Oneliner or in the form for Custom MQL query together form a query.

Practical facts about MQL

  1. Within MQL, it is also possible to work with basic mathematical operations (addition +, subtraction -, multiplication * and division /).

    For example image or image

  2. In MQL, the NOT operator can work alone. Used to negate a condition or query. E.g. ITEM_ID != 43 is the same as NOT ( ITEM_ID = 43 ).

    image

    The operator NOT will mainly be used as a negation for more complicated queries, when we have created a query and want to find the exact opposite of the entered query. Simply put the entire query in the round brackets of the NOT operator.

    ezgif.com-gif-maker

  3. The creation of conditions is very similar to propositional calculus.

Syntax errors

Just as you can make spelling or grammatical errors in English, you can make syntax errors in MQL. If we make a syntax error in our writing, MERGADO Editor usually warns us about it. As an example: a condition with a wrong operator or value:

:bulb: Read the post dedicated to errors in forms for Custom MQL query.

What is MQL for?

Creating queries

As has already been said a few times throughout this post, MQL is used to write conditions that we then use on the Products page to filter products and for creating their queries, which we then use in the rules. Using MQL we can define the conditions we need. More advanced knowledge of MQL opens up unlimited possibilities for working with data.

Usage in Element-Path

We also use conditions (written in MQL) as a refinement component in conditions in the path to the element.

:information_source:  There are conditions in the MQL and conditions in the element path. These are two different things in meaning even though they are called the same thing. The condition in MQL consists of 3 parts, as we described above, and the condition in the element path begins and ends with curly brackets { }. A condition in an element path can have multiple conditions in MQL.

Screenshot from 2023-06-06 10-24-55

We can practically put anything from MQL into the condition in the path to the element - connected conditions, grouped conditions, basic mathematical operations or multiple combinations of connected and grouped conditions. Just anything.

Usage examples

In this section, we will show you examples from real practice from our users.

Description is empty

We want to filter products that have a DESCRIPTION element without values. In Oneliner, the condition will look like this:

image

In the form for Custom MQL query as follows:

And if we wanted to target the DESCRIPTION element in the rule, which has no value, the path would be as follows: DESCRIPTION { @@VALUE = "" }. We used special attribute @@VALUE in the path. However, we do not recommend this procedure, as it is many times more efficient to create a query.

The category does not contain any text and at the same time the Type parameter contains a rectangle

We want to filter products that contain the value “shower” in the CATEGORYTEXT element and whose Type parameter contains the value “rectangle”. In Oneliner, the condition will look like this:

And in the form for Custom MQL query like this:

image

Second category

In the Rewrite rule we want to target the second value of the CATEGORY element nested under the CATEGORIES element. In the path to the second value, we use the @@POSITION special attribute:

More examples

We also present other examples from practice.

Display Oneliner:

Form for Custom MQL query:

In the rules:

  • IMAGES | IMAGE { @@POSITION = 4 }
  • VARIANTS | VARIANT | STOCK | WAREHOUSES | WAREHOUSE { NAME = "Amoresa" } | VALUE
  • PARAM { PARAM_NAME = "Producer" } | VAL

Summary

  • What is MQL?
    MQL is an abbreviation for MERGADO Query Language. Conditions are written using MQL.
     
  • What is a condition (in MQL)?
    The condition is the basic building block of MQL and consists of 3 parts:

    Screenshot from 2023-06-06 13-07-55

    In the 1st part you write the path to the element, in the 2nd part you write the operator and in the 3rd part you write either a value (numeric/textual), the path to the element, or a regular expression.
     
  • What can be done with conditions and MQL?
    • Conditions can be joined using the AND and OR joining operators.
    • Conditions can be grouped using round brackets. This way we know which conditions are to be evaluated together.
    • Within MQL it is possible to work with basic mathematical operations (addition +, subtraction -, multiplication * and division /), and even negation (using the NOT operator).
       
  • What are the conditions and MQL for?