Indexes
An index is an entity that gathers a set of documents with its own settings.
It can be comparable to a table in SQL
, or a collection in MongoDB.
An index is defined by an uid
and contains the following information:
One primary key
An attribute that must be present in every document of a given index, used to identify and distinguish documents.
Example: In a document with the primary field
"id": "Abc_012"
, “id” is the index’s primary key and “Abc_012” is the document’s unique identifier.Default settings that can be configured as needed: relevancy rules, synonyms, stop words, and field properties.
Example
Suppose you manage a database that contains information about movies. You would probably want to have multiple categories: one for movie descriptions, one for actors, one for costumes and one for reviews. Each of these categories would be represented by an index in MeiliSearch.
Each index holds information about the fields found in the documents, how they get handled by MeiliSearch, and their order of importance. Besides, an index defines its own synonyms, relevancy rules, and stop words. The settings of one index don’t impact other indexes.
For example, it means you could create on the same server synonyms for a movie
index and different synonyms for a costumes
index.
Index creation
An index is created the first time documents are added to it or manually using the create index endpoint.
Example
In a new MeiliSearch instance without any index, let’s add documents using the add or replace documents endpoint.
We provide movies
as our index. Because that index was not previously created, using the following code will:
- Create the
movie
index. - Add the documents to it.
cURL
curl \
-X POST 'http://localhost:7700/indexes/movies/documents' \
--data '[{
"id": 287947,
"title": "Shazam",
"poster": "https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg",
"overview": "A boy is given the ability to become an adult superhero in times of need with a single magic word.",
"release_date": "2019-03-23"
}]'
JavaScript
client.index('movies').addDocuments([{
id: 287947,
title: 'Shazam',
poster: 'https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg',
overview: 'A boy is given the ability to become an adult superhero in times of need with a single magic word.',
release_date: '2019-03-23'
}])
Python
client.index('movies').add_documents([{
'id': 287947,
'title': 'Shazam',
'poster': 'https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg',
'overview': 'A boy is given the ability to become an adult superhero in times of need with a single magic word.',
'release_date': '2019-03-23'
}])
PHP
$client->index('movies')->addDocuments([
[
'id' => 287947
'title' => 'Shazam',
'poster' => 'https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg',
'overview' => 'A boy is given the ability to become an adult superhero in times of need with a single magic word.',
'release_date' => '2019-03-23'
]
]);
Ruby
client.index('movies').add_documents([
{
id: 287947,
title: 'Shazam',
poster: 'https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg',
overview: 'A boy is given the ability to become an adult superhero in times of need with a single magic word.',
release_date: '2019-03-23'
}
])
Go
documents := []map[string]interface{}{
{
"id": 287947,
"title": "Shazam",
"poster": "https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg",
"overview": "A boy is given the ability to become an adult superhero in times of need with a single magic word.",
"release_date": "2019-03-23",
},
}
client.Index("movies").AddDocument(documents)
Rust
let progress: Progress = movies.add_or_replace(&[
Movie {
id: 287947,
title: "Shazam".to_string(),
poster: "https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg".to_string(),
overview: "A boy is given the ability to become an adult superhero in times of need with a single magic word.".to_string(),
release_date: "2019-03-23".to_string(),
}
], None).await.unwrap();
Index UID
The uid
is the unique identifier of a given index. It is used on every indexes/:index_uid
route as the :index_uid
parameter.
The uid is set at index creation time. Once a uid
has been defined for an index, you cannot create another index with the same uid
and the identifier cannot be changed anymore.
{
"uid": "movie",
"createdAt": "2019-11-20T09:40:33.711324Z",
"updatedAt": "2019-11-20T10:16:42.761858Z"
}
Primary key
An index is a collection of documents. All documents have a primary key, which is a mandatory field
A field, or a key-value pair, is a set of two data items linked together: an attribute and its associated value.
Ex: "attribute": "value"
. This field is composed of a primary key attribute
An attribute is the name of a field, like a key.
Ex: "title": "Batman"
In the example above, “title” is the attribute.
name and a unique value. All documents in a given index share the same primary key attribute but a different unique value.
The primary key’s attribute name must be known by the index. You can set a primary key for an index or let it be inferred by MeiliSearch.
Learn more about document primary key
Relevancy rules
Each index applies its own relevancy rules. All indexes are created with the same built-in ranking rules
A set of consecutive rules applied to ensure relevancy in search results.
For example, to sort results by number of typos or number of matched query terms in each matching document.
executed in a default order. Once your first document has been added, the index will record how the attributes must be sorted. Their order of importance will be deduced from their order of appearance in the document.
For example, if in your first document attributes are listed as follows: id, title, description, release_date
, any document containing the matching query in title
will be considered more relevant than a document containing it in description
.
On top of that, you can add your custom rules to the ranking rules. For example, you may want to rank your movies either by release date or popularity, or both and so on. Rules are customizable so the results meet your user’s needs as close as possible.
Learn more about ranking rules
Synonyms
In your dataset, you may decide to create synonyms for words which have the same meaning. To do so, a set of synonyms can be defined for an index. Even though they are different, they should be treated similarly. If either of the associated words is searched, the same results shall be displayed.
Since synonyms are linked to a given index, they won’t apply to any other index on the same MeiliSearch instance.
Stop words
Sometimes you may want to ignore certain words in documents and search queries. To do so, a set of stop words can be defined for an index. Unless you actually need them, some words neither add semantic value nor context. Besides, they are often too frequent (i.e., the
or of
in English).
By adding words to a stop words list, these specific terms will be ignored during search. It will avoid documents being considered highly relevant because of the presence of some words in an important attribute or in a good position. This will also greatly improve the response time because all the documents that contain only those words will not be used for documents sorting.
For example, suppose you would perform the following search query: the great gatsby
. Having the word the
in a film review wouldn’t make the review more relevant. By adding the
to the stop word list, performance would be increased and search results more relevant.
Field properties
By default, every document field is searchable and returned on search queries.
Fields can have either or both or none of the following properties that can be modified in the settings:
- Searchable: The content of searchable fields is used by MeiliSearch to assess the relevancy of a document.
- Displayed: Documents returned upon search contain only displayed fields.
By default, each field is stored and this behavior cannot be changed.