π The text
Operator
The text
operator is used to perform a full-text search using the analyzer that you specify in the index configuration. It is used to search for words or phrases in the full-text fields of your documents.
The text
operator uses the OR
operator to combine the search terms. For example, if you search for Joy of Cooking
, the search will return all documents that contain either Joy
, of
, Cooking
, or a combination of those.
This can sometimes be the desired outcome, but it might also return a lot of undesired results. Those documents containing the word of
, for example, are most likely not relevant to your search.
We've already covered the path
property, but there are other properties that you can use with the text
operator.
The full definition of the text operator goes as follows. More details can be found in the documentation.
{
$search: {
"index": <index name>, // optional, defaults to "default"
"text": {
"query": "<search-string>",
"path": "<field-to-search>",
"fuzzy": <options>,
"score": <options>,
"synonyms": "<synonyms-mapping-name>"
}
}
}
Let's take a look at each of these properties.
The fuzzy
propertyβ
Fuzzy search is a technique that allows you to search for terms that are similar to the one you are looking for. It is useful when your users might make typos or spelling mistakes in their search queries.
There are many options you can use with the fuzzy
property, and you can find out more in the docs. For now, we'll focus on the maxEdits
option. MaxEdits will specify the number of one-character edits that can be made to find a match.
For example, if you search for Alice
, you should find the book Alice in Wonderland. However, if you make one, or multiple typos β such as Alyse
, Alise
, or Allice
β you will not find the book. This is where the maxEdits
option comes in handy. With a maxEdits of one, you will find the book even if you make one typo.
The synonyms
propertyβ
The synonyms property allows you to specify a mapping of synonyms that will be used in your search. For example, if you search for car
, you might want to find documents that contain the word automobile
as well. You can specify a mapping of synonyms in your index configuration and then use the synonyms
property in your search query to use that mapping.