# Chapter 2: Fine-tuning search results
Meilisearch is designed to offer a great search experience out of the box, but sometimes you need to customize it to better fit your needs. You can alter the search behavior for each index using the settings
object.
For this chapter, we will be using a collection of movies as our dataset.
To follow along, first click this link to download the file: movies.json. Then, move it into your working directory and run the following command:
curl \
-X POST 'http://127.0.0.1:7700/indexes/movies/documents' \
-H 'Content-Type: application/json' \
--data-binary @movies.json
If you have already downloaded the dataset and created the movies
index, you can skip to the next section.
# Ranking rules
Meilisearch sorts search responses based on an array of rules called rankingRules
, which is part of the settings
object.
The order of the rankingRules
array matters: the first rule has the most impact and the last rule has the least. This is the default order:
"rankingRules": [
"words",
"typo",
"proximity",
"attribute",
"sort",
"exactness",
]
You can read more about each ranking rule and what it does in our dedicated guide.
In this example, we change the order of the ranking rules for the movies
index by moving exactness
to the top:
curl \
-X POST 'http://localhost:7700/indexes/movies/settings/ranking-rules' \
-H 'Content-Type: application/json' \
--data-binary '[
"exactness",
"words",
"typo",
"proximity",
"attribute",
"sort",
"release_date:asc",
"rank:desc"
]'
You can read more about ranking rules in our dedicated guide.
# Displayed attributes
By default, all attributes are displayed in search results but the displayedAttributes
array in the settings
object allows you to change that.
If you only want to display the title
, poster
, and overview
for the movies
index:
curl \
-X POST 'http://localhost:7700/indexes/movies/settings/displayed-attributes' \
-H 'Content-Type: application/json' \
--data-binary '[
"title",
"overview",
"poster"
]'
You can read more about displayed attributes in our dedicated guide.
# Distinct attribute
If you have groups of almost identical documents, you may wish to only return one of them per search. distinctAttribute
is a tool in the settings
object that allows you to do just that.
When you set one of your attributes as the distinctAttribute
, Meilisearch will not return more than one document with the same value associated to that attribute.
Suppose you have an e-commerce dataset with an index on jackets. There are several identical items with minor variations in color.
[
{
"id": 1,
"description": "Leather jacket",
"brand": "Lee jeans",
"color": "brown",
"product_id": "123456"
},
{
"id": 2,
"description": "Leather jacket",
"brand": "Lee jeans",
"color": "black",
"product_id": "123456"
},
{
"id": 3,
"description": "Leather jacket",
"brand": "Lee jeans",
"color": "blue",
"product_id": "123456"
}
]
If you searched for lee leather jacket
with the default settings, you would get all three documents. But if you set product_id
as the distinctAttribute
, Meilisearch will only return one of those jackets.
You can read more about displayed attributes in our dedicated guide.
# Searchable attributes
Documents in Meilisearch are composed of multiple fields. By default, Meilisearch looks for matches in every field, but the searchableAttributes
array in the settings
object allows you to change that.
For example, if you search the movies
index for 2012
, Meilisearch searches for 2012
in every field: the title
, overview
, release_year
, and so on. If you just want to search in the title
field:
curl \
-X POST 'http://localhost:7700/indexes/movies/settings/searchable-attributes' \
-H 'Content-Type: application/json' \
--data-binary '[
"title"
]'
Meilisearch will now only consider title
during search and you will see fewer results.
NOTE
Meilisearch will still highlight matches in other fields, but they wonβt be used to compute results.
# The order of searchableAttributes
The order of the searchableAttributes
array corresponds to the order of importance of the attributes.
[
"overview",
"title",
]
With the above order, matching words found in the overview
field will have a higher impact on relevancy than the same words found in title
. You can read more about this in our relevancy guide.
# Stop words
Meilisearch allows you to ignore certain words in your search queries by adding them to the stopWords
array. A good example is the word the
in English.
curl \
-X POST 'http://localhost:7700/indexes/movies/settings/stop-words' \
-H 'Content-Type: application/json' \
--data-binary '["the"]'
If you search for the cat
after running the above command, Meilisearch will consider your search query to be cat
, improving the speed and relevancy of your search.
You can read more about stop words in the API reference.
# Synonyms
The synonyms
array lets you associate certain words in your dataset, telling Meilisearch that they are equivalent and interchangeable.
curl \
-X POST 'http://localhost:7700/indexes/movies/settings/synonyms' \
-H 'Content-Type: application/json' \
--data-binary '{
"winnie": ["piglet"], "piglet": ["winnie"]
}'
This will set winnie
and piglet
as synonyms; searching for either word will show the same results.
You can read more about it in our dedicated guide.
The next chapter tackles more advanced topics including security and data backup.