3

The new version of MongoDB allows Full Text Search. That part is running fine for me:

db.collection.runCommand('text',{search:<keyword>})

However, I'm not sure that it is possible to run it through python's mongoengine. Does anyone know if there is a way to run "runCommand" with mongoengine or a workaround?

(I'm using mongoengine for my project, I'd hate to have to drop it for pymongo as it would probably mean recoding many things.)

Thanks!

cenna75
  • 539
  • 1
  • 5
  • 13

3 Answers3

4

You can use MongoEngine by using pymongo directly eg:

class MyDoc(Document):
    pass

coll = MyDoc._get_collection()
coll.database.command(
    "text",
    coll.name,
    search="alice", 
    project={"name": 1, "_id": 0}, 
    limit=10)
Ross
  • 17,861
  • 2
  • 55
  • 73
  • Nice workaround. So what you're saying is that it is actually impossible to access the 'runcommand' command directly using Mongoengine and that the only way is to embed pymongo in there? – cenna75 Oct 22 '13 at 14:31
  • Well the above example is directly using MongoEngine, but you're correct MongoEngine uses pymongo underneath and to search you need to drop down to pymongo. – Ross Dec 11 '13 at 12:04
1

the question is old but I bumped on the same ask. MongoEngine now enables text search directly.

First, create a text index on the required fields:

class MyDoc(Document):
   document_name = StringField()
   document_content = StringField()

   meta = {
      'indexes': [
         {
            'fields': [
               '$document_name',
               '$document_content'
            ]
         }
      ]
   }

Then, documents are searchable using the search_text function:

document = News.objects.search_text('testing')

MongoEngine Text Search documentation for further details.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

Pymongo uses keyord arguments for this. See documentation.
In you case db.command('text', 'colection_name', seach='keyword').

Cezar Moise
  • 123
  • 1
  • 3
  • My point is actually to avoid using pymongo as I've been going with mongoengine. I'm looking for the mongoengine equivalent of your answer. – cenna75 Oct 18 '13 at 12:29