Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when using a text index on Mongodb

Tags:

mongodb

I'm trying to play with text index on Mongodb.

I already used a text index on one collection :

db.ensureIndex({field1 : "text"}) 

and it works.

But I retried on another collection and I get the following message :

db.movies.ensureIndex({genres: "text"})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "ok" : 0,
    "errmsg" : "found language override field in document with non-string type",
    "code" : 17261
}

The collection "movies" has a "genres" field which is an array of string. For example :

> db.movies.findOne()
{
    "_id" : ObjectId("51c460fdc30a209dd9621dc4"),
    "genres" : [
            "Crime",
            "Drama"
    ]
    ...
}

This field is present on all documents.

I don't understand this error. Any idea ?

like image 979
Hugo Lassiège Avatar asked Nov 27 '14 22:11

Hugo Lassiège


2 Answers

Your movies collection contains a field "language" with a type not accepted by mongo DB. Make sure "language" is a not null string contains only these values (See Text Search Languages):

da or danish
nl or dutch
en or english
fi or finnish
fr or french
de or german
hu or hungarian
it or italian
nb or norwegian
pt or portuguese
ro or romanian
ru or russian
es or spanish
sv or swedish
tr or turkish
like image 186
anhlc Avatar answered Oct 12 '22 00:10

anhlc


This error occurs when you try to create a text index on a field in a document, and the document has a field named language which is of non string type.

From the docs:

If a collection contains documents or sub-documents that are in different languages, include a field named language in the documents or sub-documents and specify as its value the language for that document or sub-document.

MongoDB will use the specified language for that document or sub-document when building the text index

By default the language in which the text indices are built is English. If a field named language is present in the scope of the document, MongoDb assumes it to be a field specifying the language to be used to build the indexes for that document.

So, to trick MongoDB in such cases where you use a text index and have a field named language which is part of the document and not a field to specify the language for building indexes, you need to set the language_override attribute to some dummy field which doesn't exist. This is to inform MongoDb that, look, you should use English as the default language, and if my document contains a field named dummy, then use the value in that field as the language to build the index for that document and not the language field.

db.movies.ensureIndex( { generes: "text" },
                       { language_override: "dummy" } )
like image 39
BatScream Avatar answered Oct 12 '22 01:10

BatScream