Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating text index in pymongo

I have a database of English words stored in the field 'h'. I've just found out about text indexes and I wanna create one on this field to speed up searching by regex, but somehow, I just can't get the syntax right. I'm using pymongo 2.7.1 and python 3.4.

 from pymongo import MongoClient
 from pymongo import ASCENDING
 from pymongo import DESCENDING
 from pymongo import TEXT
 #...
 collection.create_index('h', TEXT)

And I'm getting the following error:

in create_index
raise TypeError("cache_for must be an integer or float.")
TypeError: cache_for must be an integer or float.

Note that ASCENDING and DESCENDING work. Also I'd like to set the default language as English.

like image 528
thehousedude Avatar asked Aug 27 '14 14:08

thehousedude


Video Answer


1 Answers

Sending it as an array worked.

collection.create_index([('h', TEXT)], default_language='english')

I've also used collection.getIndexes() to check it:

    {
    "key" : {
        "h" : "text"
    },
    "ns" : "a.a",
    "name" : "h_text"
}
like image 188
thehousedude Avatar answered Oct 20 '22 01:10

thehousedude