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 a uid and contains the following information:

  • One primary key
  • 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 moviesIndexes - 图1 (opens new window). You would probably want to keep multiple types of documents, such as movies, TV shows, actors, directors, and more. Each of these categories would be represented by an index in MeiliSearch.

Each index holds information about the fields found in the documents, including how they are handled by MeiliSearch and their order of importance. In addition, each has its own set of 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

Let’s use the add or replace documents endpoint to add documents to a new MeiliSearch instance without an index.

We will create an index called movies. The code below will create the movies index and add a sample document to it.

<>

cURL

JS

Python

PHP

Java

Ruby

Go

Rust

Swift

Dart

  1. curl \
  2. -X POST 'http://localhost:7700/indexes/movies/documents' \
  3. -H 'Content-Type: application/json' \
  4. --data-binary '[{
  5. "id": 287947,
  6. "title": "Shazam",
  7. "poster": "https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg",
  8. "overview": "A boy is given the ability to become an adult superhero in times of need with a single magic word.",
  9. "release_date": "2019-03-23"
  10. }]'
  1. client.index('movies').addDocuments([{
  2. id: 287947,
  3. title: 'Shazam',
  4. poster: 'https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg',
  5. overview: 'A boy is given the ability to become an adult superhero in times of need with a single magic word.',
  6. release_date: '2019-03-23'
  7. }])
  1. client.index('movies').add_documents([{
  2. 'id': 287947,
  3. 'title': 'Shazam',
  4. 'poster': 'https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg',
  5. 'overview': 'A boy is given the ability to become an adult superhero in times of need with a single magic word.',
  6. 'release_date': '2019-03-23'
  7. }])
  1. $client->index('movies')->addDocuments([
  2. [
  3. 'id' => 287947
  4. 'title' => 'Shazam',
  5. 'poster' => 'https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg',
  6. 'overview' => 'A boy is given the ability to become an adult superhero in times of need with a single magic word.',
  7. 'release_date' => '2019-03-23'
  8. ]
  9. ]);
  1. client.index("movies").addDocuments("[{"
  2. + "\"id\": 287947,"
  3. + "\"title\": \"Shazam\","
  4. + "\"poster\": \"https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg\","
  5. + "\"overview\": \"A boy is given the ability to become an adult superhero in times of need with a single magic word.\","
  6. + "\"release_date\": \"2019-03-23\""
  7. + "}]"
  8. );
  1. client.index('movies').add_documents([
  2. {
  3. id: 287947,
  4. title: 'Shazam',
  5. poster: 'https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg',
  6. overview: 'A boy is given the ability to become an adult superhero in times of need with a single magic word.',
  7. release_date: '2019-03-23'
  8. }
  9. ])
  1. documents := []map[string]interface{}{
  2. {
  3. "id": 287947,
  4. "title": "Shazam",
  5. "poster": "https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg",
  6. "overview": "A boy is given the ability to become an adult superhero in times of need with a single magic word.",
  7. "release_date": "2019-03-23",
  8. },
  9. }
  10. client.Index("movies").AddDocument(documents)
  1. let progress: Progress = movies.add_or_replace(&[
  2. Movie {
  3. id: 287947,
  4. title: "Shazam".to_string(),
  5. poster: "https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg".to_string(),
  6. overview: "A boy is given the ability to become an adult superhero in times of need with a single magic word.".to_string(),
  7. release_date: "2019-03-23".to_string(),
  8. }
  9. ], None).await.unwrap();
  1. let path = Bundle.main.url(forResource: "movies", withExtension: "json")
  2. let documents: Data = Data(contentsOf: path)
  3. client.index("movies").addDocuments(documents: documents) { (result: Result<Update, Swift.Error>) in
  4. switch result {
  5. case .success(let update):
  6. print(update)
  7. case .failure(let error):
  8. print(error)
  9. }
  10. }
  1. await client.index('movies').addDocuments([
  2. {
  3. 'id': 287947,
  4. 'title': 'Shazam',
  5. 'poster':
  6. 'https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg',
  7. 'overview':
  8. 'A boy is given the ability to become an adult superhero in times of need with a single magic word.',
  9. 'release_date': '2019-03-23'
  10. }
  11. ]);

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.

  1. {
  2. "uid": "movie",
  3. "createdAt": "2019-11-20T09:40:33.711324Z",
  4. "updatedAt": "2019-11-20T10:16:42.761858Z"
  5. }

Primary key

An index is a collection of documents. All documents have a primary key, which is a mandatory field. This field is composed of a primary key 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 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, suppose your first document lists attributes in the following order:

  1. id, title, description, release_date

A document containing matches in its title field will be considered more relevant than a document only containing matches in its description.

You can alter the order in which ranking rules take effect, or define custom ranking rules to return certain results first.

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.

Learn more about synonyms

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 (for example, the or of in English).

Words added to the stop words list will be ignored during search. In addition to improving relevancy, designating common words as stop words also greatly improves performance.

For example, suppose you want to search for the great gatsby. You would prefer to receive documents containing the terms great gatsby, rather than documents containing the terms the great, or just the. In this case, adding the to the stop word list would improve performance and make search results more relevant.

Learn more about stop words

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.

Learn more about field properties