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