Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to perform a full text search in MongoDB and Mongoose

I'm searching on Google since days and I tried many things but I still can not perform a good full text search on my user collection.

I tried ElasticSearch but was pretty impossible to query and paginate...

I tried many plugins for Mongoose like ElMongo, mongoose-full-text, Mongoosastic, etc... everyone are really bad documented and I don't know how to perform a good full text search.

So, my collection is a normal collection:

user = {   name: String,   email: String,   profile: {     something: String,     somethingElse: String   } } 

I have a search input in a page with a simple POST, if I type hello world what I need is to search on the entire collection fields the matching words of my search query and get the results.

It will be really nice also to have options to handle a pagination like 10 items per page or something...

What is the best solution to achieve this? I'm using MongoDB 2.6.* with Mongoose, NodeJS and ExpressJS.

Thanks.

like image 976
Ayeye Brazo Avatar asked Feb 27 '15 21:02

Ayeye Brazo


People also ask

Is MongoDB good for full-text search?

While MongoDB's full-text search features may not be as robust as those of some dedicated search engines, they are capable enough for many use cases. Note that there are more search query modifiers — such as case and diacritic sensitivity and support for multiple languages — within a single text index.

What is best for full-text search?

Top-4 Most Interesting Full-Text Search Solutions in 2021ClickHelp. Sphinx. Elasticsearch. Algolia.

How do I search for text in a field in MongoDB?

In MongoDB, we can perform text search using text index and $text operator. Text index: MongoDB proved text indexes that are used to find the specified text from the string content. Text indexes should be either a string or an array of string elements.

How do I search for a word in MongoDB?

For on-premises (non-Atlas) deployments, MongoDB's text search capability supports query operations that perform a text search of string content. To perform text search, MongoDB uses a text index and the $text operator.


1 Answers

You can add a text index to your Mongoose schema definition that lets you use the $text operator in your find queries to search all fields included in the text index.

To create an index to support text search on, say, name and profile.something:

var schema = new Schema({   name: String,   email: String,   profile: {     something: String,     somethingElse: String   } }); schema.index({name: 'text', 'profile.something': 'text'}); 

Or if you want to include all string fields in the index, use the '$**' wildcard:

schema.index({'$**': 'text'}); 

This would enable you to performed a paged text search query like:

MyModel.find({$text: {$search: searchString}})        .skip(20)        .limit(10)        .exec(function(err, docs) { ... }); 

For more details, read the full MongoDB Text Indexes documentation.

like image 132
JohnnyHK Avatar answered Sep 21 '22 13:09

JohnnyHK