MongoDB full text search index: error: too many text index for, why?
Asked Answered
L

2

8

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

Launder answered 22/12, 2013 at 20:35 Comment(4)
You have more than one text index, currently you can only have one, what you want is this link: docs.mongodb.org/manual/tutorial/…Chivy
Problem is, then I add second text index to the same collection, full text search stop work and I get that error. Seems for multiple fields text search not working.Launder
Can you show us an example of where the multiple fields is not working? You should be able to ensure one index across multiple fieldsChivy
Maybe this: #18386353 (in the comments)Protuberance
V
9

MongoDB only allows one text-index per collection.

But you can use a text-index which spans multiple fields:

db.collection.ensureIndex( {
    description: "text",
    title: "text"
} );

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.

  1. use a multi-field text index, but discard the results which come from the wrong field on the application layer.
  2. extract one of the two fields to a different collection. The documents in that collection could either contain full copies, redacted copies or just the field you index and the _id of the original document.
Victoria answered 23/12, 2013 at 4:14 Comment(5)
Thanks it works! De facto one index for multiple fields is true, I tried to add one index for one field whats why mongo fails. Thanks.Launder
Hi, I'm semi-new to node and I don't know where to put this syntax. Where is 'db' coming from?Morphine
@Morphine The syntax used in this question and answer applies to the mongodb command-line shell.Victoria
Do you know why they have this limit? I tried searching around, but I haven't found a reason, just statements that the limit exists.Tuberculin
This seems like a pretty big limitation. It needs to get fixed because I may have to rethink my choice of DBs in the future.Gladygladys
H
2

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 the movies collection, and then perform the text search db.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.

Hew answered 5/9, 2016 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.