Search indexes

Create MongoDB Search indexes, see which ones you have and change or drop them.

Create the indexes that $search and $vectorSearch use, see which ones you have and change or drop them. They live on the same nodes as the data and see the writes confirmed before each search.

Requirements

  • A user that can create search indexes on the collection.

Create

JavaScript
db.articles.createSearchIndex({
  name: "text",
  definition: { mappings: { dynamic: false, fields: { title: { type: "string", analyzer: "lucene.english" } } } },
});

List, change and drop

JavaScript
db.articles.getSearchIndexes();
db.articles.updateSearchIndex("text", { mappings: { dynamic: true } });
db.articles.dropSearchIndex("text");

Check

In mongosh, getSearchIndexes() shows the index with queryable: true once it can answer the searches of $search.

Common problems

  • The definition is refused when the index is created if an analyzer is not valid, for example with minGram 0. The error says which part fails.

See also