Text Search

Atlas Full-Text Search

MongoDB Atlas Full-Text Search Indexes leverage Apache Lucene topower rich text search with features like language analysis andscoring.

Visit Atlas Full-Text Searchto learn more. You can use the Atlas promotional codeMONGODB4DOT2 for $200 of Atlas credit. For information onredeeming Atlas credit, see Atlas Billing.

Overview

MongoDB supports query operations that perform a text search of stringcontent. To perform text search, MongoDB uses atext index and the $text operator.

Note

Views do not support text search.

Example

This example demonstrates how to build a text index and use it to findcoffee shops, given only text fields.

Create a collection stores with the following documents:

  1. db.stores.insert(
  2. [
  3. { _id: 1, name: "Java Hut", description: "Coffee and cakes" },
  4. { _id: 2, name: "Burger Buns", description: "Gourmet hamburgers" },
  5. { _id: 3, name: "Coffee Shop", description: "Just coffee" },
  6. { _id: 4, name: "Clothes Clothes Clothes", description: "Discount clothing" },
  7. { _id: 5, name: "Java Shopping", description: "Indonesian goods" }
  8. ]
  9. )

Text Index

MongoDB provides text indexes to supporttext search queries on string content. text indexes can include anyfield whose value is a string or an array of string elements.

To perform text search queries, you must have atext index on your collection. A collection can only have onetext search index, but that index can cover multiple fields.

For example you can run the following in a mongo shell toallow text search over the name and description fields:

  1. db.stores.createIndex( { name: "text", description: "text" } )

$text Operator

Use the $text query operator to perform text searches on acollection with a text index.

$text will tokenize the search string using whitespace and mostpunctuation as delimiters, and perform a logical OR of all suchtokens in the search string.

For example, you could use the following query to find all storescontaining any terms from the list “coffee”, “shop”, and “java”:

  1. db.stores.find( { $text: { $search: "java coffee shop" } } )

Exact Phrase

You can also search for exact phrases by wrapping them in double-quotes.If the $search string includes a phrase and individual terms, text searchwill only match documents that include the phrase.

For example, the following will find all documents containing“coffee shop”:

  1. db.stores.find( { $text: { $search: "\"coffee shop\"" } } )

For more information, see Phrases.

Term Exclusion

To exclude a word, you can prepend a “-” character. For example, tofind all stores containing “java” or “shop” but not “coffee”, use thefollowing:

  1. db.stores.find( { $text: { $search: "java shop -coffee" } } )

Sorting

MongoDB will return its results in unsorted order by default. However,text search queries will compute a relevance score for each documentthat specifies how well a document matches the query.

To sort the results in order of relevance score, you must explicitlyproject the $meta textScore field and sort on it:

  1. db.stores.find(
  2. { $text: { $search: "java coffee shop" } },
  3. { score: { $meta: "textScore" } }
  4. ).sort( { score: { $meta: "textScore" } } )

Text search is also available in the aggregation pipeline.

Language Support

MongoDB supports text search for various languages. SeeText Search Languages for a list of supportedlanguages.