Skip to main content

Full Text search in MongoDB

Full text search is similar to content search from entire database or from storage where data is located. It is something similar to how we search any content in any search application by entering certain string keywords or phrases and getting back the relevant results sorted by their ranking. This is common requirement in any large data-set application for quick and efficient searching method. 

This post I am sharing about text search from MongoDB database. Text search option is available in almost every database either RDBMS family or NoSQL family.  Mongodb have something different that is ranking (weight of attributes). Starting from version 2.4, MongoDB began with an experimental feature supporting Full-Text Search using Text Indexes. This feature has now become an integral part of the product. The Text Search uses streaming techniques to look for specified words in the string fields by dropping stop words like a, an, the, etc. 

What are the features of  "Mongodb Full Text Search" ?

• Full-text search as an index type when creating new indexes, just like any other.
• Support for advanced queries, similar to the Google search syntax e.g. negation and phrase matching.
• Indexing of multiple fields, with weighting to give different fields higher priority.
Supports 15 different languages.

What are the steps to configure full text search in mongodb ?

1.Enabling Text Search
2.Creating Text Index
3.Using Text Index

Configure or enable full text at database level ?
We need to enable manually in MongoDB 2.4. but later version it is by default enable when we install.
in MongoDB configuration file specify with:

setParameter=textSearchEnabled=true
or 
>db.adminCommand({setParameter:true,textSearchEnabled:true})

For this we need to create index for all possible attributes like below

db.collection_name.createIndex(
    {
        company_name: 'text',
        job_description: 'text',
        job_title: 'text',
    },
    {
        weights: {
            company_name: 10,
            job_description: 5,
            job_title: 1
        }
    }
)

Here weight is to specify the ranking of text in result set.


Now use this syntax to search from collection

db. collection_name.find( { $text: { $search: "engineer" } } )

for OR condition
db. collection_name.find( { $text: { $search: "engineer", $search: "mobile" } } )

for AND condition
db. collection_name.find({$text :{$search: "engineer android mobile -developer"}

Now enjoy the quick search and efficient from big data in your application.

Thanks for reading
Plz dont forget to like Facebook Page..
https://www.facebook.com/pages/Sql-DBAcoin/523110684456757

Comments

Post a Comment

Plz dont forget to like Facebook Page..
https://www.facebook.com/pages/Sql-DBAcoin/523110684456757

Popular posts from this blog

How to encrypt and decrypt Table data in postgres

For encrypting and decrypting , we must use the bytea data type on the column which we implement. Bcoz bytea will use the pgcrypto method by default. However, you will need to create the pgcrypto extension to enable these functions as they are not pre-defined in PostgreSQL/PPAS. Example CREATE EXTENSION pgcrypto; CREATE TABLE userinfo (username varchar(20), password bytea); >>    Inserting the data in an encrypted format INSERT INTO userinfo VALUES(' suman ',encrypt('111222','password','aes')); select * from userinfo ; >>    Retrieving the data as decrypted format SELECT decrypt(password,decode('password','escape'::text),'aes'::text) FROM userinfo; Thanks for reading Plz dont forget to like Facebook Page.. https://www.facebook.com/pages/Sql-DBAcoin/523110684456757

How to recover msdb database from suspect mode

 It was Monday 9 th Jun 47 degr. temperature of Delhi-NCR. Temperature was like boiling me and database. When I reached my office( @ 8.45 am) got an alert from one of Server. “MSDB is in suspected mode” At the same time comes in my mind, this issue will boil me today.. I just tried to cool my self through cold drink then connected server from my local system using windows authentication mode..

SQL71562: external references are not supported when creating a package from this platform

Last week I got this error from one of developer who was trying to deploy his project from Testing server to SQL Azure QA server. He was using “Deploy Database to SQL Azure” option from SSMS Tool-Task option. After connecting to SQL Azure portal when operation started to deployment below errors occurs. Validation of the schema model for data package failed. Error SQL71562: Error validating element xx.xxx.xx:function .dbo.xxx has an unresolved refrence to object xx.dbo.xxxx external refrences are not supported when creating a package from this platform . Reason: The reason of the this error was; some functions of project was dependent on master database and only single database was being deploy to SQL Azure. DACFx must block Export when object definitions (views, procedures, etc.) contain external references, as Azure SQL Database does not allow cross-database external references So, this error was coming. Solution : I suggested him to create those function to locally