MongoDB Full Text Search

Source: Internet
Author: User

The full-text index sets up each word in an indexed, indicating the number and location of occurrences of the word in the article, and when the user queries, the retrieval program searches based on the pre-established index and feeds the results back to the user's search method.

This process is similar to the process of looking up a word in a dictionary using the search Word table.

MongoDB supports full-text indexing starting with version 2.4 and currently supports full-text indexes in 15 languages (Chinese is temporarily not supported).

    • Danish
    • Dutch
    • 中文版
    • Finnish
    • French
    • German
    • Hungarian
    • Italian
    • Norwegian
    • Portuguese
    • Romanian
    • Russian
    • Spanish
    • Swedish
    • Turkish
Enable full-Text indexing

MongoDB turns on full-text indexing by default after version 2.6, and if you use previous versions, you need to use the following code to enable full-text indexing:

>db.  Admincommand({setparameter:true,textsearchenabled:True})  

or use the command:

--setparameter textsearchenabled=true  
To create a full-text index

Consider the following posts collection of document data, including the article content (Post_text) and tags (tags):

{   "Post_text":"Enjoy the MongoDB articles on w3cschool.cc","tags":[" MongoDB "," W3cschool "]}           

We can create a full-text index of the Post_text field so that we can search for content within the article:

>db.  Posts.  Ensureindex({post_text:"text"})         
Using Full-Text indexing

Now that we have a full-text index on Post_text, we can search for the keyword w3cschool.cc in the article:

>db.  Posts.  Find({$text: {$search:"w3cschool.cc"}})      

The following command returns the following document data containing the W3CSCHOOL.CC keyword:

{    "_ID" : ObjectId("53493d14d852429c10000002"), "Post_text" : "Enjoy the MongoDB articles on w3cschool.cc", "Tags" : [ "MongoDB", "W3cschool" ]}{  "_id" : objectid ( " 53493d1fd852429c10000003 "),  " Post_text ":  "writing tutorials on w3cschool.cc" ,< Span class= "PLN" >  "tags"  : [   "MongoDB" ,  "tutorial"  ] }       /span>                

If you are using an older version of MongoDB, you can use the following command:

>db.  Posts.  RunCommand("text", {search:"w3cschool.cc"})      

Using full-text indexing can improve search efficiency.

To delete a full-text index

To delete a full-text index that already exists, you can use the Find command to find the index name:

>db.  Posts.  Getindexes()     

Get the index name from the above command, the index name of this example is Post_text_text, execute the following command to delete the index:

>db.  Posts.  Dropindex("Post_text_text")       

MongoDB Full Text Search

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.