MongoDB full text search index: error: too many te

2019-04-28 11:18发布

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

2条回答
Emotional °昔
2楼-- · 2019-04-28 12:00

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.

查看更多
该账号已被封号
3楼-- · 2019-04-28 12:20

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.
查看更多
登录 后发表回答