I have one problem, I have collection and I want to set text search index to 2 fields(description and title). But when I add second index I get following error and text search stopped working.
{ "serverUsed" : "localhost/127.0.0.1:27017" , "ok" : 0.0 , "errmsg" : "too many text index for: testdb.users"}
when I delete one index search start work again. what is the problem? One collections support full text search index only for one field????
I am using the current version of mongodb under windows and I am using mongodb java driver API.
Thanks
To create a text based index on a key, use command
db.collectionName.ensureIndex({'textColumnName': 'text'})
. After this index is applied, use the search commands to search for a word i.e.db.collectionName.find({$text: {$search:'your text here'}})
. There is a text score based on which the results are ranked, to see it project it in the score key like this :db.collectionName.find({$text: {$search:'your text here'}}, {score: {$meta: 'textScore'}}).sort({score: {$meta: 'textScore'}})
.If we create a text index on the
title
field of themovies
collection, and then perform the text searchdb.movies.find( { $text : { $search : "Big Lebowski" } } )
. The following documents will be returned, assuming they are in the movies collection:{ "title" : "The Big Lebowski" , star: "Jeff Bridges" }
{ "title" : "Big" , star : "Tom Hanks" }
{ "title" : "Big Fish" , star: "Ewan McGregor" }
This is because, there will be a ***logical OR***ing on
Big
&Lebowski
.MongoDB only allows one text-index per collection.
But you can use a text-index which spans multiple fields:
That way you will get results when the phrase you are searching for is found in either. When this is not what you want, like when you have two search-queries which each return results from one of the fields but not the other, you have two options.
_id
of the original document.