A brief introduction of MongoDB Textsearch

Source: Internet
Author: User

MongoDB of Textsearch Introduction  

MongoDB supports text search operations on text content, which provides indexing text index and query operation $text to complete the textual search function. Let's take a simple example to experience the full-text search functionality that MongoDB provides.

1. Create a new blogs collection and insert the following document.

Db.blogs.insert ({_id:1,title: "MongoDB text Search", content: "This was a simple MongoDB text search Introduction"})

Db.blogs.insert ({_id:2,title: "MongoDB text Index", content: "This is AE MongoDB text index Introduction"})

Db.blogs.insert ({_id:3,title: "MongoDB text Operators", content: "This is AE MongoDB text query Introduction"})

2. Create text Index.

Only collection with text index supports full-text search;

Each collection can have only one text index;

The Text index can contain any field of type string, string array type;

Text index can contain multiple fields;

Execute the new text index statement as follows

Db.blogs.ensureIndex ({title: "Text", Content: "Text"})

3. Perform a simple full-text search

Db.blogs.find ({$text: {$search: "Index"}})

4. Query for records containing index or operators

Db.blogs.find ({$text: {$search: "Index Operators"}})

5. Query for records that contain MongoDB but do not contain search

Db.blogs.find ({$text: {$search: "Mongodb-search"}})

6. Query for records that contain text search phrases

Db.blogs.find ({$text: {$search: "\" Text Search\ ""}})

7. Use the Reorder search results

By default, the results returned by the full-text search are unordered;

Each full-text search MongoDB calculates a relative score for each document based on the degree of matching of the documents;

MongoDB provides $meta Textscore to support full-text search scores;

Db.blogs.find ({$text: {$search: "MongoDB Index"}}, {score:{$meta: "Textscore"}}). sort ({score:{$meta: "Textscore"}})

A brief introduction of MongoDB Textsearch

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.